This gem adds generic support for batch actions to Rails controllers.
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_model Post
# Runs `model.publish` for every model from params[:ids]
batch_action :publish
# Runs `model.destroy` for every model from params[:ids] or throws exception unless you can
batch_action :destroy, if: ->() { can? :destroy, Post }
# Runs block for every model from params[:ids]
batch_action :specific do |objects|
objects.each{|x| x.specific!}
end
# Runs `model.resurrect` for every model from returned relation
batch_action :resurrect, :scope => ->(ids) { Post.where(other_ids: ids) }
end
Note that you can omit batch_model
call if you use the inherited_resources gem. It grabs your model class from resource_class
.
There's one more important thing to know: set of active batch actions can be retrieved from controller by calling batch_actions
on controller instance.
- 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