From c08e7d97a20e130ef61919537f33441bb0042d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= Date: Fri, 2 Aug 2024 08:47:34 +0200 Subject: [PATCH] refactor: Prefer Model.reflections over private Model#_reflections. (#2927) * Prefer Model.reflections over private Model#_reflections. - `_reflections` returns different key type per Rails version * Move to reflect_on_association. --- lib/avo/base_resource.rb | 4 ++-- lib/avo/fields/belongs_to_field.rb | 8 ++++---- lib/avo/fields/has_base_field.rb | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/avo/base_resource.rb b/lib/avo/base_resource.rb index 956bcfbe97..2abb8371e6 100644 --- a/lib/avo/base_resource.rb +++ b/lib/avo/base_resource.rb @@ -114,7 +114,7 @@ def order_actions end def get_record_associations(record) - record._reflections + record.class.reflections end def valid_association_name(record, association_name) @@ -382,7 +382,7 @@ def hydrate_model_with_default_values if field.type == "belongs_to" - reflection = @model._reflections[@params[:via_relation]] + reflection = @model.class.reflections[@params[:via_relation]] if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class]) # set the value to the actual record diff --git a/lib/avo/fields/belongs_to_field.rb b/lib/avo/fields/belongs_to_field.rb index 1513d6cf41..a93c442a2d 100644 --- a/lib/avo/fields/belongs_to_field.rb +++ b/lib/avo/fields/belongs_to_field.rb @@ -245,10 +245,10 @@ def target_resource reflection_key = polymorphic_as || id - if @model._reflections[reflection_key.to_s].klass.present? - App.get_resource_by_model_name @model._reflections[reflection_key.to_s].klass.to_s - elsif @model._reflections[reflection_key.to_s].options[:class_name].present? - App.get_resource_by_model_name @model._reflections[reflection_key.to_s].options[:class_name] + 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] else App.get_resource_by_name reflection_key.to_s end diff --git a/lib/avo/fields/has_base_field.rb b/lib/avo/fields/has_base_field.rb index c63378007f..d161b402e2 100644 --- a/lib/avo/fields/has_base_field.rb +++ b/lib/avo/fields/has_base_field.rb @@ -59,10 +59,10 @@ def field_label end def target_resource - if @model._reflections[id.to_s].klass.present? - Avo::App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s - elsif @model._reflections[id.to_s].options[:class_name].present? - Avo::App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name] + if @model.class.reflect_on_association(id).klass.present? + Avo::App.get_resource_by_model_name @model.class.reflect_on_association(id).klass.to_s + elsif @model.class.reflect_on_association(id).options[:class_name].present? + Avo::App.get_resource_by_model_name @model.class.reflect_on_association(id).options[:class_name] else Avo::App.get_resource_by_name id.to_s end