active_model_serializers 0.9.9 → 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 -204
- 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/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +87 -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/serializer.rb +192 -267
- 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 +123 -171
- 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 -6
- 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/utils.rb +0 -16
- data/lib/active_model/serializable.rb +0 -59
- 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/association.rb +0 -58
- 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 -26
- 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 -3
- data/test/tmp/app/helpers/accounts_helper.rb +0 -3
- data/test/tmp/app/serializers/account_serializer.rb +0 -4
- data/test/tmp/config/routes.rb +0 -2
- 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 -239
- 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
@@ -1,333 +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
|
-
search_list = build_serializer_class_list(resource, options)
|
70
|
-
result = search_list.map do |klass_name|
|
71
|
-
Serializer.serializers_cache.fetch_or_store(klass_name) do
|
72
|
-
_const_get(klass_name)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
result.find { |serializer| !serializer.nil? }
|
77
|
-
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)
|
78
39
|
end
|
40
|
+
end
|
79
41
|
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
83
50
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
CONFIG.plural_default_root ? root_name.pluralize : root_name
|
88
|
-
end
|
89
|
-
end
|
51
|
+
def self.fragmented(serializer)
|
52
|
+
@_fragmented = serializer
|
53
|
+
end
|
90
54
|
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
94
63
|
|
95
|
-
|
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
|
96
73
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
102
83
|
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
106
93
|
|
107
|
-
|
108
|
-
|
109
|
-
|
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
|
103
|
+
end
|
110
104
|
|
111
|
-
|
112
|
-
@serializers_cache ||= Concurrent::Map.new
|
105
|
+
self._associations[attr] = {type: type, association_options: options}
|
113
106
|
end
|
107
|
+
end
|
114
108
|
|
115
|
-
|
109
|
+
def self.url(attr)
|
110
|
+
@_urls.push attr
|
111
|
+
end
|
116
112
|
|
117
|
-
|
118
|
-
|
113
|
+
def self.urls(*attrs)
|
114
|
+
@_urls.concat attrs
|
115
|
+
end
|
119
116
|
|
120
|
-
|
121
|
-
|
122
|
-
|
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))
|
123
124
|
end
|
125
|
+
end
|
124
126
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
130
133
|
end
|
131
|
-
|
132
|
-
|
133
|
-
"
|
134
|
-
klass_name << "#{options[:namespace]}::" if options[:namespace]
|
135
|
-
klass_name << options[:prefix].to_s.classify if options[:prefix]
|
136
|
-
if resource.is_a?(String)
|
137
|
-
klass_name << "#{resource}Serializer"
|
138
|
-
else
|
139
|
-
klass_name << "#{resource.class.name}Serializer"
|
140
|
-
end
|
141
|
-
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}"
|
142
137
|
end
|
143
138
|
|
144
|
-
|
145
|
-
|
139
|
+
adapter_class
|
140
|
+
end
|
146
141
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
end unless method_defined?(attr)
|
142
|
+
def self._root
|
143
|
+
@@root ||= false
|
144
|
+
end
|
151
145
|
|
152
|
-
|
153
|
-
|
154
|
-
end
|
146
|
+
def self._root=(root)
|
147
|
+
@@root = root
|
155
148
|
end
|
156
149
|
|
157
|
-
def
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
@
|
165
|
-
@
|
166
|
-
@
|
167
|
-
@
|
168
|
-
@
|
169
|
-
@
|
150
|
+
def self.root_name
|
151
|
+
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
152
|
+
end
|
153
|
+
|
154
|
+
attr_accessor :object, :root, :meta, :meta_key, :scope
|
155
|
+
|
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]
|
163
|
+
|
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 }
|
168
|
+
end
|
169
|
+
end
|
170
170
|
end
|
171
|
-
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
|
172
171
|
|
173
172
|
def json_key
|
174
|
-
|
173
|
+
if root == true || root.nil?
|
175
174
|
self.class.root_name
|
176
175
|
else
|
177
176
|
root
|
178
177
|
end
|
179
|
-
|
180
|
-
key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
|
181
178
|
end
|
182
179
|
|
183
|
-
def
|
184
|
-
|
185
|
-
hash[name] = send(name)
|
186
|
-
end
|
180
|
+
def id
|
181
|
+
object.id if object
|
187
182
|
end
|
188
183
|
|
189
|
-
def
|
190
|
-
|
191
|
-
included_associations = filter(associations.keys)
|
192
|
-
associations.each_with_object({}) do |(name, association), hash|
|
193
|
-
if included_associations.include? name
|
194
|
-
if association.embed_ids?
|
195
|
-
ids = serialize_ids association
|
196
|
-
if association.embed_namespace?
|
197
|
-
hash = hash[association.embed_namespace] ||= {}
|
198
|
-
hash[association.key] = ids
|
199
|
-
else
|
200
|
-
hash[association.key] = ids
|
201
|
-
end
|
202
|
-
elsif association.embed_objects?
|
203
|
-
if association.embed_namespace?
|
204
|
-
hash = hash[association.embed_namespace] ||= {}
|
205
|
-
end
|
206
|
-
hash[association.embedded_key] = serialize association, options
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
184
|
+
def type
|
185
|
+
object.class.to_s.demodulize.underscore.pluralize
|
210
186
|
end
|
211
187
|
|
212
|
-
def
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
end
|
220
|
-
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
|
221
195
|
|
222
|
-
|
223
|
-
associations = self.class._associations
|
224
|
-
included_associations = filter(associations.keys)
|
225
|
-
associations.each_with_object({}) do |(name, association), hash|
|
226
|
-
if included_associations.include? name
|
227
|
-
association_serializer = build_serializer(association)
|
228
|
-
# we must do this always because even if the current association is not
|
229
|
-
# embedded in root, it might have its own associations that are embedded in root
|
230
|
-
hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
|
231
|
-
if oldval.respond_to?(:to_ary)
|
232
|
-
[oldval, newval].flatten.uniq
|
233
|
-
else
|
234
|
-
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
|
235
|
-
end
|
236
|
-
end
|
196
|
+
attributes += options[:required_fields] if options[:required_fields]
|
237
197
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
serialized_data = association_serializer.serializable_object
|
244
|
-
key = association.root_key
|
245
|
-
if hash.has_key?(key)
|
246
|
-
hash[key].concat(serialized_data).uniq!
|
247
|
-
else
|
248
|
-
hash[key] = serialized_data
|
249
|
-
end
|
250
|
-
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)
|
251
203
|
end
|
252
204
|
end
|
253
205
|
end
|
254
206
|
|
255
|
-
def
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
def association_options_for_serializer(association)
|
261
|
-
prefix = association.options[:prefix]
|
262
|
-
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)
|
263
211
|
|
264
|
-
|
265
|
-
opts[:namespace] = namespace if namespace
|
266
|
-
opts[:prefix] = prefix if prefix
|
267
|
-
end
|
268
|
-
end
|
212
|
+
serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
|
269
213
|
|
270
|
-
|
271
|
-
|
272
|
-
|
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
|
273
222
|
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
associated_data.map { |elem| serialize_id(elem, association) }
|
278
|
-
else
|
279
|
-
serialize_id(associated_data, association) if associated_data
|
223
|
+
if block_given?
|
224
|
+
block.call(name, serializer, association_options[:association_options])
|
225
|
+
end
|
280
226
|
end
|
281
227
|
end
|
282
228
|
|
283
|
-
def
|
284
|
-
|
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
|
285
234
|
end
|
286
235
|
|
287
|
-
def
|
288
|
-
|
289
|
-
key.to_s.camelize(:lower)
|
290
|
-
else
|
291
|
-
key
|
292
|
-
end
|
236
|
+
def self.serializers_cache
|
237
|
+
@serializers_cache ||= ThreadSafe::Cache.new
|
293
238
|
end
|
294
239
|
|
295
|
-
|
296
|
-
Hash[hash.map do |k,v|
|
297
|
-
key = if k.is_a?(Symbol)
|
298
|
-
format_key(k).to_sym
|
299
|
-
else
|
300
|
-
format_key(k)
|
301
|
-
end
|
240
|
+
private
|
302
241
|
|
303
|
-
|
304
|
-
end]
|
305
|
-
end
|
306
|
-
|
307
|
-
attr_writer :serialization_options
|
308
|
-
def serialization_options
|
309
|
-
@serialization_options || {}
|
310
|
-
end
|
242
|
+
attr_reader :options
|
311
243
|
|
312
|
-
def
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
hash.merge! associations(options)
|
317
|
-
hash = convert_keys(hash) if key_format.present?
|
318
|
-
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
|
319
|
-
@wrap_in_array ? [hash] : hash
|
320
|
-
end
|
321
|
-
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
|
322
248
|
|
323
|
-
|
324
|
-
|
325
|
-
|
249
|
+
if serializer_class
|
250
|
+
serializer_class
|
251
|
+
elsif klass.superclass
|
252
|
+
get_serializer_for(klass.superclass)
|
253
|
+
end
|
254
|
+
end
|
326
255
|
end
|
327
256
|
|
328
|
-
def type_name(elem)
|
329
|
-
elem.class.to_s.demodulize.underscore.to_sym
|
330
|
-
end
|
331
257
|
end
|
332
|
-
|
333
258
|
end
|
@@ -1,18 +1,16 @@
|
|
1
1
|
require 'active_model'
|
2
|
-
require 'active_model/serializer'
|
3
|
-
require 'active_model/serializer_support'
|
4
2
|
require 'active_model/serializer/version'
|
5
|
-
require 'active_model/serializer
|
3
|
+
require 'active_model/serializer'
|
4
|
+
require 'active_model/serializer/fieldset'
|
6
5
|
|
7
6
|
begin
|
8
7
|
require 'action_controller'
|
9
8
|
require 'action_controller/serialization'
|
10
|
-
require 'action_controller/serialization_test_case'
|
11
9
|
|
12
10
|
ActiveSupport.on_load(:action_controller) do
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
include ::ActionController::Serialization
|
12
|
+
ActionDispatch::Reloader.to_prepare do
|
13
|
+
ActiveModel::Serializer.serializers_cache.clear
|
16
14
|
end
|
17
15
|
end
|
18
16
|
rescue LoadError
|
data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb
RENAMED
@@ -1,12 +1,12 @@
|
|
1
1
|
module Rails
|
2
2
|
module Generators
|
3
3
|
class SerializerGenerator < NamedBase
|
4
|
-
source_root File.expand_path(
|
5
|
-
check_class_collision suffix
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
check_class_collision :suffix => "Serializer"
|
6
6
|
|
7
|
-
argument :attributes, type
|
7
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
8
8
|
|
9
|
-
class_option :parent, type
|
9
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
|
10
10
|
|
11
11
|
def create_serializer_file
|
12
12
|
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
@@ -25,13 +25,13 @@ module Rails
|
|
25
25
|
def parent_class_name
|
26
26
|
if options[:parent]
|
27
27
|
options[:parent]
|
28
|
-
elsif (
|
29
|
-
|
30
|
-
'ApplicationSerializer'
|
28
|
+
elsif defined?(::ApplicationSerializer)
|
29
|
+
"ApplicationSerializer"
|
31
30
|
else
|
32
|
-
|
31
|
+
"ActiveModel::Serializer"
|
33
32
|
end
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
37
|
+
|
data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb
RENAMED
@@ -1,8 +1,8 @@
|
|
1
1
|
<% module_namespacing do -%>
|
2
2
|
class <%= class_name %>Serializer < <%= parent_class_name %>
|
3
3
|
attributes <%= attributes_names.map(&:inspect).join(", ") %>
|
4
|
+
end
|
4
5
|
<% association_names.each do |attribute| -%>
|
5
|
-
|
6
|
+
attribute :<%= attribute %>
|
6
7
|
<% end -%>
|
7
|
-
end
|
8
8
|
<% end -%>
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class AdapterSelectorTest < ActionController::TestCase
|
6
|
+
class MyController < ActionController::Base
|
7
|
+
def render_using_default_adapter
|
8
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
render json: @profile
|
10
|
+
end
|
11
|
+
|
12
|
+
def render_using_adapter_override
|
13
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
14
|
+
render json: @profile, adapter: :json_api
|
15
|
+
end
|
16
|
+
|
17
|
+
def render_skipping_adapter
|
18
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
19
|
+
render json: @profile, adapter: false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
tests MyController
|
24
|
+
|
25
|
+
def test_render_using_default_adapter
|
26
|
+
get :render_using_default_adapter
|
27
|
+
assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_render_using_adapter_override
|
31
|
+
get :render_using_adapter_override
|
32
|
+
|
33
|
+
expected = {
|
34
|
+
data: {
|
35
|
+
name: "Name 1",
|
36
|
+
description: "Description 1",
|
37
|
+
id: assigns(:profile).id.to_s,
|
38
|
+
type: "profiles"
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
assert_equal expected.to_json, response.body
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_render_skipping_adapter
|
46
|
+
get :render_skipping_adapter
|
47
|
+
assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|