Sha256: be4b4bcded8d039bee2d549ae308c97ac536e13799c8c93afd83452e7e119578

Contents?: true

Size: 1.43 KB

Versions: 83

Compression:

Stored size: 1.43 KB

Contents

module Avo
  class ExecutionContext
    include Avo::Concerns::HasHelpers

    attr_accessor :target, :context, :params, :view_context, :current_user, :request, :include, :main_app, :avo, :locale

    delegate_missing_to :@view_context

    def initialize(**args)
      # Extend the class with custom modules if required.
      if args[:include].present?
        args[:include].each do |mod|
          self.class.send(:include, mod)
        end
      end

      # If target doesn't respond to call, we don't need to initialize the others attr_accessors.
      return unless (@target = args[:target]).respond_to? :call

      args.except(:target).each do |key, value|
        singleton_class.class_eval { attr_accessor key }
        instance_variable_set("@#{key}", value)
      end

      # Set defaults on not initialized accessors
      @context ||= Avo::Current.context
      @current_user ||= Avo::Current.user
      @params ||= Avo::Current.params
      @request ||= Avo::Current.request
      @view_context ||= Avo::Current.view_context
      @locale ||= Avo::Current.locale
      @main_app ||= @view_context&.main_app
      @avo ||= @view_context&.avo
    end

    delegate :result, to: :card
    delegate :authorize, to: Avo::Services::AuthorizationService

    # Return target if target is not callable, otherwise, execute target on this instance context
    def handle
      target.respond_to?(:call) ? instance_exec(&target) : target
    end
  end
end

Version data entries

83 entries across 83 versions & 1 rubygems

Version Path
avo-3.16.4 lib/avo/execution_context.rb
avo-3.16.3 lib/avo/execution_context.rb
avo-3.16.2 lib/avo/execution_context.rb
avo-3.16.1 lib/avo/execution_context.rb
avo-3.16.0 lib/avo/execution_context.rb
avo-3.15.7 lib/avo/execution_context.rb
avo-3.15.6 lib/avo/execution_context.rb
avo-3.15.5 lib/avo/execution_context.rb
avo-3.15.4 lib/avo/execution_context.rb
avo-3.15.3 lib/avo/execution_context.rb
avo-3.15.2 lib/avo/execution_context.rb
avo-3.15.1 lib/avo/execution_context.rb
avo-3.15.0 lib/avo/execution_context.rb
avo-3.14.5 lib/avo/execution_context.rb
avo-3.14.4 lib/avo/execution_context.rb
avo-3.14.3 lib/avo/execution_context.rb
avo-3.14.2 lib/avo/execution_context.rb
avo-3.14.1 lib/avo/execution_context.rb
avo-3.14.0 lib/avo/execution_context.rb
avo-3.13.7 lib/avo/execution_context.rb