Skip to content

Commit

Permalink
New take on configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Mugnolo and Santiago Pastorino committed Jan 10, 2014
1 parent cf91146 commit 0ba4830
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 134 deletions.
25 changes: 11 additions & 14 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'active_model/array_serializer'
require 'active_model/serializable'
require 'active_model/serializer/associations'
require 'active_model/serializer/config'
require 'active_model/serializer/configuration'
require 'active_model/serializer/dsl'

require 'thread'
Expand All @@ -22,22 +22,13 @@ def inherited(base)

def setup
@mutex.synchronize do
yield CONFIG
yield Configuration.global
end
end

def embed(type, options={})
CONFIG.embed = type
CONFIG.embed_in_root = true if options[:embed_in_root] || options[:include]
ActiveSupport::Deprecation.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.embed_in_root = #{CONFIG.embed_in_root || false}
end
WARN
Configuration.global.embed = type
Configuration.global.embed_in_root = true if options[:embed_in_root] || options[:include]
end

if RUBY_VERSION >= '2.0'
Expand Down Expand Up @@ -74,22 +65,28 @@ def root_name

def_delegators :dsl, :attributes, :has_one, :has_many

def configuration
@configuration ||= Configuration.global.build
end

private

def dsl
@dsl ||= DSL.new self
end
end

attr_accessor :object, :scope, :root, :meta_key, :meta, :configuration

def initialize(object, options={})
@object = object
@scope = options[:scope]
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@wrap_in_array = options[:_wrap_in_array]
@configuration = self.class.configuration.build options
end
attr_accessor :object, :scope, :root, :meta_key, :meta

def json_key
if root == true || root.nil?
Expand Down
6 changes: 3 additions & 3 deletions lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module ActiveModel
class Serializer
class Association
def initialize(name, options={})
def initialize(name, options = {})
if options.has_key?(:include)
ActiveSupport::Deprecation.warn <<-WARN
** Notice: include was renamed to embed_in_root. **
Expand All @@ -13,8 +13,8 @@ def initialize(name, options={})

@name = name.to_s
@options = options
self.embed = options.fetch(:embed) { CONFIG.embed }
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
self.embed = options.fetch(:embed) { Configuration.global.embed }
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { Configuration.global.embed_in_root } }
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root] || name
Expand Down
31 changes: 0 additions & 31 deletions lib/active_model/serializer/config.rb

This file was deleted.

82 changes: 82 additions & 0 deletions lib/active_model/serializer/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'singleton'

module ActiveModel
class Serializer
class Configuration
class Null
include Singleton

def method_missing(*)
nil
end

def respond_to?(*)
true
end
end

attr_accessor :parent

class << self
def global
@global ||= new default_options
end

def default_options
{ embed: :objects }
end
end

def build(options = {})
self.class.new options, self
end

def initialize(options = {}, parent = Null.instance)
@root = read_option options, :root
@embed = read_option options, :embed
@embed_in_root = read_option options, :embed_in_root
@parent = parent
end

def root
return_first @root, parent.root
end

def embed
return_first @embed, parent.embed
end

def embed_in_root
return_first @embed_in_root, parent.embed_in_root
end

# FIXME: Get rid of this mess.
def embed_objects=(value)
@embed = :objects if value
end

# FIXME: Get rid of this mess.
def embed_ids=(value)
@embed = :ids if value
end

def embed_objects
[:objects, :object].include? embed
end

def embed_ids
[:ids, :id].include? embed
end

private

def read_option(options, name)
options[name] || false if options.has_key? name
end

def return_first(*values)
values.compact.first
end
end
end
end
86 changes: 0 additions & 86 deletions test/unit/active_model/serializer/config_test.rb

This file was deleted.

41 changes: 41 additions & 0 deletions test/unit/active_model/serializer/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'test_helper'

module ActiveModel
class Serializer
class Configuration
class GlobalTest < Minitest::Test
def test_returns_global_configuration
assert_kind_of Configuration, Configuration.global
end

def test_global_configuration_returns_the_same_instance
assert_equal Configuration.global.object_id, Configuration.global.object_id
end

def test_global_configuration_has_default_options_set
assert Configuration.default_options.all? do |name, value|
Configuration.global.send(name) == value
end
end
end

class OptionsTest < Minitest::Test
def setup
@configuration = Configuration.global.build(root: 'root', embed: :ids, embed_in_root: false)
end

def test_configuration_has_root_option
assert_equal 'root', @configuration.root
end

def test_configuration_has_embed_option
assert_equal :ids, @configuration.embed
end

def test_configuration_has_embed_in_root_option
assert_equal false, @configuration.embed_in_root
end
end
end
end
end

0 comments on commit 0ba4830

Please sign in to comment.