Bulk close operation

This commit is contained in:
Robin Ward 2014-01-30 12:44:28 -05:00
parent e9664d5cfa
commit 6f23870327
6 changed files with 66 additions and 13 deletions

View File

@ -9,7 +9,7 @@
**/ **/
Discourse.TopicBulkActionsController = Ember.ArrayController.extend(Discourse.ModalFunctionality, { Discourse.TopicBulkActionsController = Ember.ArrayController.extend(Discourse.ModalFunctionality, {
onShow: function() { onShow: function() {
this.set('controllers.modal.modalClass', 'topic-bulk-actions-modal'); this.set('controllers.modal.modalClass', 'topic-bulk-actions-modal small');
}, },
perform: function(operation) { perform: function(operation) {
@ -30,9 +30,26 @@ Discourse.TopicBulkActionsController = Ember.ArrayController.extend(Discourse.Mo
}); });
}, },
forEachPerformed: function(operation, cb) {
var self = this;
this.perform(operation).then(function (topics) {
if (topics) {
topics.forEach(cb);
self.send('closeModal');
}
});
},
actions: { actions: {
showChangeCategory: function() { showChangeCategory: function() {
this.send('changeBulkTemplate', 'modal/bulk_change_category'); this.send('changeBulkTemplate', 'modal/bulk_change_category');
this.set('controllers.modal.modalClass', 'topic-bulk-actions-modal full');
},
closeTopics: function() {
this.forEachPerformed({type: 'close'}, function(t) {
t.set('closed', true);
});
}, },
changeCategory: function() { changeCategory: function() {

View File

@ -1 +1,2 @@
<button class='btn' {{action showChangeCategory}}>{{i18n topics.bulk.change_category}}</button> <button class='btn' {{action showChangeCategory}}>{{i18n topics.bulk.change_category}}</button>
<button class='btn' {{action closeTopics}}>{{i18n topics.bulk.close_topics}}</button>

View File

@ -251,9 +251,9 @@
p { p {
margin-top: 0; margin-top: 0;
} }
.modal-body { &.full .modal-body {
height: 420px; height: 400px;
max-height: 420px; max-height: 400px;
} }
} }
.tabbed-modal { .tabbed-modal {

View File

@ -604,6 +604,7 @@ en:
toggle: "toggle bulk selection of topics" toggle: "toggle bulk selection of topics"
actions: "Bulk Actions" actions: "Bulk Actions"
change_category: "Change Category" change_category: "Change Category"
close_topics: "Close Topics"
selected: selected:
one: "You have selected <b>1</b> topic." one: "You have selected <b>1</b> topic."
other: "You have selected <b>{{count}}</b> topics." other: "You have selected <b>{{count}}</b> topics."

View File

@ -4,27 +4,36 @@ class TopicsBulkAction
@user = user @user = user
@topic_ids = topic_ids @topic_ids = topic_ids
@operation = operation @operation = operation
@changed_ids = []
end end
def self.operations def self.operations
%w(change_category) %w(change_category close)
end end
def perform! def perform!
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type]) raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
send(@operation[:type]) send(@operation[:type])
@changed_ids
end end
private private
def change_category def change_category
changed_ids = []
topics.each do |t| topics.each do |t|
if guardian.can_edit?(t) if guardian.can_edit?(t)
changed_ids << t.id if t.change_category(@operation[:category_name]) @changed_ids << t.id if t.change_category(@operation[:category_name])
end
end
end
def close
topics.each do |t|
if guardian.can_moderate?(t)
t.update_status('closed', true, @user)
@changed_ids << t.id
end end
end end
changed_ids
end end
def guardian def guardian

View File

@ -1,10 +1,10 @@
require 'spec_helper' require 'spec_helper'
require 'topics_bulk_action' require_dependency 'topics_bulk_action'
describe TopicsBulkAction do describe TopicsBulkAction do
describe "invalid operation" do describe "invalid operation" do
let(:user) { Fabricate.build(:user) } let(:user) { Fabricate.build(:user) }
it "raises an error with an invalid operation" do it "raises an error with an invalid operation" do
tba = TopicsBulkAction.new(user, [1], type: 'rm_root') tba = TopicsBulkAction.new(user, [1], type: 'rm_root')
@ -14,7 +14,7 @@ describe TopicsBulkAction do
describe "change_category" do describe "change_category" do
let(:topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
let(:category) { Fabricate(:category) } let(:category) { Fabricate(:category) }
context "when the user can edit the topic" do context "when the user can edit the topic" do
it "changes the category and returns the topic_id" do it "changes the category and returns the topic_id" do
@ -27,7 +27,7 @@ describe TopicsBulkAction do
end end
context "when the user can't edit the topic" do context "when the user can't edit the topic" do
it "doesn't change the category and returns the topic_id" do it "doesn't change the category" do
Guardian.any_instance.expects(:can_edit?).returns(false) Guardian.any_instance.expects(:can_edit?).returns(false)
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_name: category.name) tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_name: category.name)
topic_ids = tba.perform! topic_ids = tba.perform!
@ -36,7 +36,32 @@ describe TopicsBulkAction do
topic.category.should_not == category topic.category.should_not == category
end end
end end
end
describe "close" do
let(:topic) { Fabricate(:topic) }
context "when the user can moderate the topic" do
it "closes the topic and returns the topic_id" do
Guardian.any_instance.expects(:can_moderate?).returns(true)
Guardian.any_instance.expects(:can_create?).returns(true)
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'close')
topic_ids = tba.perform!
topic_ids.should == [topic.id]
topic.reload
topic.should be_closed
end
end
context "when the user can't edit the topic" do
it "doesn't close the topic" do
Guardian.any_instance.expects(:can_moderate?).returns(false)
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'close')
topic_ids = tba.perform!
topic_ids.should be_blank
topic.reload
topic.should_not be_closed
end
end
end end
end end