GithubHelp home page GithubHelp logo

batch_actions's Introduction

BatchActions

This gem adds generic support for batch actions to Rails controllers and InheritedResources.

Travis CI Code Climate

Sponsored by Evil Martians.

Installation

Add this line to your application's Gemfile:

gem 'batch_actions'

And then execute:

$ bundle

Or install it yourself as:

$ gem install batch_actions

Usage

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

Inheritance

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.

CanCan

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.

InheritedResources

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.

TODO

  1. call before and after filters for producted actions if they are called from dispatcher.
  2. implement flash messages with inherited_resources responders for example.
  3. autoinclude it to actioncontroller inside railtie.
  4. :tirgger param must be a proc.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

batch_actions's People

Contributors

gzigzigzeo avatar grindars avatar inossidabile avatar aphilippi avatar vladvinnikov avatar

Watchers

 avatar James Cloos avatar Victor Tomilin avatar Serhii Herniak avatar Stanislav Gernyak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.