Class: Avo::Fields::BelongsToField::AutocompleteComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/avo/fields/belongs_to_field/autocomplete_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form:, field:, model_key:, foreign_key:, disabled: false, type: nil, resource: nil, polymorphic_record: nil, view: nil, style: nil, classes: nil) ⇒ AutocompleteComponent

Returns a new instance of AutocompleteComponent.



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

def initialize(form:, field:, model_key:, foreign_key:, disabled: false, type: nil, resource: nil, polymorphic_record: nil, view: nil, style: nil, classes: nil)
  @form = form
  @field = field
  @type = type
  @view = view
  @model_key = model_key
  @foreign_key = foreign_key
  @resource = resource
  @disabled = disabled
  @style = style
  @classes = classes
  @polymorphic_record = polymorphic_record
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



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

def classes
  @classes
end

#viewObject (readonly)

Returns the value of attribute view.



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

def view
  @view
end

Instance Method Details

#field_labelObject



21
22
23
24
25
26
27
28
29
30
# File 'app/components/avo/fields/belongs_to_field/autocomplete_component.rb', line 21

def field_label
  result = @field.field_label

  # New records won't have the value (instantiated model) present but the polymorphic_type and polymorphic_id prefilled
  if should_prefill?
    result = @field.value&.class == @type ? @field.field_label : nil
  end

  result
end

#field_valueObject



32
33
34
35
36
37
38
39
40
41
# File 'app/components/avo/fields/belongs_to_field/autocomplete_component.rb', line 32

def field_value
  result = @field.field_value

  # New records won't have the value (instantiated model) present but the polymorphic_type and polymorphic_id prefilled
  if should_prefill?
    result = @field.value&.class == @type ? @field.field_value : nil
  end

  result
end

#reflection_classObject



43
44
45
# File 'app/components/avo/fields/belongs_to_field/autocomplete_component.rb', line 43

def reflection_class
  has_polymorphic_association? ? polymorphic_class : @resource.model_class._reflections[@field.id.to_s].klass
end