Sha256: 573faf8d71ffa99d8c590f987cbc28daed9de3274d1410cac57a884843ba59b5

Contents?: true

Size: 1011 Bytes

Versions: 2

Compression:

Stored size: 1011 Bytes

Contents

module Ratable
  module Models
    module Ratee
      extend ActiveSupport::Concern

      attr_reader :has_one

      module ActiveRecordExtension
        def acts_as_ratee(options={has_one: false})
          @has_one = options[:has_one]
          include Ratee
        end
      end

      included do
        if @has_one
          has_one :rating, class_name: 'Ratable::Rating', dependent: :destroy, as: :ratee
        else
          has_many :ratings, class_name: 'Ratable::Rating', dependent: :destroy, as: :ratee
        end
        has_one :rating_summary, class_name: 'Ratable::RatingSummary', as: :ratee
      end

      def raters
        if @has_one
          rating.rater
        else
          ratings.includes(:rater).collect { |rating| rating.rater }
        end
      end

      def rate(options={})
        options.reject! { |k| k == :ratee }
        if @has_one
          self.rating.create(options)
        else
          self.ratings.create(options)
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ratable-0.0.2 lib/ratable/models/ratee.rb
ratable-0.0.1 lib/ratable/models/ratee.rb