-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add to view custom attribute close #89 #92
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snewcomer this is a good first go, I've left some feedback and questions. I do think we should think about whether we want to be passing conn
into the functions, that opens the door for misuse and confusing behavior.
Let's think about it a bit and give @jeregrine a chance to share his thoughts 👍
lib/jsonapi/view.ex
Outdated
end) | ||
end | ||
|
||
defp merge(intermediate_map, field, value) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does merge/3
offer us over just using Map.put/3
directly?
visible_fields(data) | ||
visible_fields = fields() -- hidden() | ||
|
||
Enum.reduce(visible_fields, %{}, fn field, intermediate_map -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why reduce/3
vs map/2
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was to ensure the return type was a Map, which is what Map.take
was doing before.
lib/jsonapi/view.ex
Outdated
|
||
Enum.reduce(visible_fields, %{}, fn field, intermediate_map -> | ||
try do | ||
merge(intermediate_map, field, apply(__MODULE__, field, [data, conn])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do something like this to DRY things up a bit?
value =
try do
apply(__MODULE__, field, [data, conn])
rescue
_e -> Map.get(data, field)
end
Map.put(intermediate_map, field, value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah much better 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@doomspork Good calls. So the only reason to also pass conn
would be to get at the current user
or something like that. So if your API is returning a list of users, for every user you could ensure that only the current logged in user's email was sent back. Lmk your thoughts!
visible_fields(data) | ||
visible_fields = fields() -- hidden() | ||
|
||
Enum.reduce(visible_fields, %{}, fn field, intermediate_map -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was to ensure the return type was a Map, which is what Map.take
was doing before.
lib/jsonapi/view.ex
Outdated
|
||
Enum.reduce(visible_fields, %{}, fn field, intermediate_map -> | ||
try do | ||
merge(intermediate_map, field, apply(__MODULE__, field, [data, conn])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah much better 💯
Thank you @snewcomer! I want to review #87 and then we can cut a new release 😁 |
Ref #89