Skip to content

Commit

Permalink
Catch errors for field_resource and target_resource on HasBaseField a…
Browse files Browse the repository at this point in the history
…nd BelongsToField
  • Loading branch information
bryszard committed Dec 6, 2023
1 parent 41df23b commit 0642a60
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/avo/fields/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/avo/fields/has_base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0642a60

Please sign in to comment.