Module: Avo::Fields::Concerns::UseViewComponents

Extended by:
ActiveSupport::Concern
Included in:
BaseField
Defined in:
lib/avo/fields/concerns/use_view_components.rb

Instance Method Summary collapse

Instance Method Details

#component_for_view(view = :index) ⇒ Object

Try and build the component class or fallback to a blank one



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/avo/fields/concerns/use_view_components.rb', line 21

def component_for_view(view = :index)
  # Use the edit variant for all "update" views
  view = :edit if view.in? [:new, :create, :update]

  custom_components = Avo::ExecutionContext.new(
    target: components,
    resource: @resource,
    record: @record,
    view: view
  ).handle

  component_class = custom_components.dig(:"#{view}_component") || "#{view_component_namespace}::#{view.to_s.camelize}Component"
  component_class.to_s.constantize
rescue
  unless Rails.env.test?
    Avo.logger.info "Failed to find component for the `#{self.class}` field on the `#{view}` view."
  end
  # When returning nil, a race condition happens and throws an error in some environments.
  # See https://github.com/avo-hq/avo/pull/365
  ::Avo::BlankFieldComponent
end

#view_component_nameObject



11
12
13
# File 'lib/avo/fields/concerns/use_view_components.rb', line 11

def view_component_name
  "#{type.camelize}Field"
end

#view_component_namespaceObject

For custom components the namespace will be different than Avo::Fields so we should take that into account.



16
17
18
# File 'lib/avo/fields/concerns/use_view_components.rb', line 16

def view_component_namespace
  "#{self.class.to_s.deconstantize}::#{view_component_name}"
end