module BatchActions::ClassMethods

Public Instance Methods

batch_action(keyword, opts = {}, &block) click to toggle source
# File lib/batch_actions/class_methods.rb, line 7
def batch_action(keyword, opts = {}, &block)
  @batch_actions = {} if @batch_actions.nil?

  if opts.include? :model
    model = opts[:model]
  elsif !@batch_model.nil?
    model = @batch_model
  else
    raise ArgumentError, "model must be specified"
  end

  if block_given?
    apply = block
  else
    raise ArgumentError, "block must be specified"
  end

  if opts.include? :scope
    scope = opts[:scope]
  else
    scope = ->(ids) do
      model.where(:id => ids)
    end
  end

  if opts.include? :if
    condition = opts[:if]
  else
    condition = ->() { true }
  end

  @batch_actions[keyword] = condition

  raise ArgumentError, "unexpected number of arguments for scope" if scope.arity < 1 || scope.arity > 2

  define_method(:"batch_#{keyword}") do
    result = instance_exec(&condition)

    raise ActionController::RoutingError.new('batch action is not allowed') unless result

    case scope.arity
    when 1
      objects = instance_exec(params[:ids], &scope)

    when 2
      objects = instance_exec(params[:ids], model.where(:id => params[:ids]), &scope)
      
    end

    instance_exec(objects, &apply)
  end
end
batch_model(klass) click to toggle source
# File lib/batch_actions/class_methods.rb, line 3
def batch_model(klass)
  @batch_model = klass
end