Sha256: 19f35a3a651d3f77155b2f9cda4cd1fc315074b631616cef24ae5ed041a1bbdc

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module BatchActions
  module ClassMethods
    def batch_model(klass)
      @batch_model = klass
    end

    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
        apply = ->(objects) do
          objects.each do |object|
            object.send(keyword)
          end
        end
      end

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

      if opts.include? :if
        condition = opts[:if]
      else
        condition = ->() do
          if self.respond_to? :can?
            can? keyword, model
          else
            true
          end
        end
      end

      @batch_actions[keyword] = condition

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

        raise "action is not allowed" unless result

        objects = instance_exec(model, &scope)
        apply.call(objects)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
batch_actions-0.0.1 lib/batch_actions/class_methods.rb