Skip to content

Commit

Permalink
Prefix id params with the resource name (luckyframework#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjamin committed Jan 30, 2019
1 parent 207c0dd commit 87c84f0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### In master but no released (since v0.12)

- Prefix id params with the resource name [#659](https://github.com/luckyframework/lucky/issues/659)

- Added Action#url_without_query_params [#662](https://github.com/luckyframework/lucky/pull/662)

- Added `Lucky::AssetHelpers.load_manifest` so that API apps don't need a blank manifest to compile.
Expand Down
8 changes: 4 additions & 4 deletions spec/lucky/action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ describe Lucky::Action do
it "adds routes to the router" do
assert_route_added? Lucky::Route.new :get, "/tests", Tests::Index
assert_route_added? Lucky::Route.new :get, "/tests/new", Tests::New
assert_route_added? Lucky::Route.new :get, "/tests/:id/edit", Tests::Edit
assert_route_added? Lucky::Route.new :get, "/tests/:id", Tests::Show
assert_route_added? Lucky::Route.new :delete, "/tests/:id", Tests::Delete
assert_route_added? Lucky::Route.new :put, "/tests/:id", Tests::Update
assert_route_added? Lucky::Route.new :get, "/tests/:test_id/edit", Tests::Edit
assert_route_added? Lucky::Route.new :get, "/tests/:test_id", Tests::Show
assert_route_added? Lucky::Route.new :delete, "/tests/:test_id", Tests::Delete
assert_route_added? Lucky::Route.new :put, "/tests/:test_id", Tests::Update
assert_route_added? Lucky::Route.new :post, "/tests", Tests::Create
end

Expand Down
2 changes: 1 addition & 1 deletion spec/lucky/namespaced_action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe Lucky::Action do
end

it "adds routes to the router" do
assert_route_added? Lucky::Route.new :get, "/admin/multi_word/users/:id", Admin::MultiWord::Users::Show
assert_route_added? Lucky::Route.new :get, "/admin/multi_word/users/:user_id", Admin::MultiWord::Users::Show
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lucky/nested_resource_action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe Lucky::Action do
end

it "adds routes to the router" do
assert_route_added? Lucky::Route.new :get, "/projects/:project_id/tasks/:id", Projects::Tasks::Show
assert_route_added? Lucky::Route.new :get, "/admin/projects/:project_id/tasks/:id", Admin::Projects::Tasks::Show
assert_route_added? Lucky::Route.new :get, "/projects/:project_id/tasks/:task_id", Projects::Tasks::Show
assert_route_added? Lucky::Route.new :get, "/admin/projects/:project_id/tasks/:task_id", Admin::Projects::Tasks::Show
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions src/run_macros/infer_route.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ class InferRoute
when "new"
[resource, "new"]
when "edit"
[resource, ":id", "edit"]
[resource, resource_id_param_name, "edit"]
when "show", "update", "delete"
[resource, ":id"]
[resource, resource_id_param_name]
else
resource_error
end
end

private def resource_id_param_name
":#{Wordsmith::Inflector.singularize(resource)}_id"
end

private def resource_error
examples = "Users::Index # Index, Show, New, Create, Edit, Update, or Delete"

Expand Down

0 comments on commit 87c84f0

Please sign in to comment.