Sha256: b33e6b957bc87cf9b164864e2712cd1452b1dc5b75303960c0e96b98801ee1fa

Contents?: true

Size: 1023 Bytes

Versions: 6

Compression:

Stored size: 1023 Bytes

Contents

class Poll < ::Renderable
  include E9Polls::Model
  include E9Rails::ActiveRecord::InheritableOptions

  self.options_parameters = [
    :header_text
  ]

  has_many :poll_answers

  accepts_nested_attributes_for :poll_answers, :allow_destroy => true, :reject_if => :reject_answer?

  validates :template, :presence => true

  def vote
    nil
  end

  def as_json(options={})
    {}.tap do |hash|
      hash[:id]       = self.id,
      hash[:name]     = self.name,
      hash[:question] = self.question,
      hash[:answers]  = self.answers,
      hash[:errors]   = self.errors
    end
  end

  def question
    template
  end

  def answers
    poll_answers
  end

  def vote=(id)
    poll_answers.find_by_id(id).try(:vote!)
  end

  def votes
    poll_answers.sum(:votes).to_i
  end

  def percentage_for(poll_answer)
    votes.zero? ? 0 : (poll_answer.votes.to_f / votes * 100).round(2)
  end

  protected

  def reject_answer?(attributes)
    !attributes.keys.member?('value') || attributes['value'].blank?
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
e9_polls-1.0.10 app/models/poll.rb
e9_polls-1.0.9 app/models/poll.rb
e9_polls-1.0.8 app/models/poll.rb
e9_polls-1.0.7 app/models/poll.rb
e9_polls-1.0.6 app/models/poll.rb
e9_polls-1.0.5 app/models/poll.rb