Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4725

Show
Ignore:
Timestamp:
08/08/06 20:52:53 (2 years ago)
Author:
rick
Message:

add ratings per plugin and release

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • applications/plugins/app/controllers/ratings_controller.rb

    r4722 r4725  
    11class 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 
    313 
    414  def create 
  • applications/plugins/app/controllers/users_controller.rb

    r4724 r4725  
    1212   
    1313  def show 
    14     @ratings = @user.rated_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',  
    1515                 :conditions => ['plugins.score = 1 and plugins.user_id != ?', @user.id], :limit => 15) 
    1616    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. 
    21module ApplicationHelper 
    32  def textilize(text) 
    43    RedCloth.new(text).to_html 
    54  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 
    611end 
  • applications/plugins/app/models/plugin.rb

    r4722 r4725  
    22  validates_presence_of :name, :user_id 
    33  belongs_to :user 
    4   has_many :ratings 
     4  has_many :ratings, :order => 'ratings.updated_at desc' 
    55  has_many :raters, :through => :ratings, :class_name => 'User', :uniq => true, :source => :user 
    66  has_many :comments, :order => 'comments.created_at', :include => :user 
  • applications/plugins/app/models/rating.rb

    r4722 r4725  
    66  validates_numericality_of :points, :only_integer => true 
    77  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 
    832 
    933  def points=(new_points) 
     
    1539    def set_plugin_score 
    1640      Plugin.update_all "score = score + #{points.to_i - @old_points.to_i}", ['id = ?', plugin_id] 
    17       @old_points = nil and return true 
    1841    end 
    1942end 
  • applications/plugins/app/models/user.rb

    r4722 r4725  
    11class User < UserAuth 
    22  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 
    49  has_many :rated_plugins, :through => :ratings, :class_name => 'Plugin', :uniq => true, :source => :plugin 
    510 
  • applications/plugins/app/views/layouts/application.rhtml

    r4722 r4725  
    55  <title>Ruby on Rails - Plugins</title> 
    66  <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' %> 
    99  <%= stylesheet_link_tag 'plugins' %> 
    1010  <%= javascript_include_tag :defaults %> 
     
    2121    <div id="main"> 
    2222      <%= start_form_tag plugins_path, :method => 'get' %> 
    23         <div id="links"> 
     23        <div class="links"> 
    2424          <%= link_to 'Recent Plugins', home_path %> | 
    2525          <%= link_to 'Popular Plugins', popular_plugins_path %> | 
     
    4242          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/> 
    4343 
    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. 
    4545        </p> 
    4646         
     
    5454            <p>Sponsored by</p> 
    5555          </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> 
    5757        </tr> 
    5858      </table> 
  • applications/plugins/app/views/plugins/show.rhtml

    r4722 r4725  
     1<%= render :partial => "sub_links" %> 
    12<%= render :partial => "plugin", :locals => { :full => true } %> 
    23 
  • applications/plugins/app/views/ratings/create.rjs

    r4722 r4725  
    1 #page[@plugin.dom_id(:plugin_score)].replace_html @plugin.reload.score 
     1page["release-#{params[:rating][:release_id]}-spinner"].hide 
     2page["release-#{params[:rating][:release_id]}-add"].className = (@rating.points > 0) ? 'selected-icon' : 'icon' 
     3page["release-#{params[:rating][:release_id]}-subtract"].className = (@rating.points < 0) ? 'selected-icon' : 'icon' 
     4page.call 'Plugin.resetRating', @rating.id, @rating.plugin_id, @rating.old_points, @rating.points 
     5page.insert_html :top, @rating.points > 0 ? :positive : :negative, :partial => 'rating' 
  • applications/plugins/app/views/releases/_form.rhtml

    r4722 r4725  
    55 
    66<p> 
     7  <label for="release_released_at">Released On</label> 
    78  <%= form.date_select :released_at %> 
    89</p> 
  • applications/plugins/app/views/releases/_release.rhtml

    r4722 r4725  
    22  <div class="post"> 
    33    <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> 
    58    </div> 
    69</div> 
  • applications/plugins/app/views/releases/index.rhtml

    r4722 r4725  
    22 
    33<%= 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  
    11<%= render :partial => "release" %> 
     2 
     3<h2>Plugins</h2> 
     4 
     5<%= render :partial => "plugins/plugin", :collection => @release.plugins %> 
    26 
    37<p> 
    48  <%= link_to 'Back', releases_path %> 
    5 <% if @release.editable_by?(current_user) -%> 
     9<% if admin? -%> 
    610  | <%= link_to 'Edit', edit_release_path(@release) %> 
    711<% end -%> 
  • applications/plugins/app/views/users/show.rhtml

    r4724 r4725  
    1717   
    1818    <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> 
    2121    <% end.empty? -%> 
    2222      <li>No ratings were found.</li> 
     
    3030  <p> 
    3131    <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' %> 
    3333  </p> 
    3434</div> 
  • applications/plugins/db/schema.rb

    r4722 r4725  
    33# then regenerate this schema definition. 
    44 
    5 ActiveRecord::Schema.define(:version => 8) do 
     5ActiveRecord::Schema.define(:version => 9) do 
    66 
    77  create_table "comments", :force => true do |t| 
     
    2727    t.column "points",     :integer 
    2828    t.column "release_id", :integer 
     29    t.column "updated_at", :datetime 
    2930  end 
    3031 
  • applications/plugins/public/javascripts/application.js

    r4724 r4725  
    11var Plugin = { 
    22  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'; }) 
    36    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')); 
    425  } 
    526} 
  • applications/plugins/public/stylesheets/plugins.css

    r4722 r4725  
    1313} 
    1414 
    15 #links { 
     15.links { 
    1616  font-size:12px; 
    1717  text-align:center; 
     
    1919} 
    2020 
    21 #links input { 
     21.links input { 
    2222  font-size:11px; 
     23} 
     24 
     25div#sub-links { 
     26  margin-bottom:0; 
     27  text-align:right; 
    2328} 
    2429 
     
    6166  visibility:hidden; 
    6267} 
     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 
     81img { border-width:0; } 
  • applications/plugins/test/fixtures/ratings.yml

    r4722 r4725  
    55  points: 1 
    66  release_id: 1 
     7  updated_at: <%= 7.minutes.ago.to_s :db %> 
    78arthur_first: 
    89  id: 2 
     
    1112  points: 0 
    1213  release_id: 1 
     14  updated_at: <%= 8.minutes.ago.to_s :db %> 
    1315quentin_another: 
    1416  id: 3 
     
    1719  points: 0 
    1820  release_id: 1 
     21  updated_at: <%= 9.minutes.ago.to_s :db %> 
    1922arthur_another: 
    2023  id: 4 
     
    2326  points: 0 
    2427  release_id: 2 
     28  updated_at: <%= 10.minutes.ago.to_s :db %> 
  • applications/plugins/test/functional/releases_controller_test.rb

    r4722 r4725  
    1414  end 
    1515 
    16   def test_should_require_login_on_update_actions 
    17     assert_requires_login do |c| 
    18       c.get    :edit, :id => 1 
    19       c.get    :new 
    20       c.post   :create 
    21       c.put    :update 
    22       c.delete :destroy 
    23     end 
    24   end 
    25  
    2616  def test_should_get_index 
    2717    get :index 
     
    3020    assert assigns(:releases) 
    3121    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 } 
    3230  end 
    3331 
     
    124122  end 
    125123 
     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 
    126136  protected 
    127137    def create_release(options = {})