hookercookerman / active_model_version_serializers Goto Github PK
View Code? Open in Web Editor NEWVersioning For ActiveModel::Serializer
License: MIT License
Versioning For ActiveModel::Serializer
License: MIT License
Hi,
I'm using a namespaced route to version my API:
namespace :api do
namespace :v1 do
end
end
I already configured my Serializer with this gem but the documentation is not clear enough and I don't know where to specify the serializer version dynamically.
I want them to load the right version based on the namespace 'v1, v2...'. How can I do that?
I have it working fine on my own dev box, and it responds with different versions just fine. When I push it to Heroku it's only using v1, even though v2 is the default.
Not sure if it's me or if there is Heroku caching issues. Anyone try that yet?
There is an issue when root element is configured this way :
ActiveSupport.on_load(:active_model_serializers) do
# Disable for all serializers (except ArraySerializer)
ActiveModel::Serializer.root = false
# Disable for ArraySerializer
ActiveModel::ArraySerializer.root = false
end
Actually the gem is doing that :
def self.version(version, superclass = ActiveModel::Serializer, &block)
base_class = self
vklass = Class.new(superclass) do
self.root(base_class._root)
alias_method base_class._name.to_sym, :object
singleton_class.class_eval do
define_method(:to_s) do
"(#{base_class.name} VERSION: #{version})"
end
alias inspect to_s
end
It sets root (or not) directly based on parameters passed to the serializer :
[1] poc_api_app(#<Class>) » superclass
=> ActiveModel::Serializer < Object
[2] poc_api_app(#<Class>) » superclass._root?
=> false
[3] poc_api_app(#<Class>) » base_class
=> UserSerializer < ActiveModel::VersionSerializer
[4] poc_api_app(#<Class>) » base_class._root?
=> true
You can notice that it doesn't use my configuration and still includes the root element because it doesn't check if superclass._root?
is false
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.