Skip to content

simple namespace implementation added #79

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

Merged
merged 7 commits into from
Jul 25, 2016

Conversation

tompesman
Copy link
Contributor

Only thing i've added is the options parameter to find_serializer_class_name method so that it can use the options[:namespace] parameter to prefix the class lookup: "#{options[:namespace]}::#{object.class.name}Serializer". The sub serializers are namespaced as well.

Usage:
JSONAPI::Serializer.serialize(object, namespace: ::MyApi)
or
JSONAPI::Serializer.serialize(object, namespace: '::MyApi')

No test or documentation at the moment, but if this PR is acceptable I can make them.

@fotinakis
Copy link
Owner

Nice! Quick questions:
Does the explicit serializer discovery not do what you want? https://github.com/fotinakis/jsonapi-serializers#explicit-serializer-discovery -- Perhaps you are trying to fix the API::V1/API::V2 problem? If so, then you'll need to make sure the tests cover the recursive include case, but this looks like a simple direction to me.

@tompesman
Copy link
Contributor Author

tompesman commented Jul 6, 2016

Explicit discovery only maps a model to a single serializer. My use case is an application with multiple API backends. So, yes, I'm solving a similar problem: API::A/API::B. I saw another PR which was a bit too complicated. I think this is less intrusive. 😃

Can you explain the recursive include case to me?

@fotinakis
Copy link
Owner

Sounds right! For the recursive case just make sure that included relationships also handle this correctly (it looks like you're passing it down the find_recursive_relationships correctly, but the test should make sure) — something like https://github.com/fotinakis/jsonapi-serializers/blob/v0.13.0/spec/serializer_spec.rb#L695

@tompesman
Copy link
Contributor Author

In which order should the serializer lookup work, we have:

  • jsonapi_serializer_class_name
  • namespace option
  • default

I think jsonapi_serializer_class_name and the namespace should be switched. Because jsonapi_serializer_class_name is fixed to a class and the namespace is an option. So a developer could for example use the option for a single API request and the jsonapi_serializer_class_name for the other calls.

@tompesman
Copy link
Contributor Author

Specs, Readme and 'lookup preference' updated.

@tompesman
Copy link
Contributor Author

Added fix to pass the namespace to the include serialization. Not sure how to test this correctly.

has_one :author
has_many :long_comments

def meta
Copy link
Owner

Choose a reason for hiding this comment

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

Drop these meta attributes, they're not doing anything as far as I can tell.

@fotinakis
Copy link
Owner

Slowly getting back to this, this looks good to me! One comment above about some cleanup and then good to go.

@tompesman
Copy link
Contributor Author

Updated the PR according to your feedback.

@fotinakis fotinakis merged commit 2943869 into fotinakis:master Jul 25, 2016
@fotinakis
Copy link
Owner

Released in gem v0.14.0, thanks for your contribution!

@tompesman
Copy link
Contributor Author

Awesome! Thank you for merging and releasing! 👍

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.

2 participants