Sha256: 0b5e4fdd6a26e2b176978957bed6b64c60e6a48df8179f93abf27206fe86df33

Contents?: true

Size: 1.89 KB

Versions: 61

Compression:

Stored size: 1.89 KB

Contents

require_dependency "avo/application_controller"

module Avo
  class ActionsController < ApplicationController
    before_action :set_resource_name
    before_action :set_resource
    before_action :set_action, only: [:show, :handle]

    def show
      @model = ActionModel.new @action.get_attributes_for_action
    end

    def handle
      resource_ids = action_params[:fields][:resource_ids].split(",").map(&:to_i)
      models = @resource.model_class.find resource_ids

      fields = action_params[:fields].select do |key, value|
        key != "resource_ids"
      end

      performed_action = @action.handle_action(models: models, fields: fields)

      respond performed_action.response
    end

    private

    def action_params
      params.permit(:resource_name, :action_id, fields: {})
    end

    def set_action
      action_class = params[:action_id].gsub("avo_actions_", "").classify.safe_constantize

      if params[:id].present?
        model = @resource.model_class.find params[:id]
      end

      @action = action_class.new(model: model, resource: resource, user: _current_user)
    end

    def respond(response)
      response[:type] ||= :reload
      response[:message_type] ||= :notice
      response[:message] ||= I18n.t("avo.action_ran_successfully")

      if response[:type] == :download
        return send_data response[:path], filename: response[:filename]
      end

      respond_to do |format|
        format.html do
          if response[:type] == :redirect
            path = response[:path]

            if path.respond_to? :call
              path = instance_eval(&path)
            end

            redirect_to path, "#{response[:message_type]}": response[:message]
          elsif response[:type] == :reload
            redirect_back fallback_location: resources_path(@resource.model_class), "#{response[:message_type]}": response[:message]
          end
        end
      end
    end
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
avo-1.7.3 app/controllers/avo/actions_controller.rb
avo-1.7.3.pre.1 app/controllers/avo/actions_controller.rb
avo-1.7.2 app/controllers/avo/actions_controller.rb
avo-1.7.1 app/controllers/avo/actions_controller.rb
avo-1.7.0 app/controllers/avo/actions_controller.rb
avo-1.6.4.pre.1 app/controllers/avo/actions_controller.rb
avo-1.6.3.pre.3 app/controllers/avo/actions_controller.rb
avo-1.6.3.pre.2 app/controllers/avo/actions_controller.rb
avo-1.6.3.pre.1 app/controllers/avo/actions_controller.rb
avo-1.6.2.pre.1 app/controllers/avo/actions_controller.rb
avo-1.6.1 app/controllers/avo/actions_controller.rb
avo-1.6.0 app/controllers/avo/actions_controller.rb
avo-1.5.5 app/controllers/avo/actions_controller.rb
avo-1.5.4 app/controllers/avo/actions_controller.rb
avo-1.5.3 app/controllers/avo/actions_controller.rb
avo-1.5.2 app/controllers/avo/actions_controller.rb
avo-1.5.1 app/controllers/avo/actions_controller.rb
avo-1.5.0 app/controllers/avo/actions_controller.rb
avo-1.4.5.pre.1 app/controllers/avo/actions_controller.rb
avo-1.4.4.pre.1 app/controllers/avo/actions_controller.rb