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, {
onShow: function() {
this.set('controllers.modal.modalClass', 'topic-bulk-actions-modal');
this.set('controllers.modal.modalClass', 'topic-bulk-actions-modal small');
},
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: {
showChangeCategory: function() {
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() {

View File

@ -1 +1,2 @@
<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 {
margin-top: 0;
}
.modal-body {
height: 420px;
max-height: 420px;
&.full .modal-body {
height: 400px;
max-height: 400px;
}
}
.tabbed-modal {

View File

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

View File

@ -4,27 +4,36 @@ class TopicsBulkAction
@user = user
@topic_ids = topic_ids
@operation = operation
@changed_ids = []
end
def self.operations
%w(change_category)
%w(change_category close)
end
def perform!
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
send(@operation[:type])
@changed_ids
end
private
def change_category
changed_ids = []
topics.each do |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
changed_ids
end
def guardian

View File

@ -1,10 +1,10 @@
require 'spec_helper'
require 'topics_bulk_action'
require_dependency 'topics_bulk_action'
describe TopicsBulkAction do
describe "invalid operation" do
let(:user) { Fabricate.build(:user) }
let(:user) { Fabricate.build(:user) }
it "raises an error with an invalid operation" do
tba = TopicsBulkAction.new(user, [1], type: 'rm_root')
@ -14,7 +14,7 @@ describe TopicsBulkAction do
describe "change_category" do
let(:topic) { Fabricate(:topic) }
let(:category) { Fabricate(:category) }
let(:category) { Fabricate(:category) }
context "when the user can edit the topic" do
it "changes the category and returns the topic_id" do
@ -27,7 +27,7 @@ describe TopicsBulkAction do
end
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)
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_name: category.name)
topic_ids = tba.perform!
@ -36,7 +36,32 @@ describe TopicsBulkAction do
topic.category.should_not == category
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