Sha256: d638dd13c74fea15caf16befdc66cf52fc78b9857a082ab27787d00f6828b832
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
# frozen_string_literal: true module Rating module Extension extend ActiveSupport::Concern included do def rate(resource, value, author: self, scope: nil) Rate.create author: author, resource: resource, scopeable: scope, value: value end def rate_for(resource, scope: nil) Rate.rate_for author: self, resource: resource, scopeable: scope end def rated?(resource, scope: nil) !rate_for(resource, scope: scope).nil? end def rates(scope: nil) rates_records.where scopeable: scope end def rated(scope: nil) rated_records.where scopeable: scope end def rating(scope: nil) rating_records.find_by scopeable: scope end end module ClassMethods def rating(as: nil) after_create -> { Rating.find_or_create_by resource: self }, unless: -> { as == :author } has_many :rating_records, as: :resource, class_name: '::Rating::Rating', dependent: :destroy has_many :rates_records, as: :resource, class_name: '::Rating::Rate', dependent: :destroy has_many :rated_records, as: :author, class_name: '::Rating::Rate', dependent: :destroy scope :order_by_rating, ->(column = :estimate, direction = :desc, scope: nil) { scope_values = { scopeable_id: scope&.id, scopeable_type: scope&.class&.base_class&.name } includes(:rating_records) .where(Rating.table_name => scope_values) .order("#{Rating.table_name}.#{column} #{direction}") } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rating-0.2.0 | lib/rating/models/rating/extension.rb |