Skip to content

Commit

Permalink
fix: MissingResourceError on polymorphic field
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Aug 2, 2024
1 parent 0ccee7b commit d53b045
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ class MissingGemError < StandardError; end
class DeprecatedAPIError < StandardError; end

class MissingResourceError < StandardError
def initialize(resource_name)
super(missing_resource_message(resource_name))
def initialize(model_class, field_name = nil)
super(missing_resource_message(model_class, field_name))
end

private

def missing_resource_message(resource_name)
name = resource_name.to_s.underscore
def missing_resource_message(model_class, field_name)
model_name = model_class.to_s.underscore
field_name ||= model_name

"Failed to find a resource while rendering the :#{name} field.\n" \
"You may generate a resource for it by running 'rails generate avo:resource #{name.singularize}'.\n" \
"Failed to find a resource while rendering the :#{field_name} field.\n" \
"You may generate a resource for it by running 'rails generate avo:resource #{model_name}'.\n" \
"\n" \
"Alternatively add the 'use_resource' option to the :#{name} field to specify a custom resource to be used.\n" \
"Alternatively add the 'use_resource' option to the :#{field_name} field to specify a custom resource to be used.\n" \
"More info on https://docs.avohq.io/#{Avo::VERSION[0]}.0/resources.html."
end
end
Expand All @@ -75,7 +76,7 @@ def init
Avo::Current.error_manager = Avo::ErrorManager.build
# Check rails version issues only on NON Production environments
unless Rails.env.production?
check_rails_version_issues
check_rails_version_issues
display_menu_editor_warning
end
Avo::Current.resource_manager = Avo::Resources::ResourceManager.build
Expand Down
2 changes: 1 addition & 1 deletion lib/avo/fields/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def in_action?
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))
resource || (raise Avo::MissingResourceError.new(model_class, id))
end
end
end
Expand Down

0 comments on commit d53b045

Please sign in to comment.