Sha256: d620702e3bdd3cb66c7c3550bce15b3c18f6c9f5804128c3112a696658bc51ba

Contents?: true

Size: 1.82 KB

Versions: 33

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

class Avo::Fields::BelongsToField::EditComponent < Avo::Fields::EditComponent
  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

  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

  def is_polymorphic?
    @field.types.present?
  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

  def polymorphic_resource
    ::Avo::App.get_resource_by_model_name(polymorphic_class)
  end

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

  # Get the actual resource
  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

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

  private

  def visit_through_association?
    @field.target_resource.class.to_s == params[:via_resource_class].to_s
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
avo-2.35.0 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.7.pre.1 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.6 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.5 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.4 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.4.pre.1 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.3 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.2 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.1 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.34.0 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.33.3 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.33.3.pre.2 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.33.3.pre.1 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.33.2 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.33.1 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.33.0 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.32.6 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.32.5 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.32.4 app/components/avo/fields/belongs_to_field/edit_component.rb
avo-2.32.3 app/components/avo/fields/belongs_to_field/edit_component.rb