Module: Letsrate::ClassMethods

Defined in:
lib/letsrate/model.rb

Instance Method Summary collapse

Instance Method Details

#letsrate_rateable(*dimensions) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/letsrate/model.rb', line 86

def letsrate_rateable(*dimensions)
  has_many :rates_without_dimension, -> { where dimension: nil}, :as => :rateable, :class_name => "Rate", :dependent => :destroy
  has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater

  has_one :rate_average_without_dimension, -> { where dimension: nil}, :as => :cacheable,
          :class_name => "RatingCache", :dependent => :destroy


  dimensions.each do |dimension|
    has_many "#{dimension}_rates".to_sym, -> {where dimension: dimension.to_s},
                                          :dependent => :destroy,
                                          :class_name => "Rate",
                                          :as => :rateable

    has_many "#{dimension}_raters".to_sym, :through => "#{dimension}_rates", :source => :rater

    has_one "#{dimension}_average".to_sym, -> { where dimension: dimension.to_s },
                                          :as => :cacheable, :class_name => "RatingCache",
                                          :dependent => :destroy
  end
end

#letsrate_raterObject



82
83
84
# File 'lib/letsrate/model.rb', line 82

def letsrate_rater
  has_many :ratings_given, :class_name => "Rate", :foreign_key => :rater_id
end