Changeset 4725
- Timestamp:
- 08/08/06 20:52:53 (2 years ago)
- Files:
-
- applications/plugins/app/controllers/ratings_controller.rb (modified) (1 diff)
- applications/plugins/app/controllers/users_controller.rb (modified) (1 diff)
- applications/plugins/app/helpers/application_helper.rb (modified) (1 diff)
- applications/plugins/app/models/plugin.rb (modified) (1 diff)
- applications/plugins/app/models/rating.rb (modified) (2 diffs)
- applications/plugins/app/models/user.rb (modified) (1 diff)
- applications/plugins/app/views/layouts/application.rhtml (modified) (4 diffs)
- applications/plugins/app/views/plugins/_sub_links.rhtml (added)
- applications/plugins/app/views/plugins/show.rhtml (modified) (1 diff)
- applications/plugins/app/views/ratings/_rating.rhtml (added)
- applications/plugins/app/views/ratings/create.rjs (modified) (1 diff)
- applications/plugins/app/views/ratings/index.rhtml (added)
- applications/plugins/app/views/releases/_form.rhtml (modified) (1 diff)
- applications/plugins/app/views/releases/_release.rhtml (modified) (1 diff)
- applications/plugins/app/views/releases/index.rhtml (modified) (1 diff)
- applications/plugins/app/views/releases/show.rhtml (modified) (1 diff)
- applications/plugins/app/views/users/show.rhtml (modified) (2 diffs)
- applications/plugins/db/migrate/009_add_rating_timestamp.rb (added)
- applications/plugins/db/schema.rb (modified) (2 diffs)
- applications/plugins/public/images/37slogo-trans.gif (added)
- applications/plugins/public/images/add.gif (added)
- applications/plugins/public/images/subtract.gif (added)
- applications/plugins/public/javascripts/application.js (modified) (1 diff)
- applications/plugins/public/stylesheets/plugins.css (modified) (3 diffs)
- applications/plugins/public/stylesheets/print.css (added)
- applications/plugins/public/stylesheets/rails.css (added)
- applications/plugins/test/fixtures/ratings.yml (modified) (4 diffs)
- applications/plugins/test/functional/releases_controller_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
applications/plugins/app/controllers/ratings_controller.rb
r4722 r4725 1 1 class RatingsController < ApplicationController 2 before_filter :login_required 2 before_filter :find_plugin, :except => :create 3 before_filter :login_required, :only => :create 4 5 def index 6 @positive_ratings = @plugin.ratings.find_positive :all, :limit => 15 7 @negative_ratings = @plugin.ratings.find_negative :all, :limit => 15 8 if logged_in? 9 @releases = Release.find(:all, :order => 'releases.released_at desc') 10 @ratings = current_user.ratings.by_plugin(@plugin) 11 end 12 end 3 13 4 14 def create applications/plugins/app/controllers/users_controller.rb
r4724 r4725 12 12 13 13 def show 14 @ratings = @user.rat ed_plugins.find(:all, :order => 'plugins.created_at desc',14 @ratings = @user.ratings.find(:all, :include => [:plugin, :release], :order => 'ratings.updated_at desc, plugins.created_at desc', 15 15 :conditions => ['plugins.score = 1 and plugins.user_id != ?', @user.id], :limit => 15) 16 16 respond_to do |format| applications/plugins/app/helpers/application_helper.rb
r4722 r4725 1 # Methods added to this helper will be available to all templates in the application.2 1 module ApplicationHelper 3 2 def textilize(text) 4 3 RedCloth.new(text).to_html 5 4 end 5 6 def icon_for(rating, points) 7 link_to_function image_tag("#{points > 0 ? :add : :subtract}.gif", :size => '12x12'), 8 "Plugin.setRating(#{rating.plugin_id}, #{rating.release_id}, #{points})", 9 :id => "release-#{rating.release_id}-#{points > 0 ? :add : :subtract}", :class => (rating.points == points ? 'selected-icon' : 'icon') 10 end 6 11 end applications/plugins/app/models/plugin.rb
r4722 r4725 2 2 validates_presence_of :name, :user_id 3 3 belongs_to :user 4 has_many :ratings 4 has_many :ratings, :order => 'ratings.updated_at desc' 5 5 has_many :raters, :through => :ratings, :class_name => 'User', :uniq => true, :source => :user 6 6 has_many :comments, :order => 'comments.created_at', :include => :user applications/plugins/app/models/rating.rb
r4722 r4725 6 6 validates_numericality_of :points, :only_integer => true 7 7 before_save :set_plugin_score 8 attr_reader :old_points 9 10 class << self 11 def find_positive(*args) 12 filter_by_points 1, *args 13 end 14 15 def find_negative(*args) 16 filter_by_points -1, *args 17 end 18 19 protected 20 def filter_by_points(points, *args) 21 with_scope(:find => { :conditions => ['points = ?', points] }) { find(*args) } 22 end 23 end 24 25 def positive? 26 points > 0 27 end 28 29 def negative? 30 points < 0 31 end 8 32 9 33 def points=(new_points) … … 15 39 def set_plugin_score 16 40 Plugin.update_all "score = score + #{points.to_i - @old_points.to_i}", ['id = ?', plugin_id] 17 @old_points = nil and return true18 41 end 19 42 end applications/plugins/app/models/user.rb
r4722 r4725 1 1 class User < UserAuth 2 2 has_many :plugins 3 has_many :ratings 3 has_many :ratings do 4 def by_plugin(plugin) 5 plugin_id = plugin.is_a?(Plugin) ? plugin.id : plugin 6 find_all_by_plugin_id(plugin_id, :order => 'releases.released_at desc', :joins => 'inner join releases on ratings.release_id = releases.id') 7 end 8 end 4 9 has_many :rated_plugins, :through => :ratings, :class_name => 'Plugin', :uniq => true, :source => :plugin 5 10 applications/plugins/app/views/layouts/application.rhtml
r4722 r4725 5 5 <title>Ruby on Rails - Plugins</title> 6 6 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 7 <%= stylesheet_link_tag ' http://www.rubyonrails.com/support/weblog/rails.css' %>8 <%= stylesheet_link_tag ' http://www.rubyonrails.com/support/weblog/print.css', :media => 'print' %>7 <%= stylesheet_link_tag 'rails' %> 8 <%= stylesheet_link_tag 'print', :media => 'print' %> 9 9 <%= stylesheet_link_tag 'plugins' %> 10 10 <%= javascript_include_tag :defaults %> … … 21 21 <div id="main"> 22 22 <%= start_form_tag plugins_path, :method => 'get' %> 23 <div id="links">23 <div class="links"> 24 24 <%= link_to 'Recent Plugins', home_path %> | 25 25 <%= link_to 'Popular Plugins', popular_plugins_path %> | … … 42 42 Ruby on Rails was created by <a href="http://www.loudthinking.com/about.html">David Heinemeier Hansson</a>, a partner at <a href="http://www.37signals.com">37signals</a>,<br/> 43 43 44 then extended and improved by a <a href=" /core">core team of committers</a> and hundreds of open-source contributors.44 then extended and improved by a <a href="http://rubyonrails.org/core">core team of committers</a> and hundreds of open-source contributors. 45 45 </p> 46 46 … … 54 54 <p>Sponsored by</p> 55 55 </td> 56 <td><a href="http://www.37signals.com" class="no_link_hover"><img alt="37signals" height="22" src=" http://www.rubyonrails.org/support/weblog/37slogo-trans.gif" style="margin-top: 2px" width="100" /></a></td>56 <td><a href="http://www.37signals.com" class="no_link_hover"><img alt="37signals" height="22" src="/images/37slogo-trans.gif" style="margin-top: 2px" width="100" /></a></td> 57 57 </tr> 58 58 </table> applications/plugins/app/views/plugins/show.rhtml
r4722 r4725 1 <%= render :partial => "sub_links" %> 1 2 <%= render :partial => "plugin", :locals => { :full => true } %> 2 3 applications/plugins/app/views/ratings/create.rjs
r4722 r4725 1 #page[@plugin.dom_id(:plugin_score)].replace_html @plugin.reload.score 1 page["release-#{params[:rating][:release_id]}-spinner"].hide 2 page["release-#{params[:rating][:release_id]}-add"].className = (@rating.points > 0) ? 'selected-icon' : 'icon' 3 page["release-#{params[:rating][:release_id]}-subtract"].className = (@rating.points < 0) ? 'selected-icon' : 'icon' 4 page.call 'Plugin.resetRating', @rating.id, @rating.plugin_id, @rating.old_points, @rating.points 5 page.insert_html :top, @rating.points > 0 ? :positive : :negative, :partial => 'rating' applications/plugins/app/views/releases/_form.rhtml
r4722 r4725 5 5 6 6 <p> 7 <label for="release_released_at">Released On</label> 7 8 <%= form.date_select :released_at %> 8 9 </p> applications/plugins/app/views/releases/_release.rhtml
r4722 r4725 2 2 <div class="post"> 3 3 <h2><%= link_to release.name, release_path(release) %></h2> 4 <p class="metaData">Last updated: <%= release.released_at.to_s :standard %></p> 4 <p class="metaData"> 5 Released: <%= release.released_at.to_date.to_s :long %> | 6 <%= link_to pluralize(release.ratings_count, 'plugin'), release_path(release) %> 7 </p> 5 8 </div> 6 9 </div> applications/plugins/app/views/releases/index.rhtml
r4722 r4725 2 2 3 3 <%= render :partial => "release", :collection => @releases %> 4 5 <% if admin? -%> 6 <p><%= link_to 'Create Release', new_release_path %></p> 7 <% end -%> applications/plugins/app/views/releases/show.rhtml
r4722 r4725 1 1 <%= render :partial => "release" %> 2 3 <h2>Plugins</h2> 4 5 <%= render :partial => "plugins/plugin", :collection => @release.plugins %> 2 6 3 7 <p> 4 8 <%= link_to 'Back', releases_path %> 5 <% if @release.editable_by?(current_user)-%>9 <% if admin? -%> 6 10 | <%= link_to 'Edit', edit_release_path(@release) %> 7 11 <% end -%> applications/plugins/app/views/users/show.rhtml
r4724 r4725 17 17 18 18 <ul id="ratings"> 19 <% if @ratings.each do | plugin| -%>20 <li> <%= link_to plugin.name, plugin_path(plugin) %></li>19 <% if @ratings.each do |rating| -%> 20 <li>(<%= rating.points %>) <%= link_to rating.plugin.name, plugin_path(rating.plugin_id) %> for <%= link_to rating.release.name, release_path(rating.release_id) %></li> 21 21 <% end.empty? -%> 22 22 <li>No ratings were found.</li> … … 30 30 <p> 31 31 <label>Admin? <%= check_box_tag :admin, '1', @user.admin?, :onchange => "User.toggleAdmin('#{@user.id}')" %></label> 32 <%= image_tag 'progress.gif', : id => 'user-admin-spinner', :style => 'display:none' %>32 <%= image_tag 'progress.gif', :size => '10x10', :id => 'user-admin-spinner', :style => 'display:none' %> 33 33 </p> 34 34 </div> applications/plugins/db/schema.rb
r4722 r4725 3 3 # then regenerate this schema definition. 4 4 5 ActiveRecord::Schema.define(:version => 8) do5 ActiveRecord::Schema.define(:version => 9) do 6 6 7 7 create_table "comments", :force => true do |t| … … 27 27 t.column "points", :integer 28 28 t.column "release_id", :integer 29 t.column "updated_at", :datetime 29 30 end 30 31 applications/plugins/public/javascripts/application.js
r4724 r4725 1 1 var Plugin = { 2 2 setRating: function(plugin_id, release_id, points) { 3 if(this.icon(release_id, points).className == 'selected-icon') return; 4 $('release-' + release_id + '-spinner').show(); 5 $('release-' + release_id + '-add', 'release-' + release_id + '-subtract').each(function(a) { a.className = 'busy-icon'; }) 3 6 new Ajax.Request('/plugins/' + plugin_id + '/ratings', {parameters: 'rating[release_id]=' + release_id + '&rating[points]=' + points}); 7 }, 8 9 resetRating: function(rating_id, plugin_id, old_points, points) { 10 // update score 11 current = Number($('plugin-score-' + plugin_id).innerHTML); 12 $('plugin-score-' + plugin_id).innerHTML = current + (points - old_points); 13 14 // remove node if it exists 15 rating = $('rating-' + rating_id); 16 if(rating) rating.parentNode.removeChild(rating); 17 18 // remove 'no ratings msg' 19 ul = $((points > 0 ? 'positive' : 'negative') + '-empty'); 20 if(ul) ul.parentNode.removeChild(ul); 21 }, 22 23 icon: function(release_id, points) { 24 return $('release-' + release_id + '-' + (points > 0 ? 'add' : 'subtract')); 4 25 } 5 26 } applications/plugins/public/stylesheets/plugins.css
r4722 r4725 13 13 } 14 14 15 #links {15 .links { 16 16 font-size:12px; 17 17 text-align:center; … … 19 19 } 20 20 21 #links input {21 .links input { 22 22 font-size:11px; 23 } 24 25 div#sub-links { 26 margin-bottom:0; 27 text-align:right; 23 28 } 24 29 … … 61 66 visibility:hidden; 62 67 } 68 69 .selected-icon img { 70 background-color: #390; 71 } 72 73 .icon img { 74 background-color: #555; 75 } 76 77 .busy-icon img { 78 background-color: #999; 79 } 80 81 img { border-width:0; } applications/plugins/test/fixtures/ratings.yml
r4722 r4725 5 5 points: 1 6 6 release_id: 1 7 updated_at: <%= 7.minutes.ago.to_s :db %> 7 8 arthur_first: 8 9 id: 2 … … 11 12 points: 0 12 13 release_id: 1 14 updated_at: <%= 8.minutes.ago.to_s :db %> 13 15 quentin_another: 14 16 id: 3 … … 17 19 points: 0 18 20 release_id: 1 21 updated_at: <%= 9.minutes.ago.to_s :db %> 19 22 arthur_another: 20 23 id: 4 … … 23 26 points: 0 24 27 release_id: 2 28 updated_at: <%= 10.minutes.ago.to_s :db %> applications/plugins/test/functional/releases_controller_test.rb
r4722 r4725 14 14 end 15 15 16 def test_should_require_login_on_update_actions17 assert_requires_login do |c|18 c.get :edit, :id => 119 c.get :new20 c.post :create21 c.put :update22 c.delete :destroy23 end24 end25 26 16 def test_should_get_index 27 17 get :index … … 30 20 assert assigns(:releases) 31 21 assert_no_tag :tag => 'releases' 22 assert_no_tag 'a', :attributes => { :href => new_release_path } 23 end 24 25 def test_should_show_new_release_link_for_admins 26 login_as :quentin 27 get :index 28 assert_response :success 29 assert_tag 'a', :attributes => { :href => new_release_path } 32 30 end 33 31 … … 124 122 end 125 123 124 def test_should_require_admins_to_modify_releases 125 [nil, :aaron].each do |u| 126 assert_requires_login u do |c| 127 c.get :new 128 c.post :create 129 c.get :edit, :id => releases(:oneoh).id 130 c.put :update, :id => releases(:oneoh).id 131 c.delete :destroy, :id => releases(:oneoh).id 132 end 133 end 134 end 135 126 136 protected 127 137 def create_release(options = {})