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

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

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Attribute Summary

Attributes inherited from EditComponent

#compact, #field, #form, #index, #kwargs, #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_selector_data_attributes, #resource_grid, #resource_show_path, #resource_table

Methods inherited from BaseComponent

#has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Constructor Details

#initializeEditComponent

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(...)
  super(...)

  @polymorphic_record = nil
end

Instance Method Details

#create_path(target_resource = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 60

def create_path(target_resource = nil)
  return nil if @resource.blank?

  helpers.new_resource_path(**{
    via_relation: @field.id.to_s,
    resource: target_resource || @field.target_resource,
    via_record_id: resource.record.persisted? ? resource.record.to_param : nil,
    via_belongs_to_resource_class: resource.class.name
  }.compact)
end

#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



56
57
58
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 56

def field_html_action
  @field.get_html(:data, view: 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.record["#{@field.foreign_key}_type"]
end

#polymorphic_idObject

Get the polymorphic id



39
40
41
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 39

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

#polymorphic_recordObject

Get the actual resource



44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/avo/fields/belongs_to_field/edit_component.rb', line 44

def polymorphic_record
  return unless has_polymorphic_association?

  return unless is_polymorphic?

  return @polymorphic_record if @polymorphic_record.present?

  @polymorphic_record = polymorphic_resource.find_record polymorphic_id, query: polymorphic_class.safe_constantize, params: params

  @polymorphic_record
end

#polymorphic_resourceObject



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

def polymorphic_resource
  Avo.resource_manager.get_resource_by_model_class(polymorphic_class)
end