Fallback to using the first list if [poll] isn't present.

This commit is contained in:
Vikhyat Korrapati 2014-02-20 13:46:27 +05:30
parent ac8203b601
commit 33cb4bc5ac
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