Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Ability to specify name of resource id #22

Merged
merged 7 commits into from
Nov 20, 2015
Merged

Ability to specify name of resource id #22

merged 7 commits into from
Nov 20, 2015

Conversation

timoteus
Copy link
Contributor

Instead of assuming resource id is named "id" this PR let's you override it and specify the name of the resource id while defaulting to "id" if none is specified.

Use it like so:

 plug :load_resource, model: User, id: "user_id", only: :show

conn.params["id"]
resource_id ->
conn.params[resource_id]
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really minor thing, but I think this block could be moved into a small function e.g.

id = get_resource_id(opts)

defp get_resource_id(opts) do
  case opts[:id] do
    nil ->
      conn.params["id"]
    resource_id ->
      conn.params[resource_id]
    end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! Love it, see pushed commits.

@cpjk
Copy link
Owner

cpjk commented Nov 18, 2015

Just a few small comments! 😃

timoteus added 2 commits November 18, 2015 10:54
@@ -103,6 +103,17 @@ defmodule PlugTest do
assert load_resource(conn, opts) == expected
end

test "it loads the resource correctly with opts[:id] specified" do
opts = [model: Post, id: "user_id"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you wanna call it id_name instead of just id to go with resource_name and not confuse with the actual id?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking that actually! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, all done!

To go with resource_name and not confuse with the actual id.
@@ -222,26 +225,37 @@ defmodule Canary.Plugs do
defp fetch_resource(conn, opts) do
repo = Application.get_env(:canary, :repo)

id_name = get_resource_id_name(conn, opts)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to keep nitpicking on this, but while the opts key should be id_name, I think the variable on this line should just be id, and the function should be get_resource_id rather than get_resource_id_name, since we are really just getting the id.

That's my fault: I should have been more clear in my last comment. Other than that, I think this looks good!

So, the opts key is id_name, but this variable on line 228 should just be id, and the function should be get_resource_id.

Let me know if you disagree! 😸

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry mate, I was too quick searching and replacing, I fully agree seeing as the variable on 228 isn't the name of the id parameter but the actual id 🙈

Let me fix it real quick.

Use id for the id and id_name for the name of the id parameter..
@timoteus
Copy link
Contributor Author

✌️

I see you added Travis integration! Great!

cpjk added a commit that referenced this pull request Nov 20, 2015
Add ability to specify name of resource id
@cpjk cpjk merged commit 69badce into cpjk:master Nov 20, 2015
@cpjk
Copy link
Owner

cpjk commented Nov 20, 2015

Beauty! Merged 👌

@@ -103,6 +103,17 @@ defmodule PlugTest do
assert load_resource(conn, opts) == expected
end

test "it loads the resource correctly with opts[:id_name] specified" do
opts = [model: Post, id_name: "user_id"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably have been post_id though ☺️

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha good catch

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants