Delegate bulk operations to a `TopicsBulkAction` object.

This commit is contained in:
Robin Ward 2014-01-30 11:15:49 -05:00
parent 0c73eb8ce1
commit b315a5c28f
3 changed files with 50 additions and 1 deletions

View File

@ -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

14
lib/topics_bulk_action.rb Normal file
View File

@ -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

View File

@ -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