Merge pull request #410 from arthurnn/settings_warn

Deprecate embed method on Serializer class
This commit is contained in:
Santiago Pastorino 2013-10-21 11:05:51 -07:00
commit 5b727cc291
3 changed files with 44 additions and 3 deletions

View File

@ -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'

View File

@ -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

View File

@ -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