Skip to content

Commit a11b37b

Browse files
committed
Refactor petition create route
Using new_petition_path instead of create_petitions_path more accurately reflects where we're POSTing too. Also expand the routing tests to cover other routes and ensure that we don't accidentally start accepting RESTful routes that modify petitions.
1 parent 099b268 commit a11b37b

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

app/views/petitions/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<%= form_for @stage_manager.stage_object, :url => create_petitions_path do |f| %>
1+
<%= form_for @stage_manager.stage_object, url: new_petition_path do |f| %>
22
<%= render_petition_form @stage_manager, f %>
33
<% end %>

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
collection do
3232
get 'check'
3333
get 'check_results'
34-
post 'create', path: 'new', as: :create
34+
post 'new', action: 'create', as: nil
3535
end
3636

3737
member do

spec/routing/petitions_spec.rb

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,73 @@
11
require 'rails_helper'
22

33
RSpec.describe "routes for petitions", type: :routes do
4+
it "routes GET /petitions to petitions#index" do
5+
expect(get("/petitions")).to route_to(controller: "petitions", action: "index")
6+
expect(petitions_path).to eq("/petitions")
7+
end
8+
49
it "routes GET /petitions/new to petitions#new" do
5-
expect({:get => "/petitions/new"}).to route_to({:controller => "petitions", :action => "new"})
6-
expect(new_petition_path).to eq '/petitions/new'
10+
expect(get("/petitions/new")).to route_to(controller: "petitions", action: "new")
11+
expect(new_petition_path).to eq("/petitions/new")
712
end
813

914
it "routes POST /petitions/new to petitions#create" do
10-
expect({:post => "/petitions/new"}).to route_to({:controller => "petitions", :action => "create"})
11-
expect(create_petitions_path).to eq('/petitions/new')
15+
expect(post("/petitions/new")).to route_to(controller: "petitions", action: "create")
16+
expect(new_petition_path).to eq("/petitions/new")
17+
end
18+
19+
it "doesn't route POST /petitions" do
20+
expect(post("/petitions")).not_to be_routable
21+
end
22+
23+
it "routes GET /petitions/:id to petitions#show" do
24+
expect(get("/petitions/1")).to route_to(controller: "petitions", action: "show", id: "1")
25+
expect(petition_path("1")).to eq("/petitions/1")
26+
end
27+
28+
it "doesn't route GET /petitions/:id/edit" do
29+
expect(patch("/petitions/1/edit")).not_to be_routable
30+
end
31+
32+
it "doesn't route PATCH /petitions/:id" do
33+
expect(patch("/petitions/1")).not_to be_routable
34+
end
35+
36+
it "doesn't route PUT /petitions/:id" do
37+
expect(put("/petitions/1")).not_to be_routable
38+
end
39+
40+
it "doesn't route DELETE /petitions/:id" do
41+
expect(delete("/petitions/1")).not_to be_routable
42+
end
43+
44+
it "routes GET /petitions/check to petitions#check" do
45+
expect(get("/petitions/check")).to route_to(controller: "petitions", action: "check")
46+
expect(check_petitions_path).to eq("/petitions/check")
47+
end
48+
49+
it "routes GET /petitions/check_results to petitions#check_results" do
50+
expect(get("/petitions/check_results")).to route_to(controller: "petitions", action: "check_results")
51+
expect(check_results_petitions_path).to eq("/petitions/check_results")
52+
end
53+
54+
it "routes GET /petitions/:id/count to petitions#count" do
55+
expect(get("/petitions/1/count")).to route_to(controller: "petitions", action: "count", id: "1")
56+
expect(count_petition_path("1")).to eq("/petitions/1/count")
57+
end
58+
59+
it "routes GET /petitions/:id/thank-you to petitions#thank_you" do
60+
expect(get("/petitions/1/thank-you")).to route_to(controller: "petitions", action: "thank_you", id: "1")
61+
expect(thank_you_petition_path("1")).to eq("/petitions/1/thank-you")
62+
end
63+
64+
it "routes GET /petitions/:id/gathering-support to petitions#gathering_support" do
65+
expect(get("/petitions/1/gathering-support")).to route_to(controller: "petitions", action: "gathering_support", id: "1")
66+
expect(gathering_support_petition_path("1")).to eq("/petitions/1/gathering-support")
67+
end
68+
69+
it "routes GET /petitions/:id/moderation-info to petitions#moderation_info" do
70+
expect(get("/petitions/1/moderation-info")).to route_to(controller: "petitions", action: "moderation_info", id: "1")
71+
expect(moderation_info_petition_path("1")).to eq("/petitions/1/moderation-info")
1272
end
1373
end

0 commit comments

Comments
 (0)