module Rating::Extension

Public Instance Methods

rate(resource, value, author: self, extra_scopes: {}, metadata: {}, scope: nil) click to toggle source
# File lib/rating/models/rating/extension.rb, line 8
def rate(resource, value, author: self, extra_scopes: {}, metadata: {}, scope: nil)
  Rate.create(
    author:       author,
    extra_scopes: extra_scopes,
    metadata:     metadata,
    resource:     resource,
    scopeable:    scope,
    value:        value
  )
end
rate_for(resource, extra_scopes: {}, scope: nil) click to toggle source
# File lib/rating/models/rating/extension.rb, line 19
def rate_for(resource, extra_scopes: {}, scope: nil)
  Rate.rate_for author: self, extra_scopes: extra_scopes, resource: resource, scopeable: scope
end
rated(extra_scopes: {}, scope: nil) click to toggle source
# File lib/rating/models/rating/extension.rb, line 34
def rated(extra_scopes: {}, scope: nil)
  attributes = { scopeable: scope }.merge(extra_scopes)

  rated_records.where attributes
end
rated?(resource, extra_scopes: {}, scope: nil) click to toggle source

TODO: use exists for performance

# File lib/rating/models/rating/extension.rb, line 24
def rated?(resource, extra_scopes: {}, scope: nil)
  !rate_for(resource, extra_scopes: extra_scopes, scope: scope).nil?
end
rates(extra_scopes: {}, scope: nil) click to toggle source
# File lib/rating/models/rating/extension.rb, line 28
def rates(extra_scopes: {}, scope: nil)
  attributes = { scopeable: scope }.merge(extra_scopes)

  rates_records.where attributes
end
rating(scope: nil) click to toggle source
# File lib/rating/models/rating/extension.rb, line 40
def rating(scope: nil)
  rating_records.find_by scopeable: scope
end
rating_warm_up(scoping: nil) click to toggle source
# File lib/rating/models/rating/extension.rb, line 44
def rating_warm_up(scoping: nil)
  return Rating.find_or_create_by(resource: self) if scoping.blank?

  [scoping].flatten.compact.map do |attribute|
    next unless respond_to?(attribute)

    [public_send(attribute)].flatten.compact.map do |object|
      Rating.find_or_create_by! resource: self, scopeable: object
    end
  end.flatten.compact
end