mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Rename Settings to Config and use accessors to configure things
This commit is contained in:
parent
5b727cc291
commit
7dcef9f0f5
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
* The following things were added
|
* The following things were added
|
||||||
- Serializer#filter method
|
- Serializer#filter method
|
||||||
- SETTINGS object
|
- CONFIG object
|
||||||
|
|
||||||
* Remove support for ruby 1.8 versions.
|
* Remove support for ruby 1.8 versions.
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
require 'active_model/array_serializer'
|
require 'active_model/array_serializer'
|
||||||
require 'active_model/serializable'
|
require 'active_model/serializable'
|
||||||
require 'active_model/serializer/associations'
|
require 'active_model/serializer/associations'
|
||||||
require 'active_model/serializer/settings'
|
require 'active_model/serializer/config'
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
@ -14,19 +14,19 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
yield SETTINGS
|
yield CONFIG
|
||||||
end
|
end
|
||||||
|
|
||||||
def embed(type, options={})
|
def embed(type, options={})
|
||||||
SETTINGS[:embed] = type
|
CONFIG.embed = type
|
||||||
SETTINGS[:include] = true if options[:include]
|
CONFIG.include = true if options[:include]
|
||||||
warn <<-WARN
|
warn <<-WARN
|
||||||
** Notice: embed is deprecated. **
|
** Notice: embed is deprecated. **
|
||||||
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
|
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
|
||||||
Please use the global .setup method instead:
|
Please use the global .setup method instead:
|
||||||
ActiveModel::Serializer.setup do |config|
|
ActiveModel::Serializer.setup do |config|
|
||||||
config.embed = :#{type}
|
config.embed = :#{type}
|
||||||
config.include = #{SETTINGS[:include] || false}
|
config.include = #{CONFIG.include || false}
|
||||||
end
|
end
|
||||||
WARN
|
WARN
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,8 +8,8 @@ module ActiveModel
|
|||||||
@name = name.to_s
|
@name = name.to_s
|
||||||
@options = options
|
@options = options
|
||||||
|
|
||||||
self.embed = options[:embed] || SETTINGS[:embed] || :objects
|
self.embed = options[:embed] || CONFIG.embed || :objects
|
||||||
@embed_in_root = @embed_ids && (options[:include] || SETTINGS[:include])
|
@embed_in_root = @embed_ids && (options[:include] || CONFIG.include)
|
||||||
@embed_key = options[:embed_key] || :id
|
@embed_key = options[:embed_key] || :id
|
||||||
@key = options[:key]
|
@key = options[:key]
|
||||||
@embedded_key = options[:root] || name
|
@embedded_key = options[:root] || name
|
||||||
|
|||||||
@ -1,18 +1,16 @@
|
|||||||
require 'active_support/hash_with_indifferent_access'
|
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class Settings
|
class Config
|
||||||
def initialize
|
def initialize
|
||||||
@data = ActiveSupport::HashWithIndifferentAccess.new
|
@data = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](key)
|
def [](key)
|
||||||
@data[key]
|
@data[key.to_s]
|
||||||
end
|
end
|
||||||
|
|
||||||
def []=(key, value)
|
def []=(key, value)
|
||||||
@data[key] = value
|
@data[key.to_s] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
@ -24,8 +22,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(name, *args)
|
def method_missing(name, *args)
|
||||||
|
name = name.to_s
|
||||||
return @data[name] if @data.include?(name)
|
return @data[name] if @data.include?(name)
|
||||||
match = name.to_s.match(/(.*?)([?=]?)$/)
|
match = name.match(/\A(.*?)([?=]?)\Z/)
|
||||||
case match[2]
|
case match[2]
|
||||||
when "="
|
when "="
|
||||||
@data[match[1]] = args.first
|
@data[match[1]] = args.first
|
||||||
@ -35,6 +34,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
SETTINGS = Settings.new
|
CONFIG = Config.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
67
test/unit/active_model/serializer/config_test.rb
Normal file
67
test/unit/active_model/serializer/config_test.rb
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class Config
|
||||||
|
class Test < ActiveModel::TestCase
|
||||||
|
def test_config_const_is_an_instance_of_config
|
||||||
|
assert_kind_of Config, CONFIG
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_config_instance
|
||||||
|
config = Config.new
|
||||||
|
config.setting1 = 'value1'
|
||||||
|
|
||||||
|
assert_equal 'value1', config.setting1
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_each_config
|
||||||
|
config = Config.new
|
||||||
|
config.setting1 = 'value1'
|
||||||
|
config.setting2 = 'value2'
|
||||||
|
|
||||||
|
actual = {}
|
||||||
|
|
||||||
|
config.each do |k, v|
|
||||||
|
actual[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SetupTest < ActiveModel::TestCase
|
||||||
|
def test_setup
|
||||||
|
ActiveModel::Serializer.setup do |config|
|
||||||
|
config.a = 'v1'
|
||||||
|
config.b = 'v2'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 'v1', CONFIG.a
|
||||||
|
assert_equal 'v2', CONFIG.b
|
||||||
|
ensure
|
||||||
|
CONFIG.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setup_config_accessors
|
||||||
|
ActiveModel::Serializer.setup do |config|
|
||||||
|
config.foo = 'v1'
|
||||||
|
config.bar = 'v2'
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 'v1', CONFIG.foo
|
||||||
|
assert_equal 'v2', CONFIG.bar
|
||||||
|
ensure
|
||||||
|
CONFIG.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_setup_acessor_when_nil
|
||||||
|
assert_nil CONFIG.foo
|
||||||
|
CONFIG.foo = 1
|
||||||
|
assert 1, CONFIG.foo
|
||||||
|
assert_nil CONFIG.bar
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -93,7 +93,7 @@ module ActiveModel
|
|||||||
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
ensure
|
ensure
|
||||||
SETTINGS.clear
|
CONFIG.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_using_a_given_serializer
|
def test_associations_using_a_given_serializer
|
||||||
|
|||||||
@ -1,68 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module ActiveModel
|
|
||||||
class Serializer
|
|
||||||
class Settings
|
|
||||||
class Test < ActiveModel::TestCase
|
|
||||||
def test_settings_const_is_an_instance_of_settings
|
|
||||||
assert_kind_of Settings, SETTINGS
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_settings_instance
|
|
||||||
settings = Settings.new
|
|
||||||
settings[:setting1] = 'value1'
|
|
||||||
|
|
||||||
assert_equal 'value1', settings[:setting1]
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_each_settings
|
|
||||||
settings = Settings.new
|
|
||||||
settings['setting1'] = 'value1'
|
|
||||||
settings['setting2'] = 'value2'
|
|
||||||
|
|
||||||
actual = {}
|
|
||||||
|
|
||||||
settings.each do |k, v|
|
|
||||||
actual[k] = v
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual)
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class SetupTest < ActiveModel::TestCase
|
|
||||||
def test_setup
|
|
||||||
ActiveModel::Serializer.setup do |settings|
|
|
||||||
settings[:a] = 'v1'
|
|
||||||
settings[:b] = 'v2'
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal 'v1', SETTINGS[:a]
|
|
||||||
assert_equal 'v2', SETTINGS[:b]
|
|
||||||
ensure
|
|
||||||
SETTINGS.clear
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_setup_config_accessors
|
|
||||||
ActiveModel::Serializer.setup do |config|
|
|
||||||
config.foo = 'v1'
|
|
||||||
config.bar = 'v2'
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal 'v1', SETTINGS.foo
|
|
||||||
assert_equal 'v2', SETTINGS.bar
|
|
||||||
ensure
|
|
||||||
SETTINGS.clear
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_setup_acessor_when_nil
|
|
||||||
assert_nil SETTINGS.foo
|
|
||||||
SETTINGS.foo = 1
|
|
||||||
assert 1, SETTINGS.foo
|
|
||||||
assert_nil SETTINGS.bar
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Loading…
Reference in New Issue
Block a user