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

Add symbol support for Serializer.type method #1515

Merged
merged 1 commit into from
Feb 21, 2016

Conversation

groyoh
Copy link
Member

@groyoh groyoh commented Feb 13, 2016

The Serializer.type method now accepts symbol as paremeter:

class AuthorSerializer < ActiveModel::Serializer
  type :profile
end

I don't know what you guys think about this but I usually prefer to use symbol as much as possible and thus prefer to write type :profile than type 'profile'. Furthermore this change should not have any runtime impact.

I also refactored the tests to make it more DRY.

@groyoh groyoh force-pushed the symbol_type branch 3 times, most recently from d3d6656 to ce9d119 Compare February 13, 2016 13:45
@@ -17,7 +17,7 @@ module ClassMethods
# class AdminAuthorSerializer < ActiveModel::Serializer
# type 'authors'
def type(type)
self._type = type
self._type = type ? type.to_s : nil
Copy link
Member

Choose a reason for hiding this comment

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

why check if type is nil?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because of this, we might call serializer.type(nil) and then nil.to_s results in "". I tried self._type = type if type at first but then two specs were failling out of nowhere. It looked like something related to the cache but I could not really figure it out.

Copy link
Member

Choose a reason for hiding this comment

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

type && type.to_s

B mobile phone

On Feb 15, 2016, at 1:53 AM, Yohan Robert notifications@github.com wrote:

In lib/active_model/serializer/type.rb:

@@ -17,7 +17,7 @@ module ClassMethods
# class AdminAuthorSerializer < ActiveModel::Serializer
# type 'authors'
def type(type)

  •      self._type = type
    
  •      self._type = type ? type.to_s : nil
    
    Because of this, we might call serializer.type(nil) and then nil.to_s results in "". I tried self._type = type if type at first but then two specs were failling out of nowhere. It looked like something related to the cache but I could not really figure it out.


Reply to this email directly or view it on GitHub.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah right, it didn't come to my mind.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it.

@joaomdmoura
Copy link
Member

Nice @groyoh I like this, @bf4 made some great comments, glad that you took care of it.
Can you also update our docs to reflect this? I also prefer symbols.

@groyoh groyoh force-pushed the symbol_type branch 3 times, most recently from 067d00f to e1bdbbf Compare February 17, 2016 18:48
@groyoh
Copy link
Member Author

groyoh commented Feb 17, 2016

@joaomdmoura could you check if the doc I added sounds good to you?

The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
It either takes a `String` or `Symbol` as parameter.

Note: This method is useful only when using the `:json_api` adapter.
Copy link
Member

Choose a reason for hiding this comment

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

though, TODO: it should replace the json_key method and root option..

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there an open PR for this?

Copy link
Member

Choose a reason for hiding this comment

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

Not that I recall

@bf4
Copy link
Member

bf4 commented Feb 19, 2016

👍

The ActiveModel::Serializer.type method now accepts symbol as paremeter:
class AuthorSerializer < ActiveModel::Serializer
  type :profile
end
The test file for the type was also refactored.
@joaomdmoura
Copy link
Member

LGTM 👍

bf4 added a commit that referenced this pull request Feb 21, 2016
[FEATURE] Add symbol support for Serializer.type method
@bf4 bf4 merged commit 532828b into rails-api:master Feb 21, 2016
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.

None yet

3 participants