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); 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); this.set('pollView', view);
}.on('postViewInserted'), }.on('postViewInserted'),

View file

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

View file

@ -18,6 +18,13 @@ describe PollPlugin::Poll do
expect(poll.options).to eq(["Chitoge", "Onodera"]) expect(poll.options).to eq(["Chitoge", "Onodera"])
end 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 it "should get details correctly" do
expect(poll.details).to eq({"Chitoge" => 0, "Onodera" => 0}) expect(poll.details).to eq({"Chitoge" => 0, "Onodera" => 0})
end end