From 0642a60dd4771cef9a3ffce404a63a6c15c98a5a Mon Sep 17 00:00:00 2001 From: Brychu Date: Wed, 6 Dec 2023 10:18:39 +0100 Subject: [PATCH] Catch errors for field_resource and target_resource on HasBaseField and BelongsToField --- lib/avo/fields/base_field.rb | 6 ++++++ lib/avo/fields/belongs_to_field.rb | 6 +++--- lib/avo/fields/has_base_field.rb | 6 +++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/avo/fields/base_field.rb b/lib/avo/fields/base_field.rb index 43f80c91f5..1d19f77ac4 100644 --- a/lib/avo/fields/base_field.rb +++ b/lib/avo/fields/base_field.rb @@ -289,6 +289,12 @@ def on_create? def in_action? @action.present? end + + def get_resource_by_model_class(model_class) + resource = Avo.resource_manager.get_resource_by_model_class(model_class) + + resource || (raise Avo::MissingResourceError.new(model_class)) + end end end end diff --git a/lib/avo/fields/belongs_to_field.rb b/lib/avo/fields/belongs_to_field.rb index 96a24e43bf..489552b8cf 100644 --- a/lib/avo/fields/belongs_to_field.rb +++ b/lib/avo/fields/belongs_to_field.rb @@ -234,7 +234,7 @@ def target_resource if is_polymorphic? if value.present? - return Avo.resource_manager.get_resource_by_model_class(value.class) + return get_resource_by_model_class(value.class) else return nil end @@ -243,9 +243,9 @@ def target_resource reflection_key = polymorphic_as || id 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 + 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] + 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 diff --git a/lib/avo/fields/has_base_field.rb b/lib/avo/fields/has_base_field.rb index 6940b90149..d61989f604 100644 --- a/lib/avo/fields/has_base_field.rb +++ b/lib/avo/fields/has_base_field.rb @@ -27,7 +27,7 @@ def initialize(id, **args, &block) end def field_resource - resource || Avo.resource_manager.get_resource_by_model_class(@record.class) + resource || get_resource_by_model_class(@record.class) end def turbo_frame @@ -57,9 +57,9 @@ def field_label def target_resource if @record._reflections[id.to_s].klass.present? - Avo.resource_manager.get_resource_by_model_class @record._reflections[id.to_s].klass.to_s + get_resource_by_model_class(@record._reflections[id.to_s].klass.to_s) elsif @record._reflections[id.to_s].options[:class_name].present? - Avo.resource_manager.get_resource_by_model_class @record._reflections[id.to_s].options[:class_name] + get_resource_by_model_class(@record._reflections[id.to_s].options[:class_name]) else Avo.resource_manager.get_resource_by_name id.to_s end