lib/mongoid_rating/model.rb in mongoid_rating-0.1.1 vs lib/mongoid_rating/model.rb in mongoid_rating-0.1.2
- old
+ new
@@ -10,12 +10,14 @@
#
# rateable :overall, range: -5..5
#
# Disable average completely
# rateable :design, range: -5..5, average: false
- #
# rateable :quality, range: -5..5, average: true
+ #
+ # float: whether to allow non-integer rates (default true)
+ #
def rateable(field, options = {})
options = {
range: 1..5,
rerate: true,
counters: true,
@@ -52,10 +54,14 @@
scope :highest_#{field}, -> {
where(#{savg}.ne => nil).order_by([#{savg}, :desc])
}
def #{field}!(value, rater)
- value = value.to_i unless #{options[:float]}
+ if #{options[:float]}
+ value = value.to_f
+ else
+ value = value.to_i
+ end
unless (#{options[:range]}).include?(value)
raise "bad vote value"
end
raise "can't rate" unless can_#{field}?(rater)
un#{field}!(rater)