Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespaced Models and the Collection Responder #244

Open
brendon opened this issue May 8, 2023 · 0 comments
Open

Namespaced Models and the Collection Responder #244

brendon opened this issue May 8, 2023 · 0 comments

Comments

@brendon
Copy link

brendon commented May 8, 2023

I've come across a situation where the CollectionResponder improperly (for me) produces collection routes that include the full namespace of a model for the route_key. I've overridden this in my app to the following:

  def navigation_location
    return options[:location] if options[:location]

    klass = resources.last.class

    if klass.respond_to?(:model_name)
      model_name = ActiveModel::Name.new(klass, klass.module_parent)
      resources[0...-1] << model_name.route_key.to_sym
    else
      resources
    end
  end

Specifically I've changed: model_name = ActiveModel::Name.new(klass, klass.module_parent) (Rails 6.1+ I believe) so that it returns a model_name object that has been passed in the module_parent namespace. This results in the route_key being namespaceless.

For example I had respond_with :admin, form, submission in my destroy method for submissions. Submission is a model: Forms::Submission and I wanted a route admin_form_submissions_path but was getting admin_form_form_submissions_path because of the namespace.

I've fixed it for me, but wondered if you think it's worth allowing it to be configurable for this responder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant