This plugin enables batch actions, allowing users to create, update, or delete multiple records simultaneously. It is intended for administrative workflows where the affected scope is explicit and reviewable.

Requirements

  • Active Scaffold 4.4.0 or newer for ActiveScaffold Batch 3.8
  • A Ruby and Rails version supported by the selected Active Scaffold version

Description

ActiveScaffold Batch provides three independent actions: batch_create, batch_update, and batch_destroy. Enable only the actions appropriate for a controller.

Installation

Add the following line to your Gemfile:

gem 'active_scaffold_batch'

Then run:

bundle install

Usage & Options

It provides three actions: batch_create, batch_update, and batch_destroy. You can enable batch processing globally:

ActiveScaffold.set_defaults do |config|
  config.actions << :batch_update
end

Or adding them in your controller:

class UsersController < ApplicationController
  active_scaffold :user do |config|
    config.actions.add :batch_update
    config.actions.add :batch_destroy
  end
end

Batch Update and Batch Destroy can target every record in the current filtered list. If the scaffold also enables the mark action, they can instead target only marked records.

Batch Create

Batch Create has two modes. Without default_batch_by_column, it displays a multiple-row creation form. The user starts with one blank record and can add more rows before submitting them together:

config.actions.add :batch_create
config.batch_create.layout = :vertical # or :horizontal

When default_batch_by_column is configured, the form creates one record for every selected value of that column while sharing the other entered attributes. For example, this creates one player for each selected team:

config.actions.add :batch_create
config.batch_create.default_batch_by_column = :team

The batch column may be a belongs_to association or a scalar column. Scalar values are entered one per line. By default, all records are validated and created in one transaction; if one fails, none are saved.

config.batch_create.run_in_transaction = true
config.batch_create.list_mode_enabled = true

Set run_in_transaction to false to retain successful rows when another fails, or to :validate_first to validate all rows before saving any of them.

Batch Update

Each editable column has an operator. NO_UPDATE leaves it untouched and REPLACE assigns the entered value. Numeric columns also support addition, subtraction, multiplication, and division using absolute or percentage operands; date columns support addition and subtraction.

config.actions.add :batch_update
config.batch_update.process_mode = :update
config.batch_update.list_mode_enabled = true

The default :update mode processes each record with authorization, validations, and callbacks. The optional :update_all mode performs direct SQL updates and skips model callbacks and validations.

Batch Destroy

config.actions.add :batch_destroy
config.batch_destroy.process_mode = :delete

The default :delete mode processes records individually. The optional :delete_all mode uses direct SQL and skips model callbacks. List-scoped actions affect the complete filtered result, not only the visible page, so marked-record scope is safer when users should select every affected record explicitly.

UI adapter support

ActiveScaffold Batch 3.8 reuses ActiveScaffold 4.4’s standard form elements. See ActiveScaffold’s complete UI element reference for form, form_title, fields_container, form_element, form_attribute, form_control, form footer controls, and the other shared elements.

Batch defines only one plugin-specific element:

Element Type Used for
batch_create_add_rows Attributes only Remote link that adds blank rows to a multiple-row Batch Create form.

For example, an adapter can customize the Batch-specific add-row link without replacing its template:

ActiveScaffold.add_element_attributes :batch_create_add_rows,
                                      class: "btn btn-sm btn-secondary"

Adapter attributes are merged with the remote-link behavior required by the plugin.

Batch-specific inputs use the shared form_control element. Its proc receives the usual column and form_ui context plus batch_role, which identifies batch_scope, batch_update_operator, batch_update_value, batch_update_numeric_option, batch_update_date_amount, batch_update_date_unit, or batch_create_row_count. This lets an adapter distinguish these controls when necessary without adding a separate registry element for each one.

The shared elements use the same definitions as ordinary ActiveScaffold forms, keeping core and Batch forms consistent.

Custom batch actions

If you want to allow bulk activation of users, you can define a custom action:

class UsersController < ApplicationController
  include ActiveScaffold::Actions::BatchBase

  active_scaffold :user do |config|
    config.action_links.collection.add :batch_activate, label: 'Activate Users',
      confirm: 'Are you sure you want to activate the selected users?',
      parameters: {batch_scope: 'marked'}
  end

  def batch_activate
    batch_action
  end

  protected

  def batch_activate_marked
    activated = 0
    each_marked_record do |record|
      if record.update(active: true)
        record.as_marked = false
        activated += 1
      else
        error_records << record
      end
    end
  end
end

Get Plugin