Files
danbooru/app/controllers/bulk_update_requests_controller.rb
evazion b4ce2d83a6 models: remove belongs_to_creator macro.
The belongs_to_creator macro was used to initialize the creator_id field
to the CurrentUser. This made tests complicated because it meant you had
to create and set the current user every time you wanted to create an
object, when lead to the current user being set over and over again. It
also meant you had to constantly be aware of what the CurrentUser was in
many different contexts, which was often confusing. Setting creators
explicitly simplifies everything greatly.
2020-01-21 00:09:38 -06:00

63 lines
2.1 KiB
Ruby

class BulkUpdateRequestsController < ApplicationController
respond_to :html, :xml, :json, :js
before_action :member_only, :except => [:index, :show]
before_action :admin_only, :only => [:approve]
before_action :load_bulk_update_request, :except => [:new, :create, :index]
def new
@bulk_update_request = BulkUpdateRequest.new(bur_params(:create))
respond_with(@bulk_update_request)
end
def create
@bulk_update_request = BulkUpdateRequest.create(bur_params(:create).merge(user: CurrentUser.user))
respond_with(@bulk_update_request, :location => bulk_update_requests_path)
end
def show
@current_item = @bulk_update_request = BulkUpdateRequest.find(params[:id])
respond_with(@bulk_update_request)
end
def edit
end
def update
raise User::PrivilegeError unless @bulk_update_request.editable?(CurrentUser.user)
@bulk_update_request.update(bur_params(:update))
respond_with(@bulk_update_request, location: bulk_update_requests_path, notice: "Bulk update request updated")
end
def approve
@bulk_update_request.approve!(CurrentUser.user)
respond_with(@bulk_update_request, :location => bulk_update_requests_path)
end
def destroy
raise User::PrivilegeError unless @bulk_update_request.rejectable?(CurrentUser.user)
@bulk_update_request.reject!(CurrentUser.user)
respond_with(@bulk_update_request, location: bulk_update_requests_path, notice: "Bulk update request rejected")
end
def index
@bulk_update_requests = BulkUpdateRequest.includes(:user, :approver, :forum_topic, forum_post: [:votes]).paginated_search(params, count_pages: true)
respond_with(@bulk_update_requests)
end
private
def load_bulk_update_request
@bulk_update_request = BulkUpdateRequest.find(params[:id])
end
def bur_params(context)
permitted_params = %i[script skip_secondary_validations]
permitted_params += %i[title reason forum_topic_id] if context == :create
permitted_params += %i[forum_topic_id forum_post_id] if context == :update && CurrentUser.is_admin?
params.fetch(:bulk_update_request, {}).permit(permitted_params)
end
end