docbrown/app/models/poll.rb
rhymes 5a2cae4fbb
Rubocop: enable and fix new Rails cops (#9537)
* Enable new Rails/* cops and use autocorrect on them

* Fixed Rails/PluckInWhere leftovers

* Fix Rails/DefaultScope

* Enable and fix Rails/PluckId

* Fix manual mistake with forcing autocorrection on Rails/PluckId

* Apply PR feedback to remove Rails/PluckId inline disables

* Apply PR feedback to get rid of Rails/PluckInWhere inline
2020-07-29 11:14:19 +02:00

34 lines
901 B
Ruby

class Poll < ApplicationRecord
attr_accessor :poll_options_input_array
serialize :voting_data
belongs_to :article
has_many :poll_options
has_many :poll_skips
has_many :poll_votes
validates :prompt_markdown, presence: true,
length: { maximum: 128 }
validates :poll_options_input_array, presence: true,
length: { minimum: 2, maximum: 15 }
before_save :evaluate_markdown
after_create :create_poll_options
def voting_data
{ votes_count: poll_votes_count, votes_distribution: poll_options.pluck(:id, :poll_votes_count) }
end
private
def create_poll_options
poll_options_input_array.each do |input|
PollOption.create!(markdown: input, poll_id: id)
end
end
def evaluate_markdown
self.prompt_html = MarkdownParser.new(prompt_markdown).evaluate_inline_limited_markdown
end
end