Class: Tadpoll::Option

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/tadpoll/option.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_option(name, poll_id) ⇒ Object

Create Option



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tadpoll/option.rb', line 9

def self.new_option(name, poll_id)
  return false if name.blank?

  option = Tadpoll::Option.new(name: name, poll_id: poll_id)

  if option.save
    return option
  else
    return false
  end
end

Instance Method Details

#find_votes_for(voter) ⇒ Object

Votes with given parameters



54
55
56
# File 'lib/tadpoll/option.rb', line 54

def find_votes_for(voter)
  votes.where(voter_id: voter.id)
end

#unvote(voter) ⇒ Object

Destory Votes with given parameters



44
45
46
47
48
49
50
51
# File 'lib/tadpoll/option.rb', line 44

def unvote(voter)
  return false if voter.nil?
  if self.voted_on_by?(voter)
    _votes_ = find_votes_for(voter)
    _votes_.each(&:destroy)
  end
  return true
end

#vote(voter) ⇒ Object

Vote



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tadpoll/option.rb', line 22

def vote(voter)
  return false if voter.nil?

  if self.poll.voted_on_by?(voter)
    return false
  else 
    vote = Tadpoll::Vote.new(
      option: self,
      voter_id: voter.id,
      voter_type: voter.class.base_class.name,
      poll_id: self.poll_id
    )
  end

  if vote.save
    return true
  else
    return false
  end
end

#vote_countObject

Count of Votes for an Option



59
60
61
# File 'lib/tadpoll/option.rb', line 59

def vote_count
  votes.count
end

#voted_on?Boolean

T / F Has the Option been voted on

Returns:

  • (Boolean)


64
65
66
# File 'lib/tadpoll/option.rb', line 64

def voted_on?
  votes.count > 0
end

#voted_on_by?(voter) ⇒ Boolean

T / F Has this Option been voted on by given Voter

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/tadpoll/option.rb', line 69

def voted_on_by?(voter)
  votes = find_votes_for(voter)
  votes.count > 0
end