lib/avo/fields/belongs_to_field.rb in avo-2.53.0 vs lib/avo/fields/belongs_to_field.rb in avo-3.0.0.beta1
- old
+ new
@@ -55,10 +55,11 @@
# - foreign_key for poly type
# - foreign_key for poly id
# - is_disabled?
class BelongsToField < BaseField
+ include Avo::Fields::Concerns::IsSearchable
include Avo::Fields::Concerns::UseResource
attr_accessor :target
attr_reader :polymorphic_as
@@ -82,14 +83,10 @@
@polymorphic_help = args[:polymorphic_help]
@target = args[:target]
@use_resource = args[:use_resource] || nil
end
- def searchable
- @searchable && ::Avo::App.license.has_with_trial(:searchable_associations)
- end
-
def value
if is_polymorphic?
# Get the value from the pre-filled assoociation record
super(polymorphic_as)
else
@@ -105,31 +102,29 @@
nil
end
# What the user sees in the text field
def field_label
- value.send(target_resource.class.title)
- rescue
- nil
+ label
end
def options
values_for_type
end
def values_for_type(model = nil)
resource = target_resource
- resource = App.get_resource_by_model_name model if model.present?
+ resource = Avo.resource_manager.get_resource_by_model_class model if model.present?
- query = resource.class.query_scope
+ query = resource.query_scope
if attach_scope.present?
- query = Avo::Hosts::AssociationScopeHost.new(block: attach_scope, query: query, parent: get_model).handle
+ query = Avo::ExecutionContext.new(target: attach_scope, query: query, parent: get_model).handle
end
- query.all.map do |model|
- [model.send(resource.class.title), model.id]
+ query.all.map do |record|
+ [resource.new(record: record).record_title, record.id]
end
end
def database_value
target_resource.id
@@ -158,12 +153,12 @@
end
def foreign_key
return polymorphic_as if polymorphic_as.present?
- if @model.present?
- get_model_class(@model).reflections[@relation_method].foreign_key
+ if @record.present?
+ get_model_class(@record).reflections[@relation_method].foreign_key
elsif @resource.present? && @resource.model_class.reflections[@relation_method].present?
@resource.model_class.reflections[@relation_method].foreign_key
end
end
@@ -183,11 +178,12 @@
def relation_model_class
@resource.model_class
end
def label
- value.send(target_resource.class.title)
+ return if target_resource.blank?
+ target_resource.new(record: value)&.record_title
end
def to_permitted_param
if polymorphic_as.present?
return ["#{polymorphic_as}_type".to_sym, "#{polymorphic_as}_id".to_sym]
@@ -235,30 +231,30 @@
def target_resource
return use_resource if use_resource.present?
if is_polymorphic?
if value.present?
- return App.get_resource_by_model_name(value.class)
+ return Avo.resource_manager.get_resource_by_model_class(value.class)
else
return nil
end
end
reflection_key = polymorphic_as || id
- if @model.class.reflect_on_association(reflection_key).klass.present?
- App.get_resource_by_model_name @model.class.reflect_on_association(reflection_key).klass.to_s
- elsif @model.class.reflect_on_association(reflection_key).options[:class_name].present?
- App.get_resource_by_model_name @model.class.reflect_on_association(reflection_key).options[:class_name]
+ if @record._reflections[reflection_key.to_s].klass.present?
+ Avo.resource_manager.get_resource_by_model_class @record._reflections[reflection_key.to_s].klass.to_s
+ elsif @record._reflections[reflection_key.to_s].options[:class_name].present?
+ Avo.resource_manager.get_resource_by_model_class @record._reflections[reflection_key.to_s].options[:class_name]
else
App.get_resource_by_name reflection_key.to_s
end
end
def get_model
- return @model if @model.present?
+ return @record if @record.present?
- @resource.model
+ @resource.record
rescue
nil
end
def name