From afdbb2bb9612c3f92e687292642cfd12aa3eb558 Mon Sep 17 00:00:00 2001 From: cpradio Date: Mon, 22 Sep 2014 14:56:48 -0400 Subject: [PATCH] FEATURE: Add Archive Topics to Bulk actions Add the ability to archive topics in bulk https://meta.discourse.org/t/archive-topics-via-bulk/20302 --- .../controllers/topic-bulk-actions.js.es6 | 6 +++++ .../modal/bulk_actions_buttons.js.handlebars | 15 +++++++---- config/locales/client.en.yml | 1 + lib/topics_bulk_action.rb | 11 +++++++- spec/components/topics_bulk_action_spec.rb | 27 +++++++++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic-bulk-actions.js.es6 b/app/assets/javascripts/discourse/controllers/topic-bulk-actions.js.es6 index efc1e58fc5a..06e4276bace 100644 --- a/app/assets/javascripts/discourse/controllers/topic-bulk-actions.js.es6 +++ b/app/assets/javascripts/discourse/controllers/topic-bulk-actions.js.es6 @@ -73,6 +73,12 @@ export default Ember.ArrayController.extend(ModalFunctionality, { }); }, + archiveTopics: function() { + this.forEachPerformed({type: 'archive'}, function(t) { + t.set('archived', true); + }); + }, + changeCategory: function() { var categoryId = parseInt(this.get('newCategoryId'), 10) || 0, category = Discourse.Category.findById(categoryId), diff --git a/app/assets/javascripts/discourse/templates/modal/bulk_actions_buttons.js.handlebars b/app/assets/javascripts/discourse/templates/modal/bulk_actions_buttons.js.handlebars index 4ef4c1cdd10..2a3d9fd9e76 100644 --- a/app/assets/javascripts/discourse/templates/modal/bulk_actions_buttons.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/bulk_actions_buttons.js.handlebars @@ -1,5 +1,10 @@ - - - - - +

+ + + + +

+

+ + +

\ No newline at end of file diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 165f3b2766a..3e137ce0253 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -771,6 +771,7 @@ en: actions: "Bulk Actions" change_category: "Change Category" close_topics: "Close Topics" + archive_topics: "Archive Topics" notification_level: "Change Notification Level" selected: one: "You have selected 1 topic." diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb index 74454dc4d44..fbdea220572 100644 --- a/lib/topics_bulk_action.rb +++ b/lib/topics_bulk_action.rb @@ -8,7 +8,7 @@ class TopicsBulkAction end def self.operations - %w(change_category close change_notification_level reset_read dismiss_posts delete) + %w(change_category close archive change_notification_level reset_read dismiss_posts delete) end def perform! @@ -61,6 +61,15 @@ class TopicsBulkAction end end + def archive + topics.each do |t| + if guardian.can_moderate?(t) + t.update_status('archived', true, @user) + @changed_ids << t.id + end + end + end + def delete topics.each do |t| t.trash! if guardian.can_delete?(t) diff --git a/spec/components/topics_bulk_action_spec.rb b/spec/components/topics_bulk_action_spec.rb index 2bcfb02c1e7..325c6e402ff 100644 --- a/spec/components/topics_bulk_action_spec.rb +++ b/spec/components/topics_bulk_action_spec.rb @@ -122,4 +122,31 @@ describe TopicsBulkAction do end end end + + describe "archive" do + let(:topic) { Fabricate(:topic) } + + context "when the user can moderate the topic" do + it "archives 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: 'archive') + topic_ids = tba.perform! + topic_ids.should == [topic.id] + topic.reload + topic.should be_archived + end + end + + context "when the user can't edit the topic" do + it "doesn't archive the topic" do + Guardian.any_instance.expects(:can_moderate?).returns(false) + tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'archive') + topic_ids = tba.perform! + topic_ids.should be_blank + topic.reload + topic.should_not be_archived + end + end + end end