Sha256: 58aa5909f549cbca1ef79e49ce6ad8579229958143123ac1cdb76ec29fed4d17

Contents?: true

Size: 1.96 KB

Versions: 30

Compression:

Stored size: 1.96 KB

Contents

module CmAdmin
  class BulkActionProcessor
    extend ActiveSupport::Concern
    attr_accessor :invalid_records, :success_records

    def initialize(current_action, model, params)
      @invalid_records = []
      @success_records = []
      @current_action = current_action
      @model = model
      @params = params
    end

    def perform_individual_action
      @params[:selected_ids].split(',').each do |id|
        ar_object = @model.ar_model.find(id)
        column_name = @model.available_fields[:index].first.field_name
        begin
          @current_action.code_block.call(id)
          @error_message = nil
        rescue NoMethodError, NameError => e
          @error_message = "#{e.message.slice(0..(e.message.index(' for')))} at #{ar_object.send(column_name)}"
        rescue ActiveRecord::RecordInvalid => e
          @error_message = "#{e.message} at #{ar_object.send(column_name)}"
        rescue StandardError => e
          @error_message = "#{ar_object.send(column_name)} - #{e.message}"
        end
        @invalid_records << OpenStruct.new({ row_identifier: ar_object.send(column_name), error_message: @error_message }) if @error_message
        @success_records << OpenStruct.new({ row_identifier: ar_object.send(column_name) }) unless @error_message
      end
      self
    end

    def perform_bulk_action
      ar_object = @model.ar_model.where(id: @params[:selected_ids]).first
      column_name = @model.available_fields[:index].first.field_name
      begin
        @current_action.code_block.call(@params[:selected_ids])
        @error_message = nil
      rescue NoMethodError, NameError => e
        @error_message = e.message.to_s
      rescue ActiveRecord::RecordInvalid => e
        @error_message = e.message.to_s
      rescue StandardError => e
        @error_message = e.message.to_s
      end
      @invalid_records << OpenStruct.new({ row_identifier: ar_object.send(column_name), error_message: @error_message }) if @error_message
      self
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
cm-admin-4.4.8 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.4.6 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.4.1 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.4.0 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.3.0 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.2.1 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.2.0 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.1.2 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.1.1 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.1.0 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-4.0.0 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.16 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.15 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.14 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.13 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.12 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.11 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.10 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.9 app/models/concerns/cm_admin/bulk_action_processor.rb
cm-admin-3.0.8 app/models/concerns/cm_admin/bulk_action_processor.rb