Merge pull request #1979 from vikhyat/poll-plugin

Fallback to using the first list if [poll] isn't present
This commit is contained in:
Régis Hanol 2014-02-21 10:36:49 +01:00
commit bbd14c5589
3 changed files with 18 additions and 4 deletions

View File

@ -97,7 +97,13 @@ Discourse.PostView.reopen({
}
var view = initializePollView(this);
view.replaceElement($post.find(".poll-ui:first"));
var pollContainer = $post.find(".poll-ui:first");
if (pollContainer.length == 0) {
pollContainer = $post.find("ul:first");
}
view.replaceElement(pollContainer);
this.set('pollView', view);
}.on('postViewInserted'),

View File

@ -26,9 +26,10 @@ module ::PollPlugin
def options
cooked = PrettyText.cook(@post.raw, topic_id: @post.topic_id)
poll_div = Nokogiri::HTML(cooked).css(".poll-ui").first
if poll_div
poll_div.css("li").map {|x| x.children.to_s.strip }.uniq
parsed = Nokogiri::HTML(cooked)
poll_list = parsed.css(".poll-ui ul").first || parsed.css("ul").first
if poll_list
poll_list.css("li").map {|x| x.children.to_s.strip }.uniq
else
[]
end

View File

@ -18,6 +18,13 @@ describe PollPlugin::Poll do
expect(poll.options).to eq(["Chitoge", "Onodera"])
end
it "should fall back to using the first list if [poll] markup is not present" do
topic = create_topic(title: "This is not a poll topic")
post = create_post(topic: topic, raw: "Pick one.\n\n* Chitoge\n* Onodera")
poll = PollPlugin::Poll.new(post)
expect(poll.options).to eq(["Chitoge", "Onodera"])
end
it "should get details correctly" do
expect(poll.details).to eq({"Chitoge" => 0, "Onodera" => 0})
end