Skip to content

Commit

Permalink
Interface improvements per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Apr 3, 2016
1 parent 57f62d1 commit 826c927
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# reified when subclassed to decorate a resource.
module ActiveModel
class Serializer
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include].freeze
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
extend ActiveSupport::Autoload
autoload :Adapter
autoload :Null
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(serializer, options = {})
end

def serializable_hash(options = nil)
options = super
options = serialization_options(options)

if serializer.respond_to?(:each)
serializable_hash_for_collection(options)
Expand Down
10 changes: 8 additions & 2 deletions lib/active_model_serializers/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def cached_name
@cached_name ||= self.class.name.demodulize.underscore
end

def serializable_hash(options = nil)
(options ||= {}).slice(*ActiveModel::Serializer::SERIALIZABLE_HASH_VALID_KEYS) # rubocop:disable Lint/UselessAssignment
# Subclasses that implement this method must first call
# options = serialization_options(options)
def serializable_hash(_options = nil)
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end

def as_json(options = nil)
Expand All @@ -41,6 +43,10 @@ def cache_check(serializer)

private

def serialization_options(options)
(options ||= {}).slice(*ActiveModel::Serializer::SERIALIZABLE_HASH_VALID_KEYS) # rubocop:disable Lint/UselessAssignment
end

def meta
instance_options.fetch(:meta, nil)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ActiveModelSerializers
module Adapter
class Json < Base
def serializable_hash(options = nil)
options = super
options = serialization_options(options)
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) }
transform_key_casing!(serialized_hash)
end
Expand Down
19 changes: 15 additions & 4 deletions test/adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ def setup
@adapter = ActiveModelSerializers::Adapter::Base.new(@serializer)
end

def test_serializable_ensures_options_is_a_hash
assert_equal({ only: [:name] }, @adapter.serializable_hash(only: [:name]))
assert_equal({}, @adapter.serializable_hash(nil))
assert_equal({}, @adapter.serializable_hash({}))
def test_serializable_hash_is_abstract_method
assert_raises(NotImplementedError) do
@adapter.serializable_hash(only: [:name])
end
end

def test_serialization_options_ensures_option_is_a_hash
adapter = Class.new(ActiveModelSerializers::Adapter::Base) do
def serializable_hash(options = nil)
serialization_options(options)
end
end.new(@serializer)
assert_equal({ only: [:name] }, adapter.serializable_hash(only: [:name]))
assert_equal({}, adapter.serializable_hash(nil))
assert_equal({}, adapter.serializable_hash({}))
end

def test_serializer
Expand Down

0 comments on commit 826c927

Please sign in to comment.