Class: Avo::ExecutionContext

Inherits:
Object
  • Object
show all
Includes:
Concerns::HasHelpers
Defined in:
lib/avo/execution_context.rb

Overview

Avo Execution Context

The ExecutionContext class is used to evaluate blocks in isolation.

Direct Known Subclasses

FieldsExecutionContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasHelpers

#helpers

Constructor Details

#initialize(**args) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/avo/execution_context.rb', line 12

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

Instance Attribute Details

#avoObject

Returns the value of attribute avo.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def avo
  @avo
end

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def context
  @context
end

#current_userObject

Returns the value of attribute current_user.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def current_user
  @current_user
end

#includeObject

Returns the value of attribute include.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def include
  @include
end

#localeObject

Returns the value of attribute locale.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def locale
  @locale
end

#main_appObject

Returns the value of attribute main_app.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def main_app
  @main_app
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def params
  @params
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def request
  @request
end

#targetObject

Returns the value of attribute target.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def target
  @target
end

#view_contextObject

Returns the value of attribute view_context.



8
9
10
# File 'lib/avo/execution_context.rb', line 8

def view_context
  @view_context
end

Instance Method Details

#handleObject

Executes the target and returns the result. It takes in a target which usually is a block. If it’s something else, it will return it.

It automatically has access to the view context, current user, request, main app, avo, locale, and params. It also has a delegate_missing_to which allows it to delegate missing methods to the view context for a more natural experience. You may pass extra arguments to the initialize method to have them available in the block that will be executed. You may pass extra modules to extend the class with.

Examples

Normal use
Avo::ExecutionContext.new(target: -> { "Hello, world!" }).handle
=> "Hello, world!"
Providing a record
Avo::ExecutionContext.new(target: -> { record.name }, record: @record).handle
=> "John Doe"
Providing a module

This will include the SanitizeHelper module in the class and so have the sanitize method available.

Avo::ExecutionContext.new(target: -> { sanitize "<script>alert('be careful');</script>#{record.name}" } record: @record, include: [ActionView::Helpers::SanitizeHelper]).handle
=> "John Doe"


68
69
70
# File 'lib/avo/execution_context.rb', line 68

def handle
  target.respond_to?(:call) ? instance_exec(&target) : target
end