lib/rating/models/rating/rate.rb in rating-0.6.0 vs lib/rating/models/rating/rate.rb in rating-0.7.0
- old
+ new
@@ -12,24 +12,32 @@
belongs_to :scopeable, polymorphic: true
validates :author, :resource, :value, presence: true
validates :value, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 100 }
- def self.create(author:, metadata:, resource:, scopeable: nil, value:)
- record = find_or_initialize_by(author: author, resource: resource, scopeable: scopeable)
+ validates :author_id, uniqueness: {
+ case_sensitive: ::Rating::Config.validations['rate']['case_sensitive'],
+ scope: ::Rating::Config.validations['rate']['scope'].map(&:to_sym)
+ }
+ def self.create(author:, extra_scopes:, metadata:, resource:, scopeable: nil, value:)
+ attributes = { author: author, resource: resource, scopeable: scopeable }.merge(extra_scopes)
+ record = find_or_initialize_by(attributes)
+
return record if record.persisted? && value == record.value
metadata.each { |k, v| record[k] = v } if metadata.present?
record.value = value
record.save
record
end
- def self.rate_for(author:, resource:, scopeable: nil)
- find_by author: author, resource: resource, scopeable: scopeable
+ def self.rate_for(author:, extra_scopes: {}, resource:, scopeable: nil)
+ attributes = { author: author, resource: resource, scopeable: scopeable }.merge(extra_scopes)
+
+ find_by attributes
end
private
def update_rating