Add non-prefixed polymorphic admin routes

These routes are solved in a different way because of an inconsistency:
we define `groups` and `budget_investments`; we should either use the
`budget_` prefix in all places or remove it everywhere.

We can now share code using `polymorphic_path` even with these models.
This commit is contained in:
Javi Martín
2020-06-06 19:04:51 +02:00
parent 7563b7f4d1
commit 65604a92c2
4 changed files with 88 additions and 4 deletions

View File

@@ -2,9 +2,9 @@
# routes with nested resources in the admin namespace
module ActionDispatch::Routing::UrlFor
def resource_hierarchy_for(resource)
if polymorphic_mapping(resource)
resolve = polymorphic_mapping(resource).send(:eval_block, self, resource, {})
resolve = resolve_for(resource)
if resolve
if resolve.last.is_a?(Hash)
[resolve.first, *resolve.last.values]
else
@@ -16,6 +16,20 @@ module ActionDispatch::Routing::UrlFor
end
def admin_polymorphic_path(resource, options = {})
polymorphic_path([:admin, *resource_hierarchy_for(resource)], options)
if %w[Budget::Group Budget::Heading Poll::Booth Poll::Officer
Poll::Question Poll::Question::Answer::Video].include?(resource.class.name)
resolve = resolve_for(resource)
resolve_options = resolve.pop
polymorphic_path([:admin, *resolve], options.merge(resolve_options))
else
polymorphic_path([:admin, *resource_hierarchy_for(resource)], options)
end
end
private
def resolve_for(resource)
polymorphic_mapping(resource)&.send(:eval_block, self, resource, {})
end
end