discourse/lib/topics_bulk_action.rb

49 lines
945 B
Ruby
Raw Normal View History

class TopicsBulkAction
def initialize(user, topic_ids, operation)
@user = user
@topic_ids = topic_ids
@operation = operation
2014-01-30 12:44:28 -05:00
@changed_ids = []
end
def self.operations
2014-01-30 12:44:28 -05:00
%w(change_category close)
end
def perform!
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
send(@operation[:type])
2014-01-30 12:44:28 -05:00
@changed_ids
end
private
def change_category
topics.each do |t|
if guardian.can_edit?(t)
2014-01-30 12:44:28 -05:00
@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
def guardian
@guardian ||= Guardian.new(@user)
end
def topics
@topics ||= Topic.where(id: @topic_ids)
end
end