Sha256: 82f49e5bae6146a6b36ec48f8a12ef1addf55c016b5752e308c29e15fcdb7049

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module ActsAsVotable
  module Voter

    def self.included(base)
      base.send :include, ActsAsVotable::Voter::InstanceMethods
    end


    module ClassMethods
    end

    module InstanceMethods

      def default_conditions
        {
          :voter_id => self.id,
          :voter_type => self.class.name
        }
      end

      def voted_on? votable
        votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name)
        votes.size > 0
      end

      def voted_as_when_voting_on votable
        votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name)
        return nil if votes.size == 0
        return votes.first.vote_flag
      end

      def find_votes extra_conditions = {}
        ActsAsVotable::Vote.find(:all, :conditions => default_conditions.merge(extra_conditions))
      end

      def total_votes_for_class klass
        find_votes({:votable_type => klass.name}).size
      end

      def vote args
        args[:votable].vote args.merge({:voter => self})
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_votable-0.0.4 lib/acts_as_votable/voter.rb