Skip to content

Instantly share code, notes, and snippets.

@cjcolvar
Last active December 18, 2015 02:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cjcolvar/5708461 to your computer and use it in GitHub Desktop.
Save cjcolvar/5708461 to your computer and use it in GitHub Desktop.
Batch delete example working with cancan's load_and_authorize_resource
class RolesController < ApplicationController
include Hydra::RoleManagement::RolesBehavior
prepend_before_filter :accept_batches, only: [:destroy]
def destroy
@roles.each {|role| role.destroy}
redirect_to roles_path, notice: "Successfully deleted groups: #{params[:ids].join(", ")}"
end
def accept_batches
if params[:id]
params[:ids] = [params[:id]].flatten
params[:id] = nil
end
params[:ids] ||= []
#This implementation of batch works based upon cancan's returning of all roles when no params[:id] is supplied
#Thus authorize_resource is checking can? :delete, Role instead of can? :delete, 1, can? :delete, 2, etc.
#Or is cancan able to check @roles instead?!?
@roles = Role.where(id: params[:ids])
end
end
resources :roles do
collection do
delete :destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment