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

ability to customize rendering of attributes via a block #54

Merged
merged 12 commits into from
Feb 9, 2018
Merged

ability to customize rendering of attributes via a block #54

merged 12 commits into from
Feb 9, 2018

Conversation

christophersansone
Copy link
Contributor

Fixes #5. Solves the same problem as #49 but with a minimal code change. The serialization declaration is somewhat different as well.

Example:

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :release_year

  attribute :title_with_year do |record|
    "#{record.name} (#{record.release_year})"
  end
end

@minsikzzang
Copy link
Contributor

I prefer this solution than other one 👍 I might have to say the other solution could cause little bit of performance degradation.

@grossadamm grossadamm requested a review from shishirmk February 5, 2018 20:19
Copy link
Contributor

@grossadamm grossadamm left a comment

Choose a reason for hiding this comment

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

I also prefer this method, would there be confusion around attributes vs attribute for users of this library?

@christophersansone
Copy link
Contributor Author

@grossadamm Good question. Currently attribute and attributes are aliased to the same method, so they are completely interchangeable. AMS does it this way, and I have seen that in other gems too. It's really just syntactic sugar. This works too:

  attribute :name
  attributes :release_year do
    ...
  end

  attributes :actor_names, :starring do |record|
    record.actors.map(&:name).join(', ')
  end

Not sure why you would ever want to have multiple attributes evaluate to the same block, but it's possible... maybe if you're using Object#send...

@apneadiving
Copy link
Contributor

I like this implementation, makes complete sense since we try to avoid instantiating serializer objects. Maybe the attribute alias should be in another PR though

@shishirmk shishirmk changed the base branch from master to dev February 9, 2018 07:56
@shishirmk
Copy link
Collaborator

@christophersansone please resolve the conflicts so that I can merge this. Could you also add to documentation an example of how to use the custom attributes feature with blocks?

Thank you for being patient and helping with trying out all the different ideas.

@minsikzzang
Copy link
Contributor

minsikzzang commented Feb 9, 2018

I think you have to rebase on dev , not merging into your branch :)

@christophersansone
Copy link
Contributor Author

@shishirmk Ready to go!

@shishirmk shishirmk merged commit b30a53b into Netflix:dev Feb 9, 2018
@minsikzzang
Copy link
Contributor

👏

@haizop haizop mentioned this pull request Mar 13, 2018
andyjeffries pushed a commit to andyjeffries/fast_jsonapi that referenced this pull request May 25, 2018
* add hash benchmarking to performance tests

* Add missing attribute in README example

* Disable GC before doing performance test

* Enable oj to AM for fair benchmark test

* ability to customize rendering of attributes via a block

* fixed attribute render spec

* minimized specs to specifially test this feature

* Update README to include attribute definitions

* Fixed syntax error

* Fixed merge issues
@TrevorHinesley TrevorHinesley mentioned this pull request Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants