active_model_serializers
https://github.com/rails-api/active_model_serializers
Ruby
ActiveModel::Serializer implementation and Rails hooks
ActiveModel::Serializer#to_hash
associations, similar to how ActiveModel::Serializers::JSON is used
in ActiveRecord::Base.
TODO: Move to here the Attributes adapter logic for
+serializable_hash_for_single_resource(options)+
and include <tt>ActiveModel::Serializers::JSON</tt>.
So that the below is true:
@param options [nil, Hash] The same valid options passed to `serializable_hash`
(:only, :except, :methods, and :include).
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/activerecord/lib/active_record/serialization.rb#L11-L17
https://github.com/rails/rails/blob/v5.0.0.beta2/activesupport/lib/active_support/core_ext/object/json.rb#L147-L162
@example
# The :only and :except options can be used to limit the attributes included, and work
# similar to the attributes method.
serializer.as_json(only: [:id, :name])
serializer.as_json(except: [:id, :created_at, :age])
# To include the result of some method calls on the model use :methods:
serializer.as_json(methods: :permalink)
# To include associations use :include:
serializer.as_json(include: :posts)
# Second level and higher order associations work as well:
serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })Edit
git clone [email protected]:rails-api/active_model_serializers.git
cd active_model_serializers
open lib/active_model/serializer.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-ActiveModel--Serializer-to_hash-for-pr
# Commit to gitgit add lib/active_model/serializer.rbgit commit -m "better docs for ActiveModel::Serializer#to_hash"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-ActiveModel--Serializer-to_hash-for-pr
hub pull-request
# Celebrate!