Sha256: bc14a07069f38c3bbbb7f161382ec2cd3daef9efc035c597b182c108ece2e2b3

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

class Avo::Fields::BelongsToField::AutocompleteComponent < ViewComponent::Base
  def initialize(form:, field:, model_key:, foreign_key:, disabled:, type: nil, resource: nil, polymorphic_record: nil)
    @form = form
    @field = field
    @type = type
    @model_key = model_key
    @foreign_key = foreign_key
    @resource = resource
    @disabled = disabled
    @polymorphic_record = polymorphic_record
  end

  def field_label
    if searchable?
      # New records won't have the value (instantiated model) present but the polymorphic_type and polymorphic_id prefilled
      if new_record? && has_polymorphic_association?
        @polymorphic_record.send(polymorphic_fields[:label])
      else
        @field.value&.class == @type ? @field.field_label : nil
      end
    else
      @field.field_label
    end
  end

  def field_value
    if searchable?
      # New records won't have the value (instantiated model) present but the polymorphic_type and polymorphic_id prefilled
      if new_record? && has_polymorphic_association?
        @polymorphic_record.send(polymorphic_fields[:id])
      else
        @field.value&.class == @type ? @field.field_value : nil
      end
    else
      @field.field_value
    end
  end

  private

  def searchable?
    @type.present?
  end

  def new_record?
    @resource.model.new_record?
  end

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

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

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

  # Get the resource for that polymorphic class
  def polymorphic_resource
    ::Avo::App.get_resource_by_model_name polymorphic_class
  end

  # Extract the needed fields to identify the record for polymorphic associations
  def polymorphic_fields
    {
      id: polymorphic_resource.id,
      label: polymorphic_resource.title
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
avo-1.22.1.pre.1 app/components/avo/fields/belongs_to_field/autocomplete_component.rb
avo-1.22.0 app/components/avo/fields/belongs_to_field/autocomplete_component.rb
avo-1.22.0.pre.1 app/components/avo/fields/belongs_to_field/autocomplete_component.rb