Add close/open poll buttons to poll UI.

This commit is contained in:
Vikhyat Korrapati 2014-04-12 22:53:21 +05:30
parent cc607dc0b5
commit 5de7d889fd
5 changed files with 137 additions and 40 deletions

View file

@ -24,6 +24,18 @@
{{/if}}
</button>
{{#if poll.post.topic.details.can_edit}}
<button {{action toggleClosePoll}} class="btn btn-small">
{{#if poll.closed}}
<i class="fa fa-unlock-alt"></i>
{{i18n poll.open_poll}}
{{else}}
<i class="fa fa-lock"></i>
{{i18n poll.close_poll}}
{{/if}}
</button>
{{/if}}
{{#if loading}}
<i class="fa fa-spin fa-spinner"></i>
{{/if}}

View file

@ -44,7 +44,6 @@ var Poll = Discourse.Model.extend({
var PollController = Discourse.Controller.extend({
poll: null,
showResults: Em.computed.oneWay('poll.closed'),
disableRadio: Em.computed.any('poll.closed', 'loading'),
actions: {
@ -67,6 +66,18 @@ var PollController = Discourse.Controller.extend({
toggleShowResults: function() {
this.set('showResults', !this.get('showResults'));
},
toggleClosePoll: function() {
this.set('loading', true);
return Discourse.ajax("/poll/toggle_close", {
type: "PUT",
data: {post_id: this.get('poll.post.id')}
}).then(function(topicJson) {
this.set('poll.post.topic.title', topicJson.basic_topic.title);
this.set('poll.post.topic.fancy_title', topicJson.basic_topic.title);
this.set('loading', false);
}.bind(this));
}
}
});

View file

@ -15,3 +15,6 @@ en:
results:
show: Show Results
hide: Hide Results
close_poll: "Close Poll"
open_poll: "Open Poll"

View file

@ -58,11 +58,45 @@ after_initialize do
render json: poll.serialize(current_user)
end
def toggle_close
post = Post.find(params[:post_id])
topic = post.topic
poll = PollPlugin::Poll.new(post)
# Make sure the user is allowed to close the poll.
Guardian.new(current_user).ensure_can_edit!(topic)
# Make sure this is actually a poll.
unless poll.has_poll_details?
render status: 400, json: false
return
end
# Make sure the topic is not closed.
if topic.closed?
render status: 400, json: false
return
end
# Modify topic title.
if topic.title =~ /^(#{I18n.t('poll.prefix').strip})\s?:/i
topic.title = topic.title.gsub(/^(#{I18n.t('poll.prefix').strip})\s?:/i, I18n.t('poll.closed_prefix') + ':')
elsif topic.title =~ /^(#{I18n.t('poll.closed_prefix').strip})\s?:/i
topic.title = topic.title.gsub(/^(#{I18n.t('poll.closed_prefix').strip})\s?:/i, I18n.t('poll.prefix') + ':')
end
topic.acting_user = current_user
topic.save!
render json: topic, serializer: BasicTopicSerializer
end
end
end
PollPlugin::Engine.routes.draw do
put '/' => 'poll#vote'
put '/toggle_close' => 'poll#toggle_close'
end
Discourse::Application.routes.append do

View file

@ -2,52 +2,89 @@ require 'spec_helper'
describe PollPlugin::PollController, type: :controller do
let(:topic) { create_topic(title: "Poll: Chitoge vs Onodera") }
let(:post) { create_post(topic: topic, raw: "Pick one.\n\n[poll]\n* Chitoge\n* Onodera\n[/poll]") }
let!(:post) { create_post(topic: topic, raw: "Pick one.\n\n[poll]\n* Chitoge\n* Onodera\n[/poll]") }
let(:user1) { Fabricate(:user) }
let(:user2) { Fabricate(:user) }
let(:admin) { Fabricate(:admin) }
it "should return 403 if no user is logged in" do
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
response.should be_forbidden
describe 'vote' do
it "returns 403 if no user is logged in" do
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
response.should be_forbidden
end
it "returns 400 if post_id or invalid option is not specified" do
log_in_user user1
xhr :put, :vote, use_route: :poll
response.status.should eq(400)
xhr :put, :vote, post_id: post.id, use_route: :poll
response.status.should eq(400)
xhr :put, :vote, option: "Chitoge", use_route: :poll
response.status.should eq(400)
xhr :put, :vote, post_id: post.id, option: "Tsugumi", use_route: :poll
response.status.should eq(400)
end
it "returns 400 if post_id doesn't correspond to a poll post" do
log_in_user user1
post2 = create_post(topic: topic, raw: "Generic reply")
xhr :put, :vote, post_id: post2.id, option: "Chitoge", use_route: :poll
end
it "saves votes correctly" do
MessageBus.expects(:publish).times(3)
log_in_user user1
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
PollPlugin::Poll.new(post).get_vote(user1).should eq("Chitoge")
log_in_user user2
xhr :put, :vote, post_id: post.id, option: "Onodera", use_route: :poll
PollPlugin::Poll.new(post).get_vote(user2).should eq("Onodera")
PollPlugin::Poll.new(post).details["Chitoge"].should eq(1)
PollPlugin::Poll.new(post).details["Onodera"].should eq(1)
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
PollPlugin::Poll.new(post).get_vote(user2).should eq("Chitoge")
PollPlugin::Poll.new(post).details["Chitoge"].should eq(2)
PollPlugin::Poll.new(post).details["Onodera"].should eq(0)
end
end
it "should return 400 if post_id or invalid option is not specified" do
log_in_user user1
xhr :put, :vote, use_route: :poll
response.status.should eq(400)
xhr :put, :vote, post_id: post.id, use_route: :poll
response.status.should eq(400)
xhr :put, :vote, option: "Chitoge", use_route: :poll
response.status.should eq(400)
xhr :put, :vote, post_id: post.id, option: "Tsugumi", use_route: :poll
response.status.should eq(400)
end
describe 'toggle_close' do
it "returns 400 if post_id doesn't correspond to a poll post" do
log_in_user admin
post2 = create_post(topic: topic, raw: "Generic reply")
xhr :put, :toggle_close, post_id: post2.id, use_route: :poll
response.status.should eq(400)
end
it "should return 400 if post_id doesn't correspond to a poll post" do
log_in_user user1
post2 = create_post(topic: topic, raw: "Generic reply")
xhr :put, :vote, post_id: post2.id, option: "Chitoge", use_route: :poll
response.status.should eq(400)
end
it "returns 400 if the topic is locked" do
log_in_user admin
topic.update_attributes closed: true
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
response.status.should eq(400)
end
it "should save votes correctly" do
MessageBus.expects(:publish).times(4)
it "raises Discourse::InvalidAccess is the user is not authorized" do
log_in_user user1
expect do
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
end.to raise_error(Discourse::InvalidAccess)
end
log_in_user user1
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
PollPlugin::Poll.new(post).get_vote(user1).should eq("Chitoge")
log_in_user user2
xhr :put, :vote, post_id: post.id, option: "Onodera", use_route: :poll
PollPlugin::Poll.new(post).get_vote(user2).should eq("Onodera")
PollPlugin::Poll.new(post).details["Chitoge"].should eq(1)
PollPlugin::Poll.new(post).details["Onodera"].should eq(1)
xhr :put, :vote, post_id: post.id, option: "Chitoge", use_route: :poll
PollPlugin::Poll.new(post).get_vote(user2).should eq("Chitoge")
PollPlugin::Poll.new(post).details["Chitoge"].should eq(2)
PollPlugin::Poll.new(post).details["Onodera"].should eq(0)
it "renames the topic" do
I18n.stubs(:t).with('poll.prefix').returns("Poll ")
I18n.stubs(:t).with('poll.closed_prefix').returns("Closed Poll ")
log_in_user admin
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
response.status.should eq(200)
topic.reload.title.should == "Closed Poll : Chitoge vs Onodera"
xhr :put, :toggle_close, post_id: post.id, use_route: :poll
response.status.should eq(200)
topic.reload.title.should == "Poll : Chitoge vs Onodera"
end
end
end