Sha256: 45ae47c6750daa070236dec5bab90efb766ea82fbad77ebe54c9b6e1731d8b59

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 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

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

      def vote_up_for model
        vote :votable => model, :vote => true
      end

      def vote_down_for model
        vote :votable => model, :vote => false
      end

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

      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
      alias :voted_as_when_voting_for :voted_as_when_voting_on

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

      def find_up_votes
        find_votes :vote_flag => true
      end

      def find_down_votes
        find_votes :vote_flag => false
      end

      def find_votes_for_class klass, extra_conditions = {}
        find_votes extra_conditions.merge({:votable_type => klass.name})
      end

      def find_up_votes_for_class klass
        find_votes_for_class klass, :vote_flag => true
      end

      def find_down_votes_for_class klass
        find_votes_for_class klass, :vote_flag => false
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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