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

Respect namespacing in SerializerResolver #93

Merged
merged 1 commit into from
Feb 15, 2021

Conversation

casperisfine
Copy link

Context

I'm trying to convert one of our app that uses AMS 0.9 (Shopify/shipit-engine#1139), and one small issue I ran into is that Shipit is a Rails engine, as such all constants are namespaced under Shipit::.

So none of the automatic serializer resolution worked because it would look for e.g. ::UserSerializer instead of Shipit::UserSerializer.

This PR

We simply first look for a serializer inside the namespace we're defining the relation, if not found we fallback to the top level namespace.

PS: thanks for the two very quick merges!

def self.resolve(name)
serializer_name = "#{name.singularize.camelize}Serializer"
serializer_const = safe_const_get(serializer_name)
class << self
Copy link
Author

Choose a reason for hiding this comment

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

I took the liverty to convert to class << self, as it makes it easier to have private class method. Previously is_serializer and safe_const_get were actually public.

def self.is_serializer(const)
const < Panko::Serializer
end
if Module.method_defined?(:module_parent_name)
Copy link
Author

Choose a reason for hiding this comment

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

Rails 6.0 renamed Module#parent_name into Module#module_parent_name, so to support Rails 5.2 we need to do some feature checking.

@casperisfine
Copy link
Author

Hum, hold on, there's some issue with the feature testing.

@@ -1,25 +1,38 @@
# frozen_string_literal: true

require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/module/introspection'
Copy link
Author

Choose a reason for hiding this comment

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

This is the two AS features used by this file.

@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
spec.extensions << "ext/panko_serializer/extconf.rb"

spec.add_dependency "oj", "~> 3.10.0"
spec.add_dependency "activesupport"
Copy link
Author

Choose a reason for hiding this comment

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

Since the resolver uses a bunch of ActiveSupport core_ext, it's better to add it to the dependencies.

Copy link
Owner

Choose a reason for hiding this comment

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

It was always a question for me, whether I want a direct dependency on rails for Panko since it can serialize plain ruby objects. but to be honest with me, it's 90% of the project, so ok. 👍

Copy link
Author

Choose a reason for hiding this comment

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

Up to you, we can also try to eliminate the dependency.

camelize for instance is very easy to get rid of, singularize however it's quite trickier.

But maybe there's a way to have this code only called in Rails context? I don't know.

Copy link
Owner

@yosiat yosiat left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks for your contribution!

Let me know if you want me to release a version or wait.

@yosiat yosiat merged commit 15d3d03 into yosiat:master Feb 15, 2021
@casperisfine
Copy link
Author

Let me know if you want me to release a version

That would be awesome, as I have Shopify/shipit-engine#1139 green, but I can't ship it using a git gem.

@yosiat
Copy link
Owner

yosiat commented Feb 15, 2021

I'll wait for the CI to pass on latest master (pushed version bump for Oj), once it's passing I'll release 0.7.5.

@yosiat
Copy link
Owner

yosiat commented Feb 15, 2021

@casperisfine Thanks for all of your contributions! released 0.7.5

https://github.com/panko-serializer/panko_serializer/releases/tag/v0.7.5

@casperisfine
Copy link
Author

Welcome, thanks for the merges and the release!

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.

None yet

3 participants