Class: Tadpoll::Poll

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_poll_with_options(name, options = []) ⇒ Object

Create new poll with option



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tadpoll/poll.rb', line 11

def self.create_poll_with_options(name, options = [])
  return false if name.blank?

  poll = Tadpoll::Poll.new(name: name)

  if poll.save
    poll.create_options(options)
    return poll
  else
    return false
  end
end

Instance Method Details

#create_options(options = []) ⇒ Object

Create new option for a poll



25
26
27
28
29
30
31
# File 'lib/tadpoll/poll.rb', line 25

def create_options(options = [])
  if options.any?
    options.each do |option|
      Tadpoll::Option.new_option(option, self.id)
    end
  end
end

#find_votes_for(args = {}) ⇒ Object

Votes with given parameters



44
45
46
# File 'lib/tadpoll/poll.rb', line 44

def find_votes_for(args = {})
  votes.where(args)
end

#unvote(voter) ⇒ Object

Remove vote from poll for given voter



34
35
36
37
38
39
40
41
# File 'lib/tadpoll/poll.rb', line 34

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

#voted_on?Boolean

T / F Has this Poll been voted on

Returns:

  • (Boolean)


49
50
51
# File 'lib/tadpoll/poll.rb', line 49

def voted_on?
  votes.count > 0
end

#voted_on_by?(voter) ⇒ Boolean

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

Returns:

  • (Boolean)


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

def voted_on_by?(voter)
  votes = find_votes_for({voter_id: voter.id})
  votes.count > 0
end