app/controllers/avo/actions_controller.rb

Summary

Maintainability
A
1 hr
Test Coverage
require_dependency "avo/application_controller"

module Avo
  class ActionsController < ApplicationController
    before_action :set_resource_name
    before_action :set_resource
    before_action :set_record, only: :show, if: ->(request) do
      # Try to se the record only if the user is on the record page.
      # set_record will fail if it's tried to be used from the Index page.
      request.params[:id].present?
    end
    before_action :set_action, only: [:show, :handle]
    before_action :verify_authorization, only: [:show, :handle]

    layout :choose_layout

    def show
      # Se the view to :new so the default value gets prefilled
      @view = Avo::ViewInquirer.new("new")

      @resource.hydrate(record: @record, view: @view, user: _current_user, params: params)
      @fields = @action.get_fields

      build_background_url
    end

    def build_background_url
      uri = URI.parse(request.url)

      # Remove the "/actions" segment from the path
      path_without_actions = uri.path.sub("/actions", "")

      params = URI.decode_www_form(uri.query || "").to_h

      params.delete("action_id")
      params[:turbo_frame] = ACTIONS_BACKGROUND_FRAME

      # Reconstruct the query string
      new_query_string = URI.encode_www_form(params)

      # Update the URI components
      uri.path = path_without_actions
      uri.query = (new_query_string == "") ? nil : new_query_string

      # Reconstruct the modified URL
      @background_url = uri.to_s
    end

    def handle
      resource_ids = action_params[:fields][:avo_resource_ids].split(",")

      performed_action = @action.handle_action(
        fields: action_params[:fields].except(:avo_resource_ids, :avo_selected_query),
        current_user: _current_user,
        resource: @resource,
        query: decrypted_query ||
          (resource_ids.any? ? @resource.find_record(resource_ids, params: params) : [])
      )

      @response = performed_action.response
      respond
    end

    private

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

    def set_action
      @action = action_class.new(
        record: @record,
        resource: @resource,
        user: _current_user,
        view: :new, # force the action view to in order to render new-related fields (hidden field)
        arguments: BaseAction.decode_arguments(params[:arguments] || params.dig(:fields, :arguments)) || {}
      )
    end

    def action_class
      Avo::BaseAction.descendants.find do |action|
        action.to_s == params[:action_id]
      end
    end

    def respond
      # Flash the messages collected from the action
      flash_messages

      respond_to do |format|
        format.turbo_stream do
          case @response[:type]
          when :keep_modal_open
            # Only render the flash messages if the action keeps the modal open
            render turbo_stream: turbo_stream.flash_alerts
          when :download
            # Trigger download, removes modal and flash the messages
            render turbo_stream: [
              turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
              turbo_stream.close_action_modal,
              turbo_stream.flash_alerts
            ]
          when :navigate_to_action
            frame_id = Avo::ACTIONS_TURBO_FRAME_ID
            src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])

            render turbo_stream: turbo_stream.turbo_frame_set_src(frame_id, src)
          when :redirect
            render turbo_stream: turbo_stream.redirect_to(
              Avo::ExecutionContext.new(target: @response[:path]).handle,
              turbo_frame: @response[:redirect_args][:turbo_frame],
              **@response[:redirect_args].except(:turbo_frame)
            )
          when :close_modal
            # Close the modal and flash the messages
            render turbo_stream: [
              turbo_stream.close_action_modal,
              turbo_stream.flash_alerts
            ]
          else
            # Reload the page
            back_path = request.referer || params[:referrer].presence || resources_path(resource: @resource)

            render turbo_stream: turbo_stream.redirect_to(back_path)
          end
        end
      end
    end

    def get_messages
      default_message = {
        type: :info,
        body: I18n.t("avo.action_ran_successfully")
      }

      return [default_message] if @response[:messages].blank?

      @response[:messages].select do |message|
        # Remove the silent placeholder messages
        message[:type] != :silent
      end
    end

    def decrypted_query
      return if (encrypted_query = action_params[:fields][:avo_selected_query]).blank?

      Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal)
    end

    def flash_messages
      get_messages.each do |message|
        flash[message[:type]] = message[:body]
      end
    end

    def verify_authorization
      raise Avo::NotAuthorizedError.new unless @action.authorized?
    end
  end
end