Module: AjaxfulRating::ClassMethods

Defined in:
lib/ajaxful_rating_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/ajaxful_rating_model.rb', line 13

def options
  @options
end

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


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ajaxful_rating_model.rb', line 26

def ajaxful_rateable(options = {})
  has_many :rates_without_dimension, :as => :rateable, :class_name => 'Rate',
    :dependent => :destroy, :conditions => {:dimension => nil}

  
  options[:dimensions].each do |dimension|
    has_many "#{dimension}_rates", :dependent => :destroy,
      :conditions => {:dimension => dimension.to_s}, :class_name => 'Rate', :as => :rateable
  end if options[:dimensions].is_a?(Array)

  @options = options.reverse_merge(
    :stars => 5,
    :allow_update => true,
    :cache_column => :rating_average
  )
  include AjaxfulRating::InstanceMethods
  extend AjaxfulRating::SingletonMethods
end

#ajaxful_rater(options = {}) ⇒ Object

Makes the association between user and Rate model.



46
47
48
# File 'lib/ajaxful_rating_model.rb', line 46

def ajaxful_rater(options = {})
  has_many :rates, options
end

#max_rate_valueObject

Maximum value accepted when rating the model. Default is 5.

Change it by passing the :stars option to ajaxful_rateable

ajaxful_rateable :stars => 10


55
56
57
# File 'lib/ajaxful_rating_model.rb', line 55

def max_rate_value
  options[:stars]
end