Extract the parsing of polls

This commit is contained in:
Gerhard Schlager 2015-05-02 23:44:45 +02:00
parent 83b7620cb2
commit 9bc750e43d
1 changed files with 33 additions and 28 deletions

View File

@ -105,6 +105,38 @@ after_initialize do
polls[poll_name] polls[poll_name]
end end
end end
def extract(raw, topic_id)
# TODO: we should fix the callback mess so that the cooked version is available
# in the validators instead of cooking twice
cooked = PrettyText.cook(raw, topic_id: topic_id)
parsed = Nokogiri::HTML(cooked)
extracted_polls = []
# extract polls
parsed.css("div.poll").each do |p|
poll = { "options" => [], "total_votes" => 0 }
# extract attributes
p.attributes.values.each do |attribute|
if attribute.name.start_with?(DATA_PREFIX)
poll[attribute.name[DATA_PREFIX.length..-1]] = attribute.value
end
end
# extract options
p.css("li[#{DATA_PREFIX}option-id]").each do |o|
option_id = o.attributes[DATA_PREFIX + "option-id"].value
poll["options"] << { "id" => option_id, "html" => o.inner_html, "votes" => 0 }
end
# add the poll
extracted_polls << poll
end
extracted_polls
end
end end
end end
@ -176,36 +208,9 @@ after_initialize do
# only care when raw has changed! # only care when raw has changed!
return unless self.raw_changed? return unless self.raw_changed?
# TODO: we should fix the callback mess so that the cooked version is available extracted_polls = DiscoursePoll::Poll::extract(self.raw, self.topic_id)
# in the validators instead of cooking twice
cooked = PrettyText.cook(self.raw, topic_id: self.topic_id)
parsed = Nokogiri::HTML(cooked)
polls = {} polls = {}
extracted_polls = []
# extract polls
parsed.css("div.poll").each do |p|
poll = { "options" => [], "total_votes" => 0 }
# extract attributes
p.attributes.values.each do |attribute|
if attribute.name.start_with?(DATA_PREFIX)
poll[attribute.name[DATA_PREFIX.length..-1]] = attribute.value
end
end
# extract options
p.css("li[#{DATA_PREFIX}option-id]").each do |o|
option_id = o.attributes[DATA_PREFIX + "option-id"].value
poll["options"] << { "id" => option_id, "html" => o.inner_html, "votes" => 0 }
end
# add the poll
extracted_polls << poll
end
# validate polls
extracted_polls.each do |poll| extracted_polls.each do |poll|
# polls should have a unique name # polls should have a unique name
if polls.has_key?(poll["name"]) if polls.has_key?(poll["name"])