mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Rename Settings to Config and use accessors to configure things
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
require 'active_model/array_serializer'
|
||||
require 'active_model/serializable'
|
||||
require 'active_model/serializer/associations'
|
||||
require 'active_model/serializer/settings'
|
||||
require 'active_model/serializer/config'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
@@ -14,19 +14,19 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def setup
|
||||
yield SETTINGS
|
||||
yield CONFIG
|
||||
end
|
||||
|
||||
def embed(type, options={})
|
||||
SETTINGS[:embed] = type
|
||||
SETTINGS[:include] = true if options[:include]
|
||||
CONFIG.embed = type
|
||||
CONFIG.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}
|
||||
config.include = #{CONFIG.include || false}
|
||||
end
|
||||
WARN
|
||||
end
|
||||
|
||||
@@ -8,8 +8,8 @@ module ActiveModel
|
||||
@name = name.to_s
|
||||
@options = options
|
||||
|
||||
self.embed = options[:embed] || SETTINGS[:embed] || :objects
|
||||
@embed_in_root = @embed_ids && (options[:include] || SETTINGS[:include])
|
||||
self.embed = options[:embed] || CONFIG.embed || :objects
|
||||
@embed_in_root = @embed_ids && (options[:include] || CONFIG.include)
|
||||
@embed_key = options[:embed_key] || :id
|
||||
@key = options[:key]
|
||||
@embedded_key = options[:root] || name
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
require 'active_support/hash_with_indifferent_access'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Settings
|
||||
class Config
|
||||
def initialize
|
||||
@data = ActiveSupport::HashWithIndifferentAccess.new
|
||||
@data = {}
|
||||
end
|
||||
|
||||
def [](key)
|
||||
@data[key]
|
||||
@data[key.to_s]
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
@data[key] = value
|
||||
@data[key.to_s] = value
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
@@ -24,8 +22,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
name = name.to_s
|
||||
return @data[name] if @data.include?(name)
|
||||
match = name.to_s.match(/(.*?)([?=]?)$/)
|
||||
match = name.match(/\A(.*?)([?=]?)\Z/)
|
||||
case match[2]
|
||||
when "="
|
||||
@data[match[1]] = args.first
|
||||
@@ -35,6 +34,6 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
SETTINGS = Settings.new
|
||||
CONFIG = Config.new
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user