Files
nairobi/config/initializers/routes_hierarchy.rb
Javi Martín 85f13f9501 Use namespace as symbol in polymorphic_path calls
Our `namespace` helper returns a string. However, Rails version 5.2.4.6
doesn't allow strings as arguments to polymorphic_path [1]

Since returning a symbol in our `namespace` helper would break other
places in the application, we're converting it to a symbol in the
methods calling `polymorphic_path`.

[1] https://github.com/advisories/GHSA-hjg4-8q5f-x6fm
2021-05-20 18:37:45 +02:00

45 lines
1.3 KiB
Ruby

# This module is expanded in order to make it easier to use polymorphic
# routes with nested resources in the admin namespace
module ActionDispatch::Routing::UrlFor
def resource_hierarchy_for(resource)
resolve = resolve_for(resource)
if resolve
if resolve.last.is_a?(Hash)
[resolve.first, *resolve.last.values]
else
resolve
end
else
resource
end
end
def admin_polymorphic_path(resource, options = {})
namespaced_polymorphic_path(:admin, resource, options)
end
def sdg_management_polymorphic_path(resource, options = {})
namespaced_polymorphic_path(:sdg_management, resource, options)
end
def namespaced_polymorphic_path(namespace, resource, options = {})
if %w[Budget::Group Budget::Heading Poll::Booth Poll::BoothAssignment Poll::Officer
Poll::Question Poll::Question::Answer::Video Poll::Shift
SDG::LocalTarget].include?(resource.class.name)
resolve = resolve_for(resource)
resolve_options = resolve.pop
polymorphic_path([namespace.to_sym, *resolve], options.merge(resolve_options))
else
polymorphic_path([namespace.to_sym, *resource_hierarchy_for(resource)], options)
end
end
private
def resolve_for(resource)
polymorphic_mapping(resource)&.send(:eval_block, self, resource, {})
end
end