Skip to content

Commit

Permalink
Make serializers serializable, step 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Feb 3, 2016
1 parent 6b50b5d commit d786cff
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ def initialize(object, options = {})
end
end

# @return [Hash] containing the attributes and first level
# associations, similar to how ActiveModel::Serializers::JSON is used
# in ActiveRecord::Base.
#
# @param options [nil, Hash] The standard options passed to `as_json`.
# See
# https://github.com/rails/rails/blob/v5.0.0.beta2/activemodel/lib/active_model/serializers/json.rb#L17-L101
# https://github.com/rails/rails/blob/v5.0.0.beta2/activemodel/lib/active_model/serialization.rb#L85-L123
# https://github.com/rails/rails/blob/v5.0.0.beta2/activesupport/lib/active_support/core_ext/object/json.rb#L147-L162
#
# TODO: Move to here the Attributes adapter logic for
# +serializable_hash_for_single_resource(options)+
def serializable_hash(options = nil)
options ||= {}
serializable = ActiveModel::SerializableResource.new(object, serializer: self, adapter: :attributes, include: '*')
serializable.serializable_hash(options)
end
alias to_hash serializable_hash
alias to_h serializable_hash

# @see #serializable_hash
def as_json(options = nil)
serializable_hash(options)
end

# Used by adapter as resource root.
def json_key
root || object.class.model_name.to_s.underscore
Expand Down

0 comments on commit d786cff

Please sign in to comment.