Skip to content

Commit

Permalink
new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto committed Dec 16, 2008
1 parent 14f26e5 commit a69c48b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
13 changes: 12 additions & 1 deletion lib/become_contest.rb
Expand Up @@ -23,8 +23,19 @@ def contest_of(association, options = {})

module InstanceMethods

def top(length = 10)
def vote_for(voteable, options = {})
self.vote(voteable, true)
end

def vote_against(voteable, options = {})
self.vote(voteable, false)
end

def vote(voteable, vote, options = {})
voter = options[:voter] || options[:by]

vote = self.votes.build(:vote => vote, :voteable => voteable, :voter => voter)
vote.save
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/become_contestable.rb
Expand Up @@ -25,6 +25,10 @@ def contestable_on(association, options = {})


module InstanceMethods

def contest
send(self.class.contestable_options[:association])
end

end

Expand Down
29 changes: 26 additions & 3 deletions lib/become_voter.rb
Expand Up @@ -7,7 +7,7 @@ def self.included(base)
module ActMethods

def become_voter
has_many :votes
has_many :votes, :as => :voter

unless included_modules.include? InstanceMethods
extend ClassMethods
Expand All @@ -26,9 +26,32 @@ def vote_against(voteable, options = {})
end

def vote(voteable, vote, options = {})
vote = self.votes.build(:vote => vote, :voteable => voteable, :contest => options[:on])
contest = options[:contest] || options[:on]

vote = self.votes.build(:vote => vote, :voteable => voteable, :contest => contest)
vote.save
end
end


def voted_for?(voteable, options = {})
voted?(voteable, true, options = {})
end

def voted_against?(voteable, options = {})
voted?(voteable, false, options = {})
end

def voted_on?(voteable, options = {})
contest = options[:contest] || options[:on] || voteable.contest
0 < self.votes.count(:all, :conditions => {:contest_id => contest.id, :contest_type => contest.class.name, :voteable_id => voteable.id, :voteable_type => voteable.class.name})
end

def voted?(voteable, for_or_against, options = {})
contest = options[:contest] || options[:on] || voteable.contest
0 < self.votes.count(:all, :conditions => {:contest_id => contest.id, :contest_type => contest.class.name, :vote => for_or_against, :voteable_id => voteable.id, :voteable_type => voteable.class.name})
end

private :voted?
end

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion lib/models/vote.rb
Expand Up @@ -2,6 +2,6 @@ class Vote < ActiveRecord::Base

belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
belongs_to :contest
belongs_to :contest, :polymorphic => true

end

0 comments on commit a69c48b

Please sign in to comment.