From d39a99d7705142bcfdc8d72e86ea47760bc7dbc9 Mon Sep 17 00:00:00 2001 From: Robin Daugherty Date: Sun, 12 Oct 2014 01:08:47 -0400 Subject: [PATCH] Add serialization_options Prior to 0.9, @options was used to see the options passed to to_json. For some reason, with 0.9 the options were no longer passed and were entirely inaccessible within a Serializer. This restores the access by placing it in an attribute named serializer_options. --- lib/active_model/serializable.rb | 4 ++-- lib/active_model/serializer.rb | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/active_model/serializable.rb b/lib/active_model/serializable.rb index 9a33d397..43950d66 100644 --- a/lib/active_model/serializable.rb +++ b/lib/active_model/serializable.rb @@ -9,11 +9,11 @@ module ActiveModel def as_json(options={}) instrument('!serialize') do if root = options.fetch(:root, json_key) - hash = { root => serializable_object } + hash = { root => serializable_object(options) } hash.merge!(serializable_data) hash else - serializable_object + serializable_object(options) end end end diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index bb0170a5..b2c7f3ad 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -273,7 +273,10 @@ end end] end + attr_accessor :serialization_options + def serializable_object(options={}) + self.serialization_options = options return @wrap_in_array ? [] : nil if @object.nil? hash = attributes hash.merge! associations