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:
@@ -2,9 +2,9 @@
|
|||||||
# routes with nested resources in the admin namespace
|
# routes with nested resources in the admin namespace
|
||||||
module ActionDispatch::Routing::UrlFor
|
module ActionDispatch::Routing::UrlFor
|
||||||
def resource_hierarchy_for(resource)
|
def resource_hierarchy_for(resource)
|
||||||
if polymorphic_mapping(resource)
|
resolve = resolve_for(resource)
|
||||||
resolve = polymorphic_mapping(resource).send(:eval_block, self, resource, {})
|
|
||||||
|
|
||||||
|
if resolve
|
||||||
if resolve.last.is_a?(Hash)
|
if resolve.last.is_a?(Hash)
|
||||||
[resolve.first, *resolve.last.values]
|
[resolve.first, *resolve.last.values]
|
||||||
else
|
else
|
||||||
@@ -16,6 +16,20 @@ module ActionDispatch::Routing::UrlFor
|
|||||||
end
|
end
|
||||||
|
|
||||||
def admin_polymorphic_path(resource, options = {})
|
def admin_polymorphic_path(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)
|
polymorphic_path([:admin, *resource_hierarchy_for(resource)], options)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def resolve_for(resource)
|
||||||
|
polymorphic_mapping(resource)&.send(:eval_block, self, resource, {})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -265,3 +265,23 @@ end
|
|||||||
resolve "Audit" do |audit|
|
resolve "Audit" do |audit|
|
||||||
[*resource_hierarchy_for(audit.associated || audit.auditable), audit]
|
[*resource_hierarchy_for(audit.associated || audit.auditable), audit]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resolve "Budget::Group" do |group, options|
|
||||||
|
[group.budget, :group, options.merge(id: group)]
|
||||||
|
end
|
||||||
|
|
||||||
|
resolve "Budget::Heading" do |heading, options|
|
||||||
|
[heading.budget, :group, :heading, options.merge(group_id: heading.group, id: heading)]
|
||||||
|
end
|
||||||
|
|
||||||
|
resolve "Poll::Booth" do |booth, options|
|
||||||
|
[:booth, options.merge(id: booth)]
|
||||||
|
end
|
||||||
|
|
||||||
|
resolve "Poll::Officer" do |officer, options|
|
||||||
|
[:officer, options.merge(id: officer)]
|
||||||
|
end
|
||||||
|
|
||||||
|
resolve "Poll::Question::Answer::Video" do |video, options|
|
||||||
|
[:video, options.merge(id: video)]
|
||||||
|
end
|
||||||
|
|||||||
@@ -50,6 +50,56 @@ describe "Polymorphic routes" do
|
|||||||
describe "admin_polymorphic_path" do
|
describe "admin_polymorphic_path" do
|
||||||
include ActionDispatch::Routing::UrlFor
|
include ActionDispatch::Routing::UrlFor
|
||||||
|
|
||||||
|
it "routes budget investments" do
|
||||||
|
budget = create(:budget)
|
||||||
|
investment = create(:budget_investment, budget: budget)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(investment)).to eq(
|
||||||
|
admin_budget_budget_investment_path(budget, investment)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes budget groups" do
|
||||||
|
budget = create(:budget)
|
||||||
|
group = create(:budget_group, budget: budget)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(group)).to eq(admin_budget_group_path(budget, group))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes budget headings" do
|
||||||
|
budget = create(:budget)
|
||||||
|
group = create(:budget_group, budget: budget)
|
||||||
|
heading = create(:budget_heading, group: group)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(heading)).to eq(
|
||||||
|
admin_budget_group_heading_path(budget, group, heading)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes poll booths" do
|
||||||
|
booth = create(:poll_booth)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(booth)).to eq(admin_booth_path(booth))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes poll officers" do
|
||||||
|
officer = create(:poll_officer)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(officer)).to eq admin_officer_path(officer)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes poll questions" do
|
||||||
|
question = create(:poll_question)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(question)).to eq(admin_question_path(question))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "routes poll answer videos" do
|
||||||
|
video = create(:poll_answer_video)
|
||||||
|
|
||||||
|
expect(admin_polymorphic_path(video)).to eq admin_video_path(video)
|
||||||
|
end
|
||||||
|
|
||||||
it "routes milestones for resources with no hierarchy" do
|
it "routes milestones for resources with no hierarchy" do
|
||||||
proposal = create(:proposal)
|
proposal = create(:proposal)
|
||||||
milestone = create(:milestone, milestoneable: proposal)
|
milestone = create(:milestone, milestoneable: proposal)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ describe "Admin edit translatable records" do
|
|||||||
context "Add a translation", :js do
|
context "Add a translation", :js do
|
||||||
context "Input fields" do
|
context "Input fields" do
|
||||||
let(:translatable) { create(:budget_heading) }
|
let(:translatable) { create(:budget_heading) }
|
||||||
let(:path) { edit_admin_budget_group_heading_path(translatable.budget, translatable.group, translatable) }
|
let(:path) { admin_polymorphic_path(translatable, action: :edit) }
|
||||||
|
|
||||||
scenario "Maintains existing translations" do
|
scenario "Maintains existing translations" do
|
||||||
visit path
|
visit path
|
||||||
|
|||||||
Reference in New Issue
Block a user