Module: AjaxfulRating::ClassMethods

Defined in:
lib/axr/model.rb

Instance Method Summary collapse

Instance Method Details

#ajaxful_rateable(options = {}) ⇒ Object

Extends the model to be easy ajaxly rateable.

Options:

  • :stars Max number of stars that can be submitted.

  • :allow_update Set to true if you want users to be able to update their votes.

  • :cache_column Name of the column for storing the cached rating average.

Example:

class Article < ActiveRecord::Base
  ajaxful_rateable :stars => 10, :cache_column => :custom_column
end


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/axr/model.rb', line 20

def ajaxful_rateable(options = {})
  has_many :rates_without_dimension, :as => :rateable, :class_name => 'Rate',
    :dependent => :destroy, :conditions => {:dimension => nil}
  has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
  
  options[:dimensions].each do |dimension|
    has_many "#{dimension}_rates", :dependent => :destroy,
      :conditions => {:dimension => dimension.to_s}, :class_name => 'Rate', :as => :rateable
    has_many "#{dimension}_raters", :through => "#{dimension}_rates", :source => :rater
  end if options[:dimensions].is_a?(Array)

  class << self
    def axr_config
      @axr_config ||= {
        :stars => 5,
        :allow_update => true,
        :cache_column => :rating_average
      }
    end
    
    alias_method :ajaxful_rating_options, :axr_config
  end
  
  axr_config.update(options)
  
  include AjaxfulRating::InstanceMethods
  extend AjaxfulRating::SingletonMethods
end

#ajaxful_rater(options = {}) ⇒ Object

Makes the association between user and Rate model.



50
51
52
# File 'lib/axr/model.rb', line 50

def ajaxful_rater(options = {})
  has_many :ratings_given, options.merge(:class_name => "Rate", :foreign_key => :rater_id)
end