Poll Plugin: Don't allow voting on closed topics.

This commit is contained in:
Vikhyat Korrapati 2014-03-16 19:18:40 +05:30
parent ae3f135c33
commit 9ef104bbce
4 changed files with 17 additions and 1 deletions

View File

@ -1,7 +1,7 @@
<table> <table>
{{#each poll.options}} {{#each poll.options}}
<tr {{bind-attr class=checked:active}} {{action selectOption option}}> <tr {{bind-attr class=checked:active}} {{action selectOption option}}>
<td class="radio"><input type="radio" name="poll" {{bind-attr checked=checked disabled=controller.loading}}></td> <td class="radio"><input type="radio" name="poll" {{bind-attr checked=checked disabled=controller.disableRadio}}></td>
<td class="option"> <td class="option">
<div class="option"> <div class="option">
{{{ option }}} {{{ option }}}

View File

@ -38,8 +38,14 @@ var PollController = Discourse.Controller.extend({
poll: null, poll: null,
showResults: false, showResults: false,
disableRadio: Em.computed.any('poll.post.topic.closed', 'loading'),
actions: { actions: {
selectOption: function(option) { selectOption: function(option) {
if (this.get('disableRadio')) {
return;
}
if (!this.get('currentUser.id')) { if (!this.get('currentUser.id')) {
this.get('postController').send('showLogin'); this.get('postController').send('showLogin');
return; return;

View File

@ -124,6 +124,8 @@ module ::PollPlugin
end end
def set_vote!(user, option) def set_vote!(user, option)
return if @post.topic.closed?
# Get the user's current vote. # Get the user's current vote.
vote = get_vote(user) vote = get_vote(user)
vote = nil unless details.keys.include? vote vote = nil unless details.keys.include? vote

View File

@ -50,6 +50,14 @@ describe PollPlugin::Poll do
poll.details["Onodera"].should eq(1) poll.details["Onodera"].should eq(1)
end end
it "should not set votes on closed polls" do
poll.set_vote!(user, "Onodera")
post.topic.closed = true
post.topic.save!
poll.set_vote!(user, "Chitoge")
poll.get_vote(user).should eq("Onodera")
end
it "should serialize correctly" do it "should serialize correctly" do
poll.serialize(user).should eq({options: poll.details, selected: nil}) poll.serialize(user).should eq({options: poll.details, selected: nil})
poll.set_vote!(user, "Onodera") poll.set_vote!(user, "Onodera")