Class: EasyAdmin::Layouts::LayoutContext

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_admin/layouts/layout_context.rb

Overview

Context object for safe data passing through layout rendering

Defined Under Namespace

Classes: CleanRoom

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record: nil, resource_class: nil, current_user: nil, view_context: nil, form_builder: nil, request_context: {}) ⇒ LayoutContext

Returns a new instance of LayoutContext.



13
14
15
16
17
18
19
20
21
# File 'lib/easy_admin/layouts/layout_context.rb', line 13

def initialize(record: nil, resource_class: nil, current_user: nil, view_context: nil, form_builder: nil, request_context: {})
  @record = record
  @resource_class = resource_class
  @current_user = current_user
  @view_context = view_context
  @form_builder = form_builder
  @request_context = request_context
  @data = {} # Custom data storage
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Method missing to delegate to view_context helpers



107
108
109
110
111
112
113
# File 'lib/easy_admin/layouts/layout_context.rb', line 107

def method_missing(method, *args, &block)
  if @view_context && @view_context.respond_to?(method)
    @view_context.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



5
6
7
# File 'lib/easy_admin/layouts/layout_context.rb', line 5

def current_user
  @current_user
end

#form_builderObject

Returns the value of attribute form_builder.



6
7
8
# File 'lib/easy_admin/layouts/layout_context.rb', line 6

def form_builder
  @form_builder
end

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'lib/easy_admin/layouts/layout_context.rb', line 5

def record
  @record
end

#request_contextObject (readonly)

Returns the value of attribute request_context.



5
6
7
# File 'lib/easy_admin/layouts/layout_context.rb', line 5

def request_context
  @request_context
end

#resource_classObject (readonly)

Returns the value of attribute resource_class.



5
6
7
# File 'lib/easy_admin/layouts/layout_context.rb', line 5

def resource_class
  @resource_class
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



5
6
7
# File 'lib/easy_admin/layouts/layout_context.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#can?(action, subject = nil) ⇒ Boolean

Check if current user can perform action

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/easy_admin/layouts/layout_context.rb', line 87

def can?(action, subject = nil)
  return false unless @current_user
  
  # If ActionPolicy is available, use it
  if defined?(ActionPolicy) && @view_context
    @view_context.allowed_to?(action, subject || @record)
  else
    # Fallback to basic checks
    true
  end
rescue
  false
end

#current_actionObject

Get current action from request context



82
83
84
# File 'lib/easy_admin/layouts/layout_context.rb', line 82

def current_action
  @request_context[:action]
end

#formObject

Alias for form_builder to match expected API



9
10
11
# File 'lib/easy_admin/layouts/layout_context.rb', line 9

def form
  @form_builder
end

#form_context?Boolean

Check if we’re in a form context

Returns:

  • (Boolean)


72
73
74
# File 'lib/easy_admin/layouts/layout_context.rb', line 72

def form_context?
  !@form_builder.nil?
end

#get(key) ⇒ Object

Retrieve custom data



29
30
31
# File 'lib/easy_admin/layouts/layout_context.rb', line 29

def get(key)
  @data[key.to_sym]
end

#has?(key) ⇒ Boolean

Check if custom data exists

Returns:

  • (Boolean)


34
35
36
# File 'lib/easy_admin/layouts/layout_context.rb', line 34

def has?(key)
  @data.key?(key.to_sym)
end

#helpersObject

Access helpers from view context



102
103
104
# File 'lib/easy_admin/layouts/layout_context.rb', line 102

def helpers
  @view_context
end

#instance_exec(&block) ⇒ Object

Safe evaluation context for conditions



67
68
69
# File 'lib/easy_admin/layouts/layout_context.rb', line 67

def instance_exec(&block)
  CleanRoom.new(self).instance_exec(&block)
end

#merge(additional_context) ⇒ Object

Merge additional context



39
40
41
42
43
44
# File 'lib/easy_admin/layouts/layout_context.rb', line 39

def merge(additional_context)
  additional_context.each do |key, value|
    set(key, value)
  end
  self
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/easy_admin/layouts/layout_context.rb', line 115

def respond_to_missing?(method, include_private = false)
  (@view_context && @view_context.respond_to?(method)) || super
end

#set(key, value) ⇒ Object

Store custom data



24
25
26
# File 'lib/easy_admin/layouts/layout_context.rb', line 24

def set(key, value)
  @data[key.to_sym] = value
end

#show_context?Boolean

Check if we’re in a show context

Returns:

  • (Boolean)


77
78
79
# File 'lib/easy_admin/layouts/layout_context.rb', line 77

def show_context?
  @form_builder.nil? && @record.present?
end

#with(additional_context = {}) ⇒ Object

Create a child context with additional data



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easy_admin/layouts/layout_context.rb', line 47

def with(additional_context = {})
  child = self.class.new(
    record: @record,
    resource_class: @resource_class,
    current_user: @current_user,
    view_context: @view_context,
    form_builder: @form_builder,
    request_context: @request_context
  )
  
  # Copy parent data
  @data.each { |k, v| child.set(k, v) }
  
  # Add new data
  additional_context.each { |k, v| child.set(k, v) }
  
  child
end