-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Deprecate ActiveModelSerializers::Model #1911
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,10 +191,24 @@ def json_key | |
end | ||
|
||
def read_attribute_for_serialization(attr) | ||
# Start by favoring serializers method override | ||
if respond_to?(attr) | ||
send(attr) | ||
else | ||
object.read_attribute_for_serialization(attr) | ||
# Otherwise, check read_attribute_for_serialization | ||
if object.respond_to?(:read_attribute_for_serialization) | ||
object.read_attribute_for_serialization(attr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a huge philosophical break and also greatly expands the surface area that AMS needs to cover from a defined interface to a defined interface when available, else try to guess. I don't think we should support serializing primitives such as Hashes. |
||
else | ||
# Fall back to object property/method | ||
if object.respond_to?(attr) | ||
object.send(attr) | ||
else | ||
# Special logic for hashes | ||
if object.is_a?(Hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👎 |
||
object[attr] || object[attr.to_s] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,11 @@ | |
require 'active_support/core_ext/object/with_options' | ||
require 'active_support/core_ext/string/inflections' | ||
require 'active_support/json' | ||
|
||
require 'active_model_serializers/model_mixin' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 serializable |
||
|
||
require 'pry-byebug' | ||
|
||
module ActiveModelSerializers | ||
extend ActiveSupport::Autoload | ||
autoload :Model | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ActiveModelSerializers::Model is a convenient | ||
# serializable class to inherit from when making | ||
# serializable non-activerecord objects. | ||
module ActiveModelSerializers | ||
module ModelMixin | ||
def self.included(klass) | ||
klass.class_eval do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing call to |
||
include ActiveModel::Model | ||
include ActiveModel::Serializers::JSON | ||
|
||
def read_attribute_for_serialization(key) | ||
if key == :id || key == 'id' | ||
send(key) | ||
else | ||
if is_a?(ActiveModelSerializers::Model) | ||
# Support legacy behavior | ||
attributes[key] || (send(key) if respond_to?(key)) | ||
else | ||
send(key) if respond_to?(key) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
#def id | ||
#self.class.name.downcase | ||
#end | ||
|
||
#def cache_key | ||
#end | ||
|
||
#def errors | ||
#@errors ||= ActiveModel::Errors.new(self) | ||
#end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. obviously this is a wip, but can't must remove these. They're part of the contract |
||
|
||
#def updated_at | ||
#end | ||
|
||
# This is just to make lint pass | ||
# We shouldnt have this... | ||
#def attributes | ||
#{} | ||
#end | ||
|
||
# The following methods are needed to be minimally implemented for ActiveModel::Errors | ||
# :nocov: | ||
#def self.human_attribute_name(attr, _options = {}) | ||
#attr | ||
#end | ||
|
||
#def self.lookup_ancestors | ||
#[self] | ||
#end | ||
# :nocov: | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require 'test_helper' | ||
|
||
require 'pry-byebug' | ||
module ActionController | ||
module Serialization | ||
class JsonApi | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModelSerializers | ||
class ModelMixinTest < ActiveSupport::TestCase | ||
def setup | ||
@klass = Class.new do | ||
#include ActiveModelSerializers::ModelMixin | ||
attr_accessor :key, :id | ||
def self.name | ||
'TestModel' | ||
end | ||
end | ||
end | ||
|
||
def test_poro_serialize | ||
serializer = Class.new(ActiveModel::Serializer) do | ||
attributes :key | ||
end | ||
model_instance = @klass.new | ||
model_instance.key = 'value' | ||
|
||
json = serializer.new(model_instance, {}).as_json | ||
assert_equal({ key: 'value' }, json) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
require 'test_helper' | ||
|
||
require 'pry-byebug' | ||
|
||
module ActiveModelSerializers | ||
module Adapter | ||
class JsonApi | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like putting anything in the gemspec not required for running the tests. I'd rather this be proposed to the Gemfile in a separate PR. Since this has come up a couple of times and I basically have the same thing in my
Gemfile.local
I think it's worth addingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1913