class Avo::Fields::BelongsToField::EditComponent

def disabled

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 field_html_action

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

def has_polymorphic_association?

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

def initialize(...)

def initialize(...)
  super(...)
  @polymorphic_record = nil
end

def is_polymorphic?

def is_polymorphic?
  @field.types.present?
end

def polymorphic_class

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

def polymorphic_id

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

def polymorphic_record

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 polymorphic_resource

def polymorphic_resource
  Avo.resource_manager.get_resource_by_model_class(polymorphic_class)
end

def visit_through_association?

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