mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Add @options back into serializers to maintain the functionality of render :json as noted in the docs
This functionality used to exist in v0.8.1. This adds the ability to pass other options that will be usable in the serializer via the options accessor. This works by adding an attr_accessor for options so it is available and is set by the remaining options in the provided options hash during initialization.
This commit is contained in:
parent
127413ffde
commit
a110df6caf
@ -20,7 +20,7 @@ module ActiveModel
|
|||||||
@each_serializer = options[:each_serializer]
|
@each_serializer = options[:each_serializer]
|
||||||
@options = options.merge(root: nil)
|
@options = options.merge(root: nil)
|
||||||
end
|
end
|
||||||
attr_accessor :object, :root, :meta_key, :meta
|
attr_accessor :object, :root, :meta_key, :meta, :options
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root.nil?
|
if root.nil?
|
||||||
|
|||||||
@ -106,8 +106,9 @@ end
|
|||||||
@root = options.fetch(:root, self.class._root)
|
@root = options.fetch(:root, self.class._root)
|
||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
|
@options = options.reject{|k,v| [:scope, :root, :meta_key, :meta].include?(k) }
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :meta_key, :meta, :root
|
attr_accessor :object, :scope, :meta_key, :meta, :root, :options
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
if root == true || root.nil?
|
if root == true || root.nil?
|
||||||
|
|||||||
17
test/unit/active_model/serializer/options_test.rb
Normal file
17
test/unit/active_model/serializer/options_test.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class OptionsTest < ActiveModel::TestCase
|
||||||
|
def setup
|
||||||
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_meta
|
||||||
|
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', random_option: "This is an option")
|
||||||
|
|
||||||
|
assert_equal("This is an option", profile_serializer.options[:random_option])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user