class Tadpoll::Poll

Public Class Methods

create_poll_with_options(name, options = []) click to toggle source

Create new poll with option

# 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

Public Instance Methods

create_options(options = []) click to toggle source

Create new option for a poll

# 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 = {}) click to toggle source

Votes with given parameters

# File lib/tadpoll/poll.rb, line 44
def find_votes_for(args = {})
  votes.where(args)
end
unvote(voter) click to toggle source

Remove vote from poll for given voter

# 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?() click to toggle source

T / F Has this Poll been voted on

# File lib/tadpoll/poll.rb, line 49
def voted_on?
  votes.count > 0
end
voted_on_by?(voter) click to toggle source

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

# File lib/tadpoll/poll.rb, line 54
def voted_on_by?(voter)
  votes = find_votes_for({voter_id: voter.id})
  votes.count > 0
end