Class: Avo::Fields::BelongsToField::EditComponent

Inherits:
EditComponent
  • Object
show all
Defined in:
app/components/avo/fields/belongs_to_field/edit_component.rb

Instance Attribute Summary

Attributes inherited from EditComponent

#compact, #field, #form, #index, #multiple, #resource, #stacked, #view

Instance Method Summary collapse

Methods inherited from EditComponent

#classes, #disabled?, #field_wrapper_args, #render?

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_select_all_input, #item_selector_init, #item_selector_input, #resource_grid, #resource_table

Constructor Details

#initialize(field: nil, resource: nil, index: 0, form: nil, compact: false) ⇒ EditComponent

Returns a new instance of EditComponent.



4
5
6
7
8
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 4

def initialize(field: nil, resource: nil, index: 0, form: nil, compact: false)
  super field: field, resource: resource, index: index, form: form, compact: compact

  @polymorphic_record = nil
end

Instance Method Details

#disabledObject



10
11
12
13
14
15
16
17
18
19
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 10

def disabled
  return true if @field.is_readonly? || @field.is_disabled?

  # When visiting the record through it's association we keep the field disabled by default
  # We make an exception when the user deliberately instructs Avo to allow detaching in this scenario
  return !@field.allow_via_detaching if @field.target_resource.present? && visit_through_association?
  return !@field.allow_via_detaching if @field.id.to_s == params[:via_relation].to_s

  false
end

#field_html_actionObject



52
53
54
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 52

def field_html_action
  @field.get_html(:data, view: @resource.view, element: :input).fetch(:action, nil)
end

#has_polymorphic_association?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 25

def has_polymorphic_association?
  polymorphic_class.present? && polymorphic_id.present?
end

#is_polymorphic?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 21

def is_polymorphic?
  @field.types.present?
end

#polymorphic_classObject

Get the polymorphic class



30
31
32
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 30

def polymorphic_class
  @resource.model["#{@field.foreign_key}_type"]
end

#polymorphic_idObject

Get the polymorphic id



35
36
37
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 35

def polymorphic_id
  @resource.model["#{@field.foreign_key}_id"]
end

#polymorphic_recordObject

Get the actual resource



40
41
42
43
44
45
46
47
48
49
50
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 40

def polymorphic_record
  return unless has_polymorphic_association?

  return unless is_polymorphic?

  return @polymorphic_record if @polymorphic_record.present?

  @polymorphic_record = polymorphic_class.safe_constantize.find polymorphic_id

  @polymorphic_record
end