From b315a5c28f0d4780c7433677e85d5011d8c38f5f Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 30 Jan 2014 11:15:49 -0500 Subject: [PATCH] Delegate bulk operations to a `TopicsBulkAction` object. --- app/controllers/topics_controller.rb | 7 ++++- lib/topics_bulk_action.rb | 14 ++++++++++ spec/controllers/topics_controller_spec.rb | 30 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/topics_bulk_action.rb diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index f2dec37ced3..480a3bf8b7b 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -1,6 +1,7 @@ require_dependency 'topic_view' require_dependency 'promotion' require_dependency 'url_helper' +require_dependency 'topics_bulk_action' class TopicsController < ApplicationController include UrlHelper @@ -269,7 +270,11 @@ class TopicsController < ApplicationController def bulk topic_ids = params.require(:topic_ids).map {|t| t.to_i} - render_json_dump topic_ids: topic_ids + operation = params.require(:operation).symbolize_keys + raise ActionController::ParameterMissing.new(:operation_type) if operation[:type].blank? + operator = TopicsBulkAction.new(current_user, topic_ids, operation) + changed_topic_ids = operator.perform! + render_json_dump topic_ids: changed_topic_ids end private diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb new file mode 100644 index 00000000000..f0e0dc91c7e --- /dev/null +++ b/lib/topics_bulk_action.rb @@ -0,0 +1,14 @@ +class TopicsBulkAction + + def initialize(user, topic_ids, operation) + @user = user + @topic_ids = topic_ids + @operation = operation + end + + def perform! + [] + end + +end + diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 5e666724501..a6eaedd8a16 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -781,4 +781,34 @@ describe TopicsController do end + describe "bulk" do + it 'needs you to be logged in' do + lambda { xhr :put, :bulk }.should raise_error(Discourse::NotLoggedIn) + end + + describe "when logged in" do + let!(:user) { log_in } + let(:operation) { {type: 'change_category', category_id: '1'} } + let(:topic_ids) { [1,2,3] } + + it "requires a list of topic_ids" do + lambda { xhr :put, :bulk, operation: operation }.should raise_error(ActionController::ParameterMissing) + end + + it "requires an operation param" do + lambda { xhr :put, :bulk, topic_ids: topic_ids}.should raise_error(ActionController::ParameterMissing) + end + + it "requires a type field for the operation param" do + lambda { xhr :put, :bulk, topic_ids: topic_ids, operation: {}}.should raise_error(ActionController::ParameterMissing) + end + + it "delegates work to `TopicsBulkAction`" do + topics_bulk_action = mock + TopicsBulkAction.expects(:new).with(user, topic_ids, operation).returns(topics_bulk_action) + topics_bulk_action.expects(:perform!) + xhr :put, :bulk, topic_ids: topic_ids, operation: operation + end + end + end end