Sha256: 135335e7e8ddd7c4d5ffcd1966a2297ac893412f1c8900b6a9e52ec628e2e62f

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 Bytes

Contents

module Rateme
  module Macros
    extend ActiveSupport::Concern

    included do
    end

    module ClassMethods
      def rateme(options = {})
        include RequiredMethods

        has_many :ratings, :as => :rateable

        cattr_accessor :minimum_rating_allowed
        cattr_accessor :maximum_rating_allowed

        self.minimum_rating_allowed = options[:from] || 1
        self.maximum_rating_allowed = options[:upto] || 5
      end
    end

    module RequiredMethods
      def rating
        if (rating = ratings.average(:value))
          rating 
        else                     
          0.to_f                           
        end                                      
      end

      def rate(value, options = {})
        ratings.create(options.merge({ :value => value }))
      end

      def rating_range
        minimum_rating_allowed..maximum_rating_allowed
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rateme-0.1.0 lib/rateme/macros.rb