lib/avo/base_resource.rb in avo-2.26.1.pr1584.pre.1 vs lib/avo/base_resource.rb in avo-2.26.2.pre.pr1579
- old
+ new
@@ -40,10 +40,14 @@
class_attribute :grid_loader
class_attribute :visible_on_sidebar, default: true
class_attribute :unscoped_queries_on_index, default: false
class_attribute :resolve_query_scope
class_attribute :resolve_find_scope
+ # TODO: refactor this into a Host without args
+ class_attribute :find_record_method, default: ->(model_class:, id:, params:) {
+ model_class.find id
+ }
class_attribute :ordering
class_attribute :hide_from_global_search, default: false
class_attribute :after_create_path, default: :show
class_attribute :after_update_path, default: :show
class_attribute :record_selector, default: true
@@ -377,11 +381,15 @@
if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
# set the value to the actual record
value = @params[:via_relation_class].safe_constantize.find(@params[:via_resource_id])
elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
- value = @params[:via_resource_id]
+ resource = Avo::App.get_resource_by_model_name params[:via_relation_class]
+ model = resource.find_record @params[:via_resource_id], params: params
+ id_param = reflection.options[:primary_key] || :id
+
+ value = model.send(id_param)
end
end
[id, value]
end
@@ -485,8 +493,14 @@
Avo::Hosts::Ordering.new resource: self, options: self.class.ordering, **args
end
def has_model_id?
model.present? && model.id.present?
+ end
+
+ def find_record(id, query: nil, params: nil)
+ query ||= self.class.find_scope
+
+ self.class.find_record_method.call(model_class: query, id: id, params: params)
end
end
end