Skip to content
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

Merged

Conversation

zamith
Copy link
Contributor

@zamith zamith commented Aug 29, 2018

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

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
@zamith zamith changed the title Allows for conditionally hidding fields Allows for conditionally hiding fields Aug 29, 2018
Copy link
Member

@doomspork doomspork left a 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!

@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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

@doomspork doomspork merged commit c3d751e into beam-community:master Aug 29, 2018
@zamith zamith deleted the zamith/conditional-hidden-fields branch September 12, 2018 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants