Module: Juixe::Acts::Voteable::ClassMethods

Defined in:
lib/acts_as_voteable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_voteable(options = {}) ⇒ Object

Options:

:vote_counter  
   Model stores the sum of votes in the  vote counter column when the value is true. This requires a column named `vote_total` in the table corresponding to `voteable` model.
   You can also specify a custom vote counter column by providing a column name instead of a true/false value to this option (e.g., :vote_counter => :my_custom_counter.) 
   Note: Specifying a counter will add it to that model‘s list of readonly attributes using attr_readonly.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_voteable.rb', line 18

def acts_as_voteable options={}
  has_many :votes, :as => :voteable, :dependent => :nullify
  include Juixe::Acts::Voteable::InstanceMethods
  extend  Juixe::Acts::Voteable::SingletonMethods
  if (options[:vote_counter])
    Vote.send(:include,  Juixe::Acts::Voteable::VoteCounterClassMethods) unless Vote.respond_to?(:vote_counters)
    Vote.vote_counters = [self]
    # define vote_counter_column instance method on voteable
    counter_column_name = (options[:vote_counter] == true) ? :vote_total : options[:vote_counter]
    class_eval <<-EOS
      def self.vote_counter_column           # def self.vote_counter_column
        :"#{counter_column_name}"            #   :vote_total
      end                                    # end
      def vote_counter_column                
        self.class.vote_counter_column       
      end                                    
    EOS
    
    define_method(:reload_vote_counter) {reload(:select => vote_counter_column.to_s)}
    attr_readonly counter_column_name
  end
end