mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #410 from arthurnn/settings_warn
Deprecate embed method on Serializer class
This commit is contained in:
commit
5b727cc291
@ -20,6 +20,15 @@ module ActiveModel
|
||||
def embed(type, options={})
|
||||
SETTINGS[:embed] = type
|
||||
SETTINGS[:include] = true if options[:include]
|
||||
warn <<-WARN
|
||||
** 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.
|
||||
Please use the global .setup method instead:
|
||||
ActiveModel::Serializer.setup do |config|
|
||||
config.embed = :#{type}
|
||||
config.include = #{SETTINGS[:include] || false}
|
||||
end
|
||||
WARN
|
||||
end
|
||||
|
||||
if RUBY_VERSION >= '2.0'
|
||||
|
||||
@ -1,16 +1,18 @@
|
||||
require 'active_support/hash_with_indifferent_access'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Settings
|
||||
def initialize
|
||||
@data = {}
|
||||
@data = ActiveSupport::HashWithIndifferentAccess.new
|
||||
end
|
||||
|
||||
def [](key)
|
||||
@data[key.to_s]
|
||||
@data[key]
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
@data[key.to_s] = value
|
||||
@data[key] = value
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
@ -20,6 +22,17 @@ module ActiveModel
|
||||
def clear
|
||||
@data.clear
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
return @data[name] if @data.include?(name)
|
||||
match = name.to_s.match(/(.*?)([?=]?)$/)
|
||||
case match[2]
|
||||
when "="
|
||||
@data[match[1]] = args.first
|
||||
when "?"
|
||||
!!@data[match[1]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
SETTINGS = Settings.new
|
||||
|
||||
@ -43,6 +43,25 @@ module ActiveModel
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user