Module: Mongoid::Rateable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_rateable/rateable.rb

Defined Under Namespace

Modules: ClassMethods, Ext

Instance Method Summary collapse

Instance Method Details

#previous_ratingObject


172
173
174
# File 'lib/mongoid_rateable/rateable.rb', line 172

def previous_rating
  read_attribute(:rating_previous)
end

#rate(mark, rater = nil, weight = 1) ⇒ Object

class methods


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mongoid_rateable/rateable.rb', line 103

def rate(mark, rater = nil, weight = 1)
  case rater
  when Array
    rater.each{|rater| rate(mark, rater, weight)}
  else
    if !rater
      unless respond_to?(:default_rater)
        raise ArgumentError, "No rater argument and no default_rater specified"
      end
      rater = default_rater
    end
    validate_rater!(rater)
    validate_rating!(mark)
    unrate_without_rating_update(rater)
    total_mark = mark.to_i*weight.to_i
    self.rates += total_mark
    self.rating_marks.new(:rater_id => rater.id, :mark => mark, :rater_class => rater.class.to_s, :weight => weight)
    self.weighted_rate_count += weight
    update_rating
  end
end

#rate_and_save(mark, rater, weight = 1) ⇒ Object


135
136
137
138
139
140
141
142
143
# File 'lib/mongoid_rateable/rateable.rb', line 135

def rate_and_save(mark, rater, weight = 1)
  case rater
  when Array
    rater.each{|rater| rate_and_save(mark, rater, weight)}
  else
    rate(mark, rater, weight)
    save
  end
end

#rate_countObject


186
187
188
# File 'lib/mongoid_rateable/rateable.rb', line 186

def rate_count
  self.rating_marks.size
end

#rate_weightObject


190
191
192
193
# File 'lib/mongoid_rateable/rateable.rb', line 190

def rate_weight
  check_weighted_rate_count
  read_attribute(:weighted_rate_count)
end

#rated?Boolean


155
156
157
# File 'lib/mongoid_rateable/rateable.rb', line 155

def rated?
  rate_count != 0
end

#rated_by?(rater) ⇒ Boolean


159
160
161
162
163
164
165
166
# File 'lib/mongoid_rateable/rateable.rb', line 159

def rated_by?(rater)
  case rater
  when Array
    rater.each{|rater| rated_by(mark, rater, weight)}
  else
    self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).count == 1
  end
end

#ratingObject


168
169
170
# File 'lib/mongoid_rateable/rateable.rb', line 168

def rating
  read_attribute(:rating)
end

#rating_deltaObject


176
177
178
# File 'lib/mongoid_rateable/rateable.rb', line 176

def rating_delta
  read_attribute(:rating_delta)
end

#unrate(rater) ⇒ Object


125
126
127
128
129
130
131
132
133
# File 'lib/mongoid_rateable/rateable.rb', line 125

def unrate(rater)
  case rater
  when Array
    rater.each{|rater| unrate(mark, rater, weight)}
  else
    unrate_without_rating_update(rater)
    update_rating
  end
end

#unrate_and_save(rater) ⇒ Object


145
146
147
148
149
150
151
152
153
# File 'lib/mongoid_rateable/rateable.rb', line 145

def unrate_and_save(rater)
  case rater
  when Array
    rater.each{|rater| unrate_and_save(mark, rater, weight)}
  else
    unrate(rater)
    save
  end
end

#unweighted_ratingObject


180
181
182
183
184
# File 'lib/mongoid_rateable/rateable.rb', line 180

def unweighted_rating
  return nil if self.rating_marks.empty?
  total_sum = self.rating_marks.map(&:mark).sum
  return total_sum.to_f/self.rating_marks.size
end

#user_mark(rater) ⇒ Object


195
196
197
198
# File 'lib/mongoid_rateable/rateable.rb', line 195

def user_mark(rater)
  r = self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).first
  r ? r.mark : nil
end

#user_marks(raters) ⇒ Object


200
201
202
203
204
205
206
207
# File 'lib/mongoid_rateable/rateable.rb', line 200

def user_marks(raters)
  if raters.map{|x| x.class}.uniq.count > 1
      raise ArgumentError, "Raters all must be of same class."
      return
  end
  r = self.rating_marks.in(:rater_id => raters.map(&:id), :rater_class => raters.first.class.to_s)
  r ? r.inject(Hash.new(0)) { |h, e| h[e.rater_id] = e.mark ; h } : nil
end