Sha256: 9bfa893447829191685ae8116cb40dbfb235f09d96c6b1c41209921fe4f51277

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Rating
  module Extension
    extend ActiveSupport::Concern

    included do
      def rate(resource, value, author: self)
        Rate.create author: author, resource: resource, value: value
      end

      def rate_for(resource)
        Rate.rate_for author: self, resource: resource
      end

      def rated?(resource)
        !rate_for(resource).nil?
      end
    end

    module ClassMethods
      def rating
        after_create { Rating.find_or_create_by resource: self }

        has_one :rating,
          as:         :resource,
          class_name: '::Rating::Rating',
          dependent:  :destroy

        has_many :rates,
          as:         :resource,
          class_name: '::Rating::Rate',
          dependent:  :destroy

        has_many :rated,
          as:         :author,
          class_name: '::Rating::Rate',
          dependent:  :destroy

        scope :order_by_rating, ->(column = :estimate, direction = :desc) {
          includes(:rating).order("#{Rating.table_name}.#{column} #{direction}")
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rating-0.1.0 lib/rating/models/rating/extension.rb