Sha256: a1bbd5225143d0ff03fdedced2497df5020a4f6beb94f3415fc01380a4104690
Contents?: true
Size: 1.73 KB
Versions: 2
Compression:
Stored size: 1.73 KB
Contents
# frozen_string_literal: true module Rating module Extension extend ActiveSupport::Concern included do def rate(resource, value, author: self, metadata: {}, scope: nil) Rate.create author: author, metadata: metadata, 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rating-0.4.0 | lib/rating/models/rating/extension.rb |
rating-0.3.0 | lib/rating/models/rating/extension.rb |