Skip to content

Commit

Permalink
Restrict serializable_hash to accepted options
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Apr 1, 2016
1 parent 96c5516 commit 691a596
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# reified when subclassed to decorate a resource.
module ActiveModel
class Serializer
SERIALIZABLE_HASH_VALID_KEYS = Set.new([:only, :except, :methods, :include])
extend ActiveSupport::Autoload
autoload :Adapter
autoload :Null
Expand Down Expand Up @@ -164,7 +165,7 @@ def serializable_hash(adapter_opts = nil)
adapter_opts ||= {}
adapter_opts = { include: '*', adapter: :attributes }.merge!(adapter_opts)
adapter = ActiveModelSerializers::Adapter.create(self, adapter_opts)
adapter.serializable_hash(adapter_opts)
adapter.serializable_hash(adapter_opts.slice(SERIALIZABLE_HASH_VALID_KEYS))
end
alias to_hash serializable_hash
alias to_h serializable_hash
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 ||= {}
options = super

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

def serializable_hash(_options = nil)
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
def serializable_hash(options = nil)
(options ||= {}).slice(ActiveModel::Serializer::SERIALIZABLE_HASH_VALID_KEYS)
end

def as_json(options = nil)
Expand Down Expand Up @@ -73,7 +73,8 @@ def key_transform(serialization_context)
default_key_transform
end

def transform_key_casing!(value, serialization_context)
def transform_key_casing!(value)
serialization_context = instance_options[:serialization_context]
return value unless serialization_context
transform = key_transform(serialization_context)
KeyTransform.send(transform, value)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_model_serializers/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module ActiveModelSerializers
module Adapter
class Json < Base
def serializable_hash(options = nil)
options ||= {}
options = super
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) }
transform_key_casing!(serialized_hash, options[:serialization_context])
transform_key_casing!(serialized_hash)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def default_key_transform
# {http://jsonapi.org/format/#crud Requests are transactional, i.e. success or failure}
# {http://jsonapi.org/format/#document-top-level data and errors MUST NOT coexist in the same document.}
def serializable_hash(options = nil)
options ||= {}
options = super
document = if serializer.success?
success_document(options)
else
failure_document
end
transform_key_casing!(document, options[:serialization_context])
transform_key_casing!(document)
end

# {http://jsonapi.org/format/#document-top-level Primary data}
Expand Down Expand Up @@ -499,7 +499,7 @@ def links_for(serializer)
# prs:
# https://github.com/rails-api/active_model_serializers/pull/1041
def pagination_links_for(serializer, options)
PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
PaginationLinks.new(serializer.object, options).as_json
end

# {http://jsonapi.org/format/#document-meta Docment Meta}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class PaginationLinks

attr_reader :collection, :context

def initialize(collection, context)
def initialize(collection, options)
@collection = collection
@context = context
@options = options
@context = options.fetch(:serialization_context)
end

def serializable_hash(options = {})
def as_json
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
pages_from.each_with_object({}) do |(key, value), hash|
params = query_parameters.merge(page: { number: value, size: per_page }).to_query
Expand All @@ -20,6 +21,10 @@ def serializable_hash(options = {})
end
end

protected

attr_reader :options

private

def pages_from
Expand Down
3 changes: 1 addition & 2 deletions lib/active_model_serializers/adapter/null.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module ActiveModelSerializers
module Adapter
class Null < Base
# Since options param is not being used, underscored naming of the param
def serializable_hash(_options = nil)
def serializable_hash(*)
{}
end
end
Expand Down

0 comments on commit 691a596

Please sign in to comment.