This gem adds generic support for batch actions to Rails controllers and InheritedResources.
Sponsored by Evil Martians.
Add this line to your application's Gemfile:
gem 'batch_actions'
And then execute:
$ bundle
Or install it yourself as:
$ gem install batch_actions
class PostController < ApplicationController
include BatchActions
batch_actions do
model Post
# Produces #batch_publish action. Requires params[:ids] to get affected
# instances and call #publish on them.
batch_action :publish
# params[:batch_destroy] should be an array containing affected ids
batch_action :destroy, param_name: :batch_destroy
# Produced controller action will be called #mass_unpublish (instead of
# batch_unpublish by default). Method #draft! will be called for each
# affected instance.
batch_action :unpublish, action_name: :mass_unpublish, batch_method: :draft!
# Affected objects will be got inside #destroyed scope, redirection will
# be done to params[:return_to] instead of url_for(action: :index)
batch_action :restore,
scope: ->(model, ids) { Post.destroyed.where(id: ids) },
respose: -> {
respond_to do |format|
format.html { redirect_to params[:return_to] }
end
}
# Produces action #do_batch with dispatches request to concrete
# actions. Concrete action is determined by param named as action name
# ("destroy=true" for batch_action :destroy) or by :trigger option value
# ("call_destroy=true" for batch_action :destroy, trigger: :call_destroy).
dispatch_action(:do_batch)
end
end
Batch action options and batch actions could be inherited.
class Admin::BaseController < ApplicationController
include BatchActions
batch_actions do
param_name :ids_eq
end
end
class Admin::NewsController < Admin::BaseController
# You can omit #batch_actions call if you do not want to set options.
batch_action :destroy
batch_action :publish
end
#batch_destroy
and #batch_publish
will require params[:ids_eq]
to work.
Because of every batch_action creates action called batch_#{name}
, you can
control access rights with CanCan. Action name could be overriden with
:action_name
param.
Note that you can omit model
call if you use the inherited_resources gem. It grabs scope from resource_class
and scope from end_of_association_chain
.
- call before and after filters for producted actions if they are called from dispatcher.
- implement flash messages with inherited_resources responders for example.
- autoinclude it to actioncontroller inside railtie.
- :tirgger param must be a proc.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request