active_model_serializers 0.9.8 → 0.10.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +21 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +8 -200
- data/CONTRIBUTING.md +23 -12
- data/Gemfile +17 -0
- data/{MIT-LICENSE → LICENSE.txt} +3 -2
- data/README.md +151 -781
- data/Rakefile +12 -0
- data/active_model_serializers.gemspec +26 -0
- data/lib/action_controller/serialization.rb +30 -75
- data/lib/active_model/serializer.rb +192 -252
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/array_serializer.rb +32 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +5 -7
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -8
- data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
- data/test/action_controller/adapter_selector_test.rb +51 -0
- data/test/action_controller/explicit_serializer_test.rb +110 -0
- data/test/action_controller/json_api_linked_test.rb +173 -0
- data/test/action_controller/serialization_scope_name_test.rb +63 -0
- data/test/action_controller/serialization_test.rb +365 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +41 -0
- data/test/adapter/json/collection_test.rb +59 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +147 -0
- data/test/adapter/json_api/collection_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +106 -0
- data/test/adapter/json_api/has_one_test.rb +59 -0
- data/test/adapter/json_api/linked_test.rb +257 -0
- data/test/adapter/json_test.rb +34 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +43 -0
- data/test/array_serializer_test.rb +29 -0
- data/test/fixtures/poro.rb +125 -142
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +106 -0
- data/test/serializers/attribute_test.rb +23 -0
- data/test/serializers/attributes_test.rb +28 -0
- data/test/serializers/cache_test.rb +128 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/generators_test.rb +59 -0
- data/test/serializers/meta_test.rb +78 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +56 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +21 -4
- metadata +121 -159
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -79
- data/lib/active_model/array_serializer.rb +0 -68
- data/lib/active_model/default_serializer.rb +0 -28
- data/lib/active_model/serializable.rb +0 -59
- data/lib/active_model/serializable/utils.rb +0 -16
- data/lib/active_model/serializer/association.rb +0 -58
- data/lib/active_model/serializer/association/has_many.rb +0 -39
- data/lib/active_model/serializer/association/has_one.rb +0 -25
- data/lib/active_model/serializer/config.rb +0 -31
- data/lib/active_model/serializer/generators/resource_override.rb +0 -13
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -22
- data/lib/active_model/serializer_support.rb +0 -5
- data/lib/active_model_serializers/model/caching.rb +0 -25
- data/test/benchmark/app.rb +0 -60
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -41
- data/test/benchmark/setup.rb +0 -75
- data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
- data/test/fixtures/active_record.rb +0 -96
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
- data/test/integration/action_controller/serialization_test.rb +0 -287
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
- data/test/integration/active_record/active_record_test.rb +0 -94
- data/test/integration/generators/resource_generator_test.rb +0 -26
- data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
- data/test/integration/generators/serializer_generator_test.rb +0 -41
- data/test/test_app.rb +0 -14
- data/test/tmp/app/assets/javascripts/accounts.js +0 -2
- data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
- data/test/tmp/app/controllers/accounts_controller.rb +0 -2
- data/test/tmp/app/helpers/accounts_helper.rb +0 -2
- data/test/tmp/app/serializers/account_serializer.rb +0 -3
- data/test/tmp/config/routes.rb +0 -1
- data/test/unit/active_model/array_serializer/except_test.rb +0 -18
- data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
- data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
- data/test/unit/active_model/array_serializer/only_test.rb +0 -18
- data/test/unit/active_model/array_serializer/options_test.rb +0 -16
- data/test/unit/active_model/array_serializer/root_test.rb +0 -102
- data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
- data/test/unit/active_model/array_serializer/serialization_test.rb +0 -216
- data/test/unit/active_model/default_serializer_test.rb +0 -13
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
- data/test/unit/active_model/serializer/associations_test.rb +0 -49
- data/test/unit/active_model/serializer/attributes_test.rb +0 -57
- data/test/unit/active_model/serializer/config_test.rb +0 -91
- data/test/unit/active_model/serializer/filter_test.rb +0 -69
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- data/test/unit/active_model/serializer/has_many_test.rb +0 -265
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
- data/test/unit/active_model/serializer/has_one_test.rb +0 -253
- data/test/unit/active_model/serializer/key_format_test.rb +0 -25
- data/test/unit/active_model/serializer/meta_test.rb +0 -39
- data/test/unit/active_model/serializer/options_test.rb +0 -42
- data/test/unit/active_model/serializer/root_test.rb +0 -117
- data/test/unit/active_model/serializer/scope_test.rb +0 -49
- data/test/unit/active_model/serializer/url_helpers_test.rb +0 -35
- data/test/unit/active_model/serilizable_test.rb +0 -50
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_model/serializer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_model_serializers"
|
8
|
+
spec.version = ActiveModel::Serializer::VERSION
|
9
|
+
spec.authors = ["Steve Klabnik"]
|
10
|
+
spec.email = ["steve@steveklabnik.com"]
|
11
|
+
spec.summary = %q{Conventions-based JSON generation for Rails.}
|
12
|
+
spec.description = %q{ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.}
|
13
|
+
spec.homepage = "https://github.com/rails-api/active_model_serializers"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", ">= 4.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "rails", ">= 4.0"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
@@ -1,106 +1,61 @@
|
|
1
1
|
require 'active_support/core_ext/class/attribute'
|
2
2
|
|
3
3
|
module ActionController
|
4
|
-
# Action Controller Serialization
|
5
|
-
#
|
6
|
-
# Overrides render :json to check if the given object implements +active_model_serializer+
|
7
|
-
# as a method. If so, use the returned serializer instead of calling +to_json+ on the object.
|
8
|
-
#
|
9
|
-
# This module also provides a serialization_scope method that allows you to configure the
|
10
|
-
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
|
11
|
-
# to the current user:
|
12
|
-
#
|
13
|
-
# class ApplicationController < ActionController::Base
|
14
|
-
# serialization_scope :current_user
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# If you need more complex scope rules, you can simply override the serialization_scope:
|
18
|
-
#
|
19
|
-
# class ApplicationController < ActionController::Base
|
20
|
-
# private
|
21
|
-
#
|
22
|
-
# def serialization_scope
|
23
|
-
# current_user
|
24
|
-
# end
|
25
|
-
# end
|
26
|
-
#
|
27
4
|
module Serialization
|
28
5
|
extend ActiveSupport::Concern
|
29
6
|
|
30
7
|
include ActionController::Renderers
|
31
8
|
|
32
|
-
|
33
|
-
attr_accessor :enabled
|
34
|
-
end
|
35
|
-
self.enabled = true
|
9
|
+
ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
|
36
10
|
|
37
11
|
included do
|
38
12
|
class_attribute :_serialization_scope
|
39
13
|
self._serialization_scope = :current_user
|
40
14
|
end
|
41
15
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
49
|
-
define_method renderer_method do |resource, options|
|
50
|
-
serializer = build_json_serializer(resource, options)
|
51
|
-
|
52
|
-
if serializer
|
53
|
-
super(serializer, options)
|
54
|
-
else
|
55
|
-
super(resource, options)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
def namespace_for_serializer
|
63
|
-
@namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
|
16
|
+
def serialization_scope
|
17
|
+
send(_serialization_scope) if _serialization_scope &&
|
18
|
+
respond_to?(_serialization_scope, true)
|
64
19
|
end
|
65
20
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
else
|
70
|
-
klass.parent
|
71
|
-
end
|
72
|
-
end
|
21
|
+
def get_serializer(resource)
|
22
|
+
@_serializer ||= @_serializer_opts.delete(:serializer)
|
23
|
+
@_serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
73
24
|
|
74
|
-
|
75
|
-
|
76
|
-
o[:namespace] = namespace_for_serializer if namespace_for_serializer
|
25
|
+
if @_serializer_opts.key?(:each_serializer)
|
26
|
+
@_serializer_opts[:serializer] = @_serializer_opts.delete(:each_serializer)
|
77
27
|
end
|
78
28
|
|
79
|
-
|
29
|
+
@_serializer
|
80
30
|
end
|
81
31
|
|
82
|
-
def
|
83
|
-
|
32
|
+
def use_adapter?
|
33
|
+
!(@_adapter_opts.key?(:adapter) && !@_adapter_opts[:adapter])
|
84
34
|
end
|
85
35
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
36
|
+
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
37
|
+
define_method renderer_method do |resource, options|
|
38
|
+
@_adapter_opts, @_serializer_opts =
|
39
|
+
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
90
40
|
|
91
|
-
|
92
|
-
options = default_serializer_options.merge(options)
|
93
|
-
@namespace_for_serializer = options.fetch(:namespace, nil)
|
41
|
+
if use_adapter? && (serializer = get_serializer(resource))
|
94
42
|
|
95
|
-
|
96
|
-
|
43
|
+
@_serializer_opts[:scope] ||= serialization_scope
|
44
|
+
@_serializer_opts[:scope_name] = _serialization_scope
|
97
45
|
|
98
|
-
|
99
|
-
|
100
|
-
|
46
|
+
# omg hax
|
47
|
+
object = serializer.new(resource, @_serializer_opts)
|
48
|
+
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
|
49
|
+
super(adapter, options)
|
50
|
+
else
|
51
|
+
super(resource, options)
|
101
52
|
end
|
53
|
+
end
|
54
|
+
end
|
102
55
|
|
103
|
-
|
56
|
+
module ClassMethods
|
57
|
+
def serialization_scope(scope)
|
58
|
+
self._serialization_scope = scope
|
104
59
|
end
|
105
60
|
end
|
106
61
|
end
|
@@ -1,318 +1,258 @@
|
|
1
|
-
require '
|
2
|
-
require 'active_model/serializable'
|
3
|
-
require 'active_model/serializer/association'
|
4
|
-
require 'active_model/serializer/config'
|
5
|
-
|
6
|
-
require 'thread'
|
7
|
-
require 'concurrent/map'
|
1
|
+
require 'thread_safe'
|
8
2
|
|
9
3
|
module ActiveModel
|
10
4
|
class Serializer
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
extend ActiveSupport::Autoload
|
6
|
+
autoload :Configuration
|
7
|
+
autoload :ArraySerializer
|
8
|
+
autoload :Adapter
|
9
|
+
include Configuration
|
14
10
|
|
15
11
|
class << self
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
attr_accessor :_attributes
|
13
|
+
attr_accessor :_attributes_keys
|
14
|
+
attr_accessor :_associations
|
15
|
+
attr_accessor :_urls
|
16
|
+
attr_accessor :_cache
|
17
|
+
attr_accessor :_fragmented
|
18
|
+
attr_accessor :_cache_key
|
19
|
+
attr_accessor :_cache_only
|
20
|
+
attr_accessor :_cache_except
|
21
|
+
attr_accessor :_cache_options
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
def self.inherited(base)
|
25
|
+
base._attributes = []
|
26
|
+
base._attributes_keys = {}
|
27
|
+
base._associations = {}
|
28
|
+
base._urls = []
|
29
|
+
end
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
:embed_in_root_key,
|
32
|
-
:embed_namespace
|
33
|
-
].freeze
|
34
|
-
|
35
|
-
def embed(type, options={})
|
36
|
-
CONFIG.embed = type
|
37
|
-
if EMBED_IN_ROOT_OPTIONS.any? { |opt| options[opt].present? }
|
38
|
-
CONFIG.embed_in_root = true
|
39
|
-
end
|
40
|
-
if options[:embed_in_root_key].present?
|
41
|
-
CONFIG.embed_in_root_key = options[:embed_in_root_key]
|
42
|
-
end
|
43
|
-
ActiveSupport::Deprecation.warn <<-WARN
|
44
|
-
** Notice: embed is deprecated. **
|
45
|
-
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
|
46
|
-
Please use the global .setup method instead:
|
47
|
-
ActiveModel::Serializer.setup do |config|
|
48
|
-
config.embed = :#{type}
|
49
|
-
config.embed_in_root = #{CONFIG.embed_in_root || false}
|
50
|
-
end
|
51
|
-
WARN
|
52
|
-
end
|
31
|
+
def self.attributes(*attrs)
|
32
|
+
attrs = attrs.first if attrs.first.class == Array
|
33
|
+
@_attributes.concat attrs
|
53
34
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
def serializer_for(resource, options = {})
|
60
|
-
if resource.respond_to?(:serializer_class)
|
61
|
-
resource.serializer_class
|
62
|
-
elsif resource.respond_to?(:to_ary)
|
63
|
-
if Object.constants.include?(:ArraySerializer)
|
64
|
-
::ArraySerializer
|
65
|
-
else
|
66
|
-
ArraySerializer
|
67
|
-
end
|
68
|
-
else
|
69
|
-
klass_name = build_serializer_class(resource, options)
|
70
|
-
Serializer.serializers_cache.fetch_or_store(klass_name) do
|
71
|
-
_const_get(klass_name)
|
72
|
-
end
|
73
|
-
end
|
35
|
+
attrs.each do |attr|
|
36
|
+
define_method attr do
|
37
|
+
object && object.read_attribute_for_serialization(attr)
|
38
|
+
end unless method_defined?(attr) || _fragmented.respond_to?(attr)
|
74
39
|
end
|
40
|
+
end
|
75
41
|
|
76
|
-
|
77
|
-
|
78
|
-
|
42
|
+
def self.attribute(attr, options = {})
|
43
|
+
key = options.fetch(:key, attr)
|
44
|
+
@_attributes_keys[attr] = {key: key} if key != attr
|
45
|
+
@_attributes.concat [key]
|
46
|
+
define_method key do
|
47
|
+
object.read_attribute_for_serialization(attr)
|
48
|
+
end unless method_defined?(key) || _fragmented.respond_to?(attr)
|
49
|
+
end
|
79
50
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
CONFIG.plural_default_root ? root_name.pluralize : root_name
|
84
|
-
end
|
85
|
-
end
|
51
|
+
def self.fragmented(serializer)
|
52
|
+
@_fragmented = serializer
|
53
|
+
end
|
86
54
|
|
87
|
-
|
88
|
-
|
89
|
-
|
55
|
+
# Enables a serializer to be automatically cached
|
56
|
+
def self.cache(options = {})
|
57
|
+
@_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
|
58
|
+
@_cache_key = options.delete(:key)
|
59
|
+
@_cache_only = options.delete(:only)
|
60
|
+
@_cache_except = options.delete(:except)
|
61
|
+
@_cache_options = (options.empty?) ? nil : options
|
62
|
+
end
|
63
|
+
|
64
|
+
# Defines an association in the object should be rendered.
|
65
|
+
#
|
66
|
+
# The serializer object should implement the association name
|
67
|
+
# as a method which should return an array when invoked. If a method
|
68
|
+
# with the association name does not exist, the association name is
|
69
|
+
# dispatched to the serialized object.
|
70
|
+
def self.has_many(*attrs)
|
71
|
+
associate(:has_many, attrs)
|
72
|
+
end
|
90
73
|
|
91
|
-
|
74
|
+
# Defines an association in the object that should be rendered.
|
75
|
+
#
|
76
|
+
# The serializer object should implement the association name
|
77
|
+
# as a method which should return an object when invoked. If a method
|
78
|
+
# with the association name does not exist, the association name is
|
79
|
+
# dispatched to the serialized object.
|
80
|
+
def self.belongs_to(*attrs)
|
81
|
+
associate(:belongs_to, attrs)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Defines an association in the object should be rendered.
|
85
|
+
#
|
86
|
+
# The serializer object should implement the association name
|
87
|
+
# as a method which should return an object when invoked. If a method
|
88
|
+
# with the association name does not exist, the association name is
|
89
|
+
# dispatched to the serialized object.
|
90
|
+
def self.has_one(*attrs)
|
91
|
+
associate(:has_one, attrs)
|
92
|
+
end
|
92
93
|
|
93
|
-
|
94
|
-
|
95
|
-
|
94
|
+
def self.associate(type, attrs) #:nodoc:
|
95
|
+
options = attrs.extract_options!
|
96
|
+
self._associations = _associations.dup
|
97
|
+
|
98
|
+
attrs.each do |attr|
|
99
|
+
unless method_defined?(attr)
|
100
|
+
define_method attr do
|
101
|
+
object.send attr
|
102
|
+
end
|
96
103
|
end
|
97
|
-
end
|
98
104
|
|
99
|
-
|
100
|
-
associate(Association::HasOne, *attrs)
|
105
|
+
self._associations[attr] = {type: type, association_options: options}
|
101
106
|
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.url(attr)
|
110
|
+
@_urls.push attr
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.urls(*attrs)
|
114
|
+
@_urls.concat attrs
|
115
|
+
end
|
102
116
|
|
103
|
-
|
104
|
-
|
117
|
+
def self.serializer_for(resource, options = {})
|
118
|
+
if resource.respond_to?(:to_ary)
|
119
|
+
config.array_serializer
|
120
|
+
else
|
121
|
+
options
|
122
|
+
.fetch(:association_options, {})
|
123
|
+
.fetch(:serializer, get_serializer_for(resource.class))
|
105
124
|
end
|
125
|
+
end
|
106
126
|
|
107
|
-
|
108
|
-
|
127
|
+
def self.adapter
|
128
|
+
adapter_class = case config.adapter
|
129
|
+
when Symbol
|
130
|
+
ActiveModel::Serializer::Adapter.adapter_class(config.adapter)
|
131
|
+
when Class
|
132
|
+
config.adapter
|
133
|
+
end
|
134
|
+
unless adapter_class
|
135
|
+
valid_adapters = Adapter.constants.map { |klass| ":#{klass.to_s.downcase}" }
|
136
|
+
raise ArgumentError, "Unknown adapter: #{config.adapter}. Valid adapters are: #{valid_adapters}"
|
109
137
|
end
|
110
138
|
|
111
|
-
|
139
|
+
adapter_class
|
140
|
+
end
|
112
141
|
|
113
|
-
|
114
|
-
|
142
|
+
def self._root
|
143
|
+
@@root ||= false
|
144
|
+
end
|
115
145
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
146
|
+
def self._root=(root)
|
147
|
+
@@root = root
|
148
|
+
end
|
120
149
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
klass_name << options[:prefix].to_s.classify if options[:prefix]
|
125
|
-
klass_name << "#{resource.class.name}Serializer"
|
126
|
-
end
|
127
|
-
end
|
150
|
+
def self.root_name
|
151
|
+
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
152
|
+
end
|
128
153
|
|
129
|
-
|
130
|
-
options = attrs.extract_options!
|
154
|
+
attr_accessor :object, :root, :meta, :meta_key, :scope
|
131
155
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
156
|
+
def initialize(object, options = {})
|
157
|
+
@object = object
|
158
|
+
@options = options
|
159
|
+
@root = options[:root] || (self.class._root ? self.class.root_name : false)
|
160
|
+
@meta = options[:meta]
|
161
|
+
@meta_key = options[:meta_key]
|
162
|
+
@scope = options[:scope]
|
136
163
|
|
137
|
-
|
164
|
+
scope_name = options[:scope_name]
|
165
|
+
if scope_name && !respond_to?(scope_name)
|
166
|
+
self.class.class_eval do
|
167
|
+
define_method scope_name, lambda { scope }
|
138
168
|
end
|
139
169
|
end
|
140
170
|
end
|
141
171
|
|
142
|
-
def initialize(object, options={})
|
143
|
-
@object = object
|
144
|
-
@scope = options[:scope]
|
145
|
-
@root = options.fetch(:root, self.class._root)
|
146
|
-
@polymorphic = options.fetch(:polymorphic, false)
|
147
|
-
@meta_key = options[:meta_key] || :meta
|
148
|
-
@meta = options[@meta_key]
|
149
|
-
@wrap_in_array = options[:_wrap_in_array]
|
150
|
-
@only = options[:only] ? Array(options[:only]) : nil
|
151
|
-
@except = options[:except] ? Array(options[:except]) : nil
|
152
|
-
@key_format = options[:key_format]
|
153
|
-
@context = options[:context]
|
154
|
-
@namespace = options[:namespace]
|
155
|
-
end
|
156
|
-
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
|
157
|
-
|
158
172
|
def json_key
|
159
|
-
|
173
|
+
if root == true || root.nil?
|
160
174
|
self.class.root_name
|
161
175
|
else
|
162
176
|
root
|
163
177
|
end
|
164
|
-
|
165
|
-
key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
|
166
178
|
end
|
167
179
|
|
168
|
-
def
|
169
|
-
|
170
|
-
hash[name] = send(name)
|
171
|
-
end
|
180
|
+
def id
|
181
|
+
object.id if object
|
172
182
|
end
|
173
183
|
|
174
|
-
def
|
175
|
-
|
176
|
-
included_associations = filter(associations.keys)
|
177
|
-
associations.each_with_object({}) do |(name, association), hash|
|
178
|
-
if included_associations.include? name
|
179
|
-
if association.embed_ids?
|
180
|
-
ids = serialize_ids association
|
181
|
-
if association.embed_namespace?
|
182
|
-
hash = hash[association.embed_namespace] ||= {}
|
183
|
-
hash[association.key] = ids
|
184
|
-
else
|
185
|
-
hash[association.key] = ids
|
186
|
-
end
|
187
|
-
elsif association.embed_objects?
|
188
|
-
if association.embed_namespace?
|
189
|
-
hash = hash[association.embed_namespace] ||= {}
|
190
|
-
end
|
191
|
-
hash[association.embedded_key] = serialize association, options
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
184
|
+
def type
|
185
|
+
object.class.to_s.demodulize.underscore.pluralize
|
195
186
|
end
|
196
187
|
|
197
|
-
def
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
end
|
205
|
-
end
|
188
|
+
def attributes(options = {})
|
189
|
+
attributes =
|
190
|
+
if options[:fields]
|
191
|
+
self.class._attributes & options[:fields]
|
192
|
+
else
|
193
|
+
self.class._attributes.dup
|
194
|
+
end
|
206
195
|
|
207
|
-
|
208
|
-
associations = self.class._associations
|
209
|
-
included_associations = filter(associations.keys)
|
210
|
-
associations.each_with_object({}) do |(name, association), hash|
|
211
|
-
if included_associations.include? name
|
212
|
-
association_serializer = build_serializer(association)
|
213
|
-
# we must do this always because even if the current association is not
|
214
|
-
# embedded in root, it might have its own associations that are embedded in root
|
215
|
-
hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
|
216
|
-
if oldval.respond_to?(:to_ary)
|
217
|
-
[oldval, newval].flatten.uniq
|
218
|
-
else
|
219
|
-
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
|
220
|
-
end
|
221
|
-
end
|
196
|
+
attributes += options[:required_fields] if options[:required_fields]
|
222
197
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
serialized_data = association_serializer.serializable_object
|
229
|
-
key = association.root_key
|
230
|
-
if hash.has_key?(key)
|
231
|
-
hash[key].concat(serialized_data).uniq!
|
232
|
-
else
|
233
|
-
hash[key] = serialized_data
|
234
|
-
end
|
235
|
-
end
|
198
|
+
attributes.each_with_object({}) do |name, hash|
|
199
|
+
unless self.class._fragmented
|
200
|
+
hash[name] = send(name)
|
201
|
+
else
|
202
|
+
hash[name] = self.class._fragmented.public_send(name)
|
236
203
|
end
|
237
204
|
end
|
238
205
|
end
|
239
206
|
|
240
|
-
def
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
def association_options_for_serializer(association)
|
246
|
-
prefix = association.options[:prefix]
|
247
|
-
namespace = association.options[:namespace] || @namespace || self.namespace
|
207
|
+
def each_association(&block)
|
208
|
+
self.class._associations.dup.each do |name, association_options|
|
209
|
+
next unless object
|
210
|
+
association_value = send(name)
|
248
211
|
|
249
|
-
|
250
|
-
opts[:namespace] = namespace if namespace
|
251
|
-
opts[:prefix] = prefix if prefix
|
252
|
-
end
|
253
|
-
end
|
212
|
+
serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
|
254
213
|
|
255
|
-
|
256
|
-
|
257
|
-
|
214
|
+
if serializer_class
|
215
|
+
serializer = serializer_class.new(
|
216
|
+
association_value,
|
217
|
+
options.merge(serializer_from_options(association_options))
|
218
|
+
)
|
219
|
+
elsif !association_value.nil? && !association_value.instance_of?(Object)
|
220
|
+
association_options[:association_options][:virtual_value] = association_value
|
221
|
+
end
|
258
222
|
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
associated_data.map { |elem| serialize_id(elem, association) }
|
263
|
-
else
|
264
|
-
serialize_id(associated_data, association) if associated_data
|
223
|
+
if block_given?
|
224
|
+
block.call(name, serializer, association_options[:association_options])
|
225
|
+
end
|
265
226
|
end
|
266
227
|
end
|
267
228
|
|
268
|
-
def
|
269
|
-
|
229
|
+
def serializer_from_options(options)
|
230
|
+
opts = {}
|
231
|
+
serializer = options.fetch(:association_options, {}).fetch(:serializer, nil)
|
232
|
+
opts[:serializer] = serializer if serializer
|
233
|
+
opts
|
270
234
|
end
|
271
235
|
|
272
|
-
def
|
273
|
-
|
274
|
-
key.to_s.camelize(:lower)
|
275
|
-
else
|
276
|
-
key
|
277
|
-
end
|
236
|
+
def self.serializers_cache
|
237
|
+
@serializers_cache ||= ThreadSafe::Cache.new
|
278
238
|
end
|
279
239
|
|
280
|
-
|
281
|
-
Hash[hash.map do |k,v|
|
282
|
-
key = if k.is_a?(Symbol)
|
283
|
-
format_key(k).to_sym
|
284
|
-
else
|
285
|
-
format_key(k)
|
286
|
-
end
|
240
|
+
private
|
287
241
|
|
288
|
-
|
289
|
-
end]
|
290
|
-
end
|
291
|
-
|
292
|
-
attr_writer :serialization_options
|
293
|
-
def serialization_options
|
294
|
-
@serialization_options || {}
|
295
|
-
end
|
242
|
+
attr_reader :options
|
296
243
|
|
297
|
-
def
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
hash.merge! associations(options)
|
302
|
-
hash = convert_keys(hash) if key_format.present?
|
303
|
-
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
|
304
|
-
@wrap_in_array ? [hash] : hash
|
305
|
-
end
|
306
|
-
alias_method :serializable_hash, :serializable_object
|
244
|
+
def self.get_serializer_for(klass)
|
245
|
+
serializers_cache.fetch_or_store(klass) do
|
246
|
+
serializer_class_name = "#{klass.name}Serializer"
|
247
|
+
serializer_class = serializer_class_name.safe_constantize
|
307
248
|
|
308
|
-
|
309
|
-
|
310
|
-
|
249
|
+
if serializer_class
|
250
|
+
serializer_class
|
251
|
+
elsif klass.superclass
|
252
|
+
get_serializer_for(klass.superclass)
|
253
|
+
end
|
254
|
+
end
|
311
255
|
end
|
312
256
|
|
313
|
-
def type_name(elem)
|
314
|
-
elem.class.to_s.demodulize.underscore.to_sym
|
315
|
-
end
|
316
257
|
end
|
317
|
-
|
318
258
|
end
|