Sha256: 7a55450a92fcb523a936cf4d512a27273943c77564c3ac3e64731f5e1c91a9f4

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 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

2 entries across 2 versions & 1 rubygems

Version Path
avo-0.5.0.beta9 app/controllers/avo/actions_controller.rb
avo-0.5.0.beta8 app/controllers/avo/actions_controller.rb