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

serializable_hash and as_json should take options = nil #965

Merged
merged 1 commit into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def initialize(serializer, options = {})
@options = options
end

def serializable_hash(options = {})
def serializable_hash(options = nil)
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end

def as_json(options = {})
def as_json(options = nil)
hash = serializable_hash(options)
include_meta(hash) unless self.class == FlattenJson
hash
Expand Down
5 changes: 3 additions & 2 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ module ActiveModel
class Serializer
class Adapter
class Json < Adapter
def serializable_hash(options = {})
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash }
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash(options) }
else
@hash = {}

Expand Down
7 changes: 4 additions & 3 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ def initialize(serializer, options = {})
end
end

def serializable_hash(options = {})
def serializable_hash(options = nil)
options = {}
Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @bacarini for finding this bug in #1041, should be options ||= {}
:person_frowning: on me

if serializer.respond_to?(:each)
serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
@hash[:data] << result[:data]

if result[:included]
Expand All @@ -27,7 +28,7 @@ def serializable_hash(options = {})
end
end
else
@hash[:data] = attributes_for_serializer(serializer, @options)
@hash[:data] = attributes_for_serializer(serializer, options)
add_resource_relationships(@hash[:data], serializer)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note there is some confusion in the method here between the options to the serializable_hash and the @options to the adapter, which I have resolved.

end
@hash
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ActiveModel
class Serializer
class Adapter
class Null < Adapter
def serializable_hash(options = {})
def serializable_hash(options = nil)
{}
end
end
Expand Down