-
Notifications
You must be signed in to change notification settings - Fork 77
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
Allows for conditionally hiding fields #126
Allows for conditionally hiding fields #126
Conversation
Why: * It is useful to be able to show or hide fields based on some condition on the data being passed to the serializer This change addresses the need by: * Passing `data` to the `hidden/1` function * Deprecating the `hidden/0` function with a warning, since it needs to be changed in all the serializers that use it. It keeps the existing behavior if a `hidden/0` function is found, so not to break any application
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.
One comment, otherwise this looks great!
lib/jsonapi/view.ex
Outdated
@@ -107,7 +107,15 @@ defmodule JSONAPI.View do | |||
end | |||
|
|||
def attributes(data, conn) do | |||
visible_fields = fields() -- hidden() | |||
hidden = | |||
if __MODULE__.__info__(:functions) |> Enum.member?({:hidden, 0}) 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.
I'd rather we avoid single pipes like this: https://github.com/christopheradams/elixir_style_guide#avoid-single-pipelines
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.
Sure, I tend to use only when the first argument is a bit long, but I don't have strong feelings about it
Why:
on the data being passed to the serializer
This change addresses the need by:
data
to thehidden/1
functionhidden/0
function with a warning, since it needs tobe changed in all the serializers that use it. It keeps the existing
behavior if a
hidden/0
function is found, so not to break anyapplication