Use "resolve" for polymorphic hierarchy paths

In the past, we couldn't use `polymorphic_path` in many places. For
instance, `polymorphic_path(budget, investment)` would return
`budget_budget_investment_path`, while in our routes we had defined
`budget_investment_path`.

With the `resolve` method, introduced in Rails 5.1, we can use symbols
to define we want it to use `investment` instead of `budget_investment`.
It also works with nested resources, so now we can write
`polymorphic_path(investment)`.

This makes the code for `resource_hierarchy_for` almost impossible to
understand. I reached this result after having a look at the internals
of the `resolve` method in order to get its results and then remove the
symbols we include.

Note using this method will not make admin routes compatible with
`polymorphic_path`. Quoting from the Rails documentation:

> This custom behavior only applies to simple polymorphic URLs where a
> single model instance is passed and not more complicated forms, e.g:
> [example showing admin routes won't work]

Also note that now the `admin_polymorphic_path` method will not work for
every model due to inconsistencies in our admin routes. For instance, we
define `groups` and `budget_investments`; we should either use the
`budget_` prefix in all places or remove it everywhere. Right now the
code only works for items with the prefix; it isn't a big deal because
we never call it with an item without the prefix.

Finally, for unknown reasons some routing tests fail if we use
`polymorphic_path`, so we need to redefine that method in those tests
and force the `only_path: true` option.
This commit is contained in:
Javi Martín
2020-03-16 16:36:58 +01:00
parent 334b57501b
commit ff93f5a591
13 changed files with 165 additions and 65 deletions

View File

@@ -1,69 +1,20 @@
# This module is expanded in order to make it easier to use polymorphic
# routes with nested resources.
# HACK: is there a way to avoid monkey-patching here? Using helpers is
# a similar use of a global namespace too...
# routes with nested resources in the admin namespace
module ActionDispatch::Routing::UrlFor
def resource_hierarchy_for(resource)
case resource.class.name
when "Budget::Investment", "Budget::Phase", "Budget::Group"
[resource.budget, resource]
when "Budget::Heading"
[resource.group.budget, resource.group, resource]
when "Milestone"
[*resource_hierarchy_for(resource.milestoneable), resource]
when "ProgressBar"
[*resource_hierarchy_for(resource.progressable), resource]
when "Audit"
[*resource_hierarchy_for(resource.associated || resource.auditable), resource]
when "Legislation::Annotation"
[resource.draft_version.process, resource.draft_version, resource]
when "Legislation::Proposal", "Legislation::Question", "Legislation::DraftVersion"
[resource.process, resource]
when "Topic"
[resource.community, resource]
if polymorphic_mapping(resource)
resolve = polymorphic_mapping(resource).send(:eval_block, self, resource, {})
if resolve.last.is_a?(Hash)
[resolve.first, *resolve.last.values]
else
resolve
end
else
resource
end
end
def polymorphic_hierarchy_path(resource)
# Unfortunately, we can't use polymorphic routes because there
# are cases where polymorphic_path doesn't get the named routes properly.
# Example:
#
# polymorphic_path([legislation_proposal.process, legislation_proposal])
#
# That line tries to find legislation_process_legislation_proposal_path
# while the correct route would be legislation_process_proposal_path
#
# We probably need to define routes differently in order to be able to use
# polymorphic_path which might be possible with Rails 5.1 `direct` and
# `resolve` methods.
resources = resource_hierarchy_for(resource)
case resource.class.name
when "Budget::Investment"
# polymorphic_path would return budget_budget_investment_path
budget_investment_path(*resources)
when "Legislation::Annotation"
# polymorphic_path would return:
# "legislation_process_legislation_draft_version_legislation_annotation_path"
legislation_process_draft_version_annotation_path(*resources)
when "Legislation::Proposal"
# polymorphic_path would return legislation_process_legislation_proposal_path
legislation_process_proposal_path(*resources)
when "Legislation::Question"
# polymorphic_path would return legislation_process_legislation_question_path
legislation_process_question_path(*resources)
when "Poll::Question"
# polymorphic_path would return poll_question_path
question_path(*resources)
else
polymorphic_path(resources)
end
end
def admin_polymorphic_path(resource, options = {})
polymorphic_path([:admin, *resource_hierarchy_for(resource)], options)
end