Sha256: 60d5e873e3b30661b182cc0ece4043918f5496380fd21d1b105aa1722d29c63d

Contents?: true

Size: 783 Bytes

Versions: 2

Compression:

Stored size: 783 Bytes

Contents

# Encoding: utf-8
module ActsAsStarrable
  module Starrable
    def starrable?
      false
    end

    extend ActiveSupport::Concern

    included do
    end

    module ClassMethods
      def acts_as_starrable
        has_many :ratings, :as => :starrable, :dependent => :destroy,
                           :class_name => 'ActsAsStarrable::Rating'
        include ActsAsStarrable::Starrable::LocalInstanceMethods
      end
    end

    module LocalInstanceMethods
      def starrable?
        true
      end

      def average_rating
        if avg = ratings.average('rating')
          (avg * 2).round / 2.0 # round to nearest .5 or .0
        end
      end

      def max_rating
        5
      end
    end
  end
end

ActiveRecord::Base.send :include, ActsAsStarrable::Starrable

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_starrable-0.0.2 lib/acts_as_starrable/starrable.rb
acts_as_starrable-0.0.1 lib/acts_as_starrable/starrable.rb