active_model_serializers 0.8.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +122 -5
- data/CONTRIBUTING.md +20 -0
- data/DESIGN.textile +4 -4
- data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +396 -95
- data/lib/action_controller/serialization.rb +50 -12
- data/lib/action_controller/serialization_test_case.rb +79 -0
- data/lib/active_model/array_serializer.rb +47 -78
- data/lib/active_model/default_serializer.rb +32 -0
- data/lib/active_model/serializable/utils.rb +16 -0
- data/lib/active_model/serializable.rb +62 -0
- data/lib/active_model/serializer/association/has_many.rb +39 -0
- data/lib/active_model/serializer/association/has_one.rb +25 -0
- data/lib/active_model/serializer/association.rb +58 -0
- data/lib/active_model/serializer/config.rb +31 -0
- data/lib/active_model/serializer/generators/resource_override.rb +13 -0
- data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
- data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
- data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
- data/lib/active_model/serializer/railtie.rb +18 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +214 -423
- data/lib/active_model/serializer_support.rb +5 -0
- data/lib/active_model_serializers/mime_types.rb +14 -0
- data/lib/active_model_serializers.rb +12 -87
- data/test/fixtures/active_record.rb +96 -0
- data/test/fixtures/poro.rb +187 -0
- data/test/fixtures/template.html.erb +1 -0
- data/test/integration/action_controller/namespaced_serialization_test.rb +105 -0
- data/test/integration/action_controller/serialization_test.rb +287 -0
- data/test/integration/action_controller/serialization_test_case_test.rb +71 -0
- data/test/integration/active_record/active_record_test.rb +94 -0
- data/test/integration/generators/resource_generator_test.rb +26 -0
- data/test/integration/generators/scaffold_controller_generator_test.rb +64 -0
- data/test/integration/generators/serializer_generator_test.rb +41 -0
- data/test/test_app.rb +14 -0
- data/test/test_helper.rb +10 -18
- data/test/unit/active_model/array_serializer/except_test.rb +18 -0
- data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
- data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
- data/test/unit/active_model/array_serializer/only_test.rb +18 -0
- data/test/unit/active_model/array_serializer/options_test.rb +16 -0
- data/test/unit/active_model/array_serializer/root_test.rb +102 -0
- data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
- data/test/unit/active_model/array_serializer/serialization_test.rb +216 -0
- data/test/unit/active_model/default_serializer_test.rb +13 -0
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +36 -0
- data/test/unit/active_model/serializer/associations_test.rb +19 -0
- data/test/unit/active_model/serializer/attributes_test.rb +57 -0
- data/test/unit/active_model/serializer/config_test.rb +91 -0
- data/test/unit/active_model/serializer/filter_test.rb +69 -0
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +189 -0
- data/test/unit/active_model/serializer/has_many_test.rb +265 -0
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +27 -0
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +196 -0
- data/test/unit/active_model/serializer/has_one_test.rb +253 -0
- data/test/unit/active_model/serializer/key_format_test.rb +25 -0
- data/test/unit/active_model/serializer/meta_test.rb +39 -0
- data/test/unit/active_model/serializer/options_test.rb +42 -0
- data/test/unit/active_model/serializer/root_test.rb +117 -0
- data/test/unit/active_model/serializer/scope_test.rb +49 -0
- data/test/unit/active_model/serializer/url_helpers_test.rb +35 -0
- metadata +107 -64
- data/.gitignore +0 -18
- data/.travis.yml +0 -28
- data/Gemfile +0 -4
- data/Gemfile.edge +0 -9
- data/Rakefile +0 -18
- data/active_model_serializers.gemspec +0 -24
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/serializer/associations.rb +0 -233
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/array_serializer_test.rb +0 -75
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_scope_name_test.rb +0 -67
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -1,233 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Associations #:nodoc:
|
4
|
-
class Config #:nodoc:
|
5
|
-
class_attribute :options
|
6
|
-
|
7
|
-
def self.refine(name, class_options)
|
8
|
-
current_class = self
|
9
|
-
|
10
|
-
Class.new(self) do
|
11
|
-
singleton_class.class_eval do
|
12
|
-
define_method(:to_s) do
|
13
|
-
"(subclass of #{current_class.name})"
|
14
|
-
end
|
15
|
-
|
16
|
-
alias inspect to_s
|
17
|
-
end
|
18
|
-
|
19
|
-
self.options = class_options
|
20
|
-
|
21
|
-
# cache the root so we can reuse it without falling back on a per-instance basis
|
22
|
-
begin
|
23
|
-
self.options[:root] ||= self.new(name, nil).root
|
24
|
-
rescue
|
25
|
-
# this could fail if it needs a valid source, for example a polymorphic association
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
self.options = {}
|
32
|
-
|
33
|
-
def initialize(name, source, options={})
|
34
|
-
@name = name
|
35
|
-
@source = source
|
36
|
-
@options = options
|
37
|
-
end
|
38
|
-
|
39
|
-
def option(key, default=nil)
|
40
|
-
if @options.key?(key)
|
41
|
-
@options[key]
|
42
|
-
elsif self.class.options.key?(key)
|
43
|
-
self.class.options[key]
|
44
|
-
else
|
45
|
-
default
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def target_serializer
|
50
|
-
serializer = option(:serializer)
|
51
|
-
serializer.is_a?(String) ? serializer.constantize : serializer
|
52
|
-
end
|
53
|
-
|
54
|
-
def source_serializer
|
55
|
-
@source
|
56
|
-
end
|
57
|
-
|
58
|
-
def key
|
59
|
-
option(:key) || @name
|
60
|
-
end
|
61
|
-
|
62
|
-
def root
|
63
|
-
option(:root) || @name
|
64
|
-
end
|
65
|
-
|
66
|
-
def name
|
67
|
-
option(:name) || @name
|
68
|
-
end
|
69
|
-
|
70
|
-
def associated_object
|
71
|
-
option(:value) || source_serializer.send(name)
|
72
|
-
end
|
73
|
-
|
74
|
-
def embed_ids?
|
75
|
-
[:id, :ids].include? option(:embed, source_serializer._embed)
|
76
|
-
end
|
77
|
-
|
78
|
-
def embed_objects?
|
79
|
-
[:object, :objects].include? option(:embed, source_serializer._embed)
|
80
|
-
end
|
81
|
-
|
82
|
-
def embed_in_root?
|
83
|
-
option(:include, source_serializer._root_embed)
|
84
|
-
end
|
85
|
-
|
86
|
-
def embeddable?
|
87
|
-
!associated_object.nil?
|
88
|
-
end
|
89
|
-
|
90
|
-
protected
|
91
|
-
|
92
|
-
def find_serializable(object)
|
93
|
-
if target_serializer
|
94
|
-
target_serializer.new(object, source_serializer.options)
|
95
|
-
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
|
96
|
-
ams.new(object, source_serializer.options)
|
97
|
-
else
|
98
|
-
object
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
class HasMany < Config #:nodoc:
|
104
|
-
def key
|
105
|
-
if key = option(:key)
|
106
|
-
key
|
107
|
-
elsif embed_ids?
|
108
|
-
"#{@name.to_s.singularize}_ids".to_sym
|
109
|
-
else
|
110
|
-
@name
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def embed_key
|
115
|
-
if key = option(:embed_key)
|
116
|
-
key
|
117
|
-
else
|
118
|
-
:id
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def serialize
|
123
|
-
associated_object.map do |item|
|
124
|
-
find_serializable(item).serializable_hash
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def serializables
|
129
|
-
associated_object.map do |item|
|
130
|
-
find_serializable(item)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def serialize_ids
|
135
|
-
ids_key = "#{@name.to_s.singularize}_ids".to_sym
|
136
|
-
if !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(ids_key)
|
137
|
-
source_serializer.object.read_attribute_for_serialization(ids_key)
|
138
|
-
else
|
139
|
-
associated_object.map do |item|
|
140
|
-
item.read_attribute_for_serialization(embed_key)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
class HasOne < Config #:nodoc:
|
147
|
-
def embeddable?
|
148
|
-
if polymorphic? && associated_object.nil?
|
149
|
-
false
|
150
|
-
else
|
151
|
-
true
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
def polymorphic?
|
156
|
-
option :polymorphic
|
157
|
-
end
|
158
|
-
|
159
|
-
def root
|
160
|
-
if root = option(:root)
|
161
|
-
root
|
162
|
-
elsif polymorphic?
|
163
|
-
associated_object.class.to_s.pluralize.demodulize.underscore.to_sym
|
164
|
-
else
|
165
|
-
@name.to_s.pluralize.to_sym
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def key
|
170
|
-
if key = option(:key)
|
171
|
-
key
|
172
|
-
elsif embed_ids? && !polymorphic?
|
173
|
-
"#{@name}_id".to_sym
|
174
|
-
else
|
175
|
-
@name
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def embed_key
|
180
|
-
if key = option(:embed_key)
|
181
|
-
key
|
182
|
-
else
|
183
|
-
:id
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def polymorphic_key
|
188
|
-
associated_object.class.to_s.demodulize.underscore.to_sym
|
189
|
-
end
|
190
|
-
|
191
|
-
def serialize
|
192
|
-
object = associated_object
|
193
|
-
|
194
|
-
if object && polymorphic?
|
195
|
-
{
|
196
|
-
:type => polymorphic_key,
|
197
|
-
polymorphic_key => find_serializable(object).serializable_hash
|
198
|
-
}
|
199
|
-
elsif object
|
200
|
-
find_serializable(object).serializable_hash
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
def serializables
|
205
|
-
object = associated_object
|
206
|
-
value = object && find_serializable(object)
|
207
|
-
value ? [value] : []
|
208
|
-
end
|
209
|
-
|
210
|
-
def serialize_ids
|
211
|
-
id_key = "#{@name}_id".to_sym
|
212
|
-
|
213
|
-
if polymorphic?
|
214
|
-
if associated_object
|
215
|
-
{
|
216
|
-
:type => polymorphic_key,
|
217
|
-
:id => associated_object.read_attribute_for_serialization(embed_key)
|
218
|
-
}
|
219
|
-
else
|
220
|
-
nil
|
221
|
-
end
|
222
|
-
elsif !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key)
|
223
|
-
source_serializer.object.read_attribute_for_serialization(id_key)
|
224
|
-
elsif associated_object
|
225
|
-
associated_object.read_attribute_for_serialization(embed_key)
|
226
|
-
else
|
227
|
-
nil
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# We do not recommend that you use AM::S in this way, but if you must, here
|
2
|
-
# is a mixin that overrides ActiveRecord::Base#to_json and #as_json.
|
3
|
-
|
4
|
-
module ActiveRecord
|
5
|
-
module SerializerOverride
|
6
|
-
def to_json options = {}
|
7
|
-
active_model_serializer.new(self).to_json options
|
8
|
-
end
|
9
|
-
|
10
|
-
def as_json options={}
|
11
|
-
active_model_serializer.new(self).as_json options
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
Base.send(:include, SerializerOverride)
|
16
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module Rails
|
2
|
-
module Generators
|
3
|
-
class SerializerGenerator < NamedBase
|
4
|
-
source_root File.expand_path("../templates", __FILE__)
|
5
|
-
check_class_collision :suffix => "Serializer"
|
6
|
-
|
7
|
-
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
8
|
-
|
9
|
-
class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
|
10
|
-
|
11
|
-
def create_serializer_file
|
12
|
-
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
def generate_id_method
|
17
|
-
RUBY_VERSION =~ /1\.8/
|
18
|
-
end
|
19
|
-
|
20
|
-
def attributes_names
|
21
|
-
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
|
22
|
-
end
|
23
|
-
|
24
|
-
def association_names
|
25
|
-
attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
|
26
|
-
end
|
27
|
-
|
28
|
-
def parent_class_name
|
29
|
-
if options[:parent]
|
30
|
-
options[:parent]
|
31
|
-
# Only works on 3.2
|
32
|
-
# elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
|
33
|
-
# "ApplicationSerializer"
|
34
|
-
elsif defined?(::ApplicationSerializer)
|
35
|
-
"ApplicationSerializer"
|
36
|
-
else
|
37
|
-
"ActiveModel::Serializer"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
<% module_namespacing do -%>
|
2
|
-
class <%= class_name %>Serializer < <%= parent_class_name %>
|
3
|
-
attributes <%= attributes_names.map(&:inspect).join(", ") %>
|
4
|
-
<% association_names.each do |attribute| -%>
|
5
|
-
has_one :<%= attribute %>
|
6
|
-
<% end -%>
|
7
|
-
<% if generate_id_method %>
|
8
|
-
|
9
|
-
# due to the difference between 1.8 and 1.9 with respect to #id and
|
10
|
-
# #object_id, we recommend that if you wish to serialize id columns, you
|
11
|
-
# do this. Feel free to remove this if you don't feel that it's appropriate.
|
12
|
-
#
|
13
|
-
# For more: https://github.com/rails-api/active_model_serializers/issues/127
|
14
|
-
def id
|
15
|
-
object.read_attribute_for_serialization(:id)
|
16
|
-
end
|
17
|
-
<% end -%>
|
18
|
-
end
|
19
|
-
<% end -%>
|
@@ -1,75 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require "test_fakes"
|
3
|
-
|
4
|
-
class ArraySerializerTest < ActiveModel::TestCase
|
5
|
-
|
6
|
-
def test_array_items_do_not_have_root
|
7
|
-
array = [
|
8
|
-
BasicActiveModel.new(:name => "First model"),
|
9
|
-
BasicActiveModel.new(:name => "Second model")
|
10
|
-
]
|
11
|
-
expected = { "root" => [
|
12
|
-
{ :name => "First model" },
|
13
|
-
{ :name => "Second model" }
|
14
|
-
] }
|
15
|
-
|
16
|
-
default_serializer = array.active_model_serializer.new(array, :root => "root")
|
17
|
-
each_serializer = array.active_model_serializer.new(array, :root => "root", :each_serializer => BasicSerializer)
|
18
|
-
|
19
|
-
default_json = default_serializer.as_json
|
20
|
-
each_json = each_serializer.as_json
|
21
|
-
|
22
|
-
assert_equal(expected, default_json)
|
23
|
-
assert_equal(expected, each_json)
|
24
|
-
end
|
25
|
-
|
26
|
-
# serialize different typed objects
|
27
|
-
def test_array_serializer
|
28
|
-
model = Model.new
|
29
|
-
user = User.new
|
30
|
-
comments = Comment.new(:title => "Comment1", :id => 1)
|
31
|
-
|
32
|
-
array = [model, user, comments]
|
33
|
-
serializer = array.active_model_serializer.new(array, :scope => {:scope => true})
|
34
|
-
assert_equal([
|
35
|
-
{ :model => "Model" },
|
36
|
-
{ :last_name => "Valim", :ok => true, :first_name => "Jose", :scope => true },
|
37
|
-
{ :title => "Comment1" }
|
38
|
-
], serializer.as_json)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_array_serializer_with_root
|
42
|
-
comment1 = Comment.new(:title => "Comment1", :id => 1)
|
43
|
-
comment2 = Comment.new(:title => "Comment2", :id => 2)
|
44
|
-
|
45
|
-
array = [ comment1, comment2 ]
|
46
|
-
|
47
|
-
serializer = array.active_model_serializer.new(array, :root => :comments)
|
48
|
-
|
49
|
-
assert_equal({ :comments => [
|
50
|
-
{ :title => "Comment1" },
|
51
|
-
{ :title => "Comment2" }
|
52
|
-
]}, serializer.as_json)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_array_serializer_with_hash
|
56
|
-
hash = {:value => "something"}
|
57
|
-
array = [hash]
|
58
|
-
serializer = array.active_model_serializer.new(array, :root => :items)
|
59
|
-
assert_equal({ :items => [ hash.as_json ]}, serializer.as_json)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_array_serializer_with_specified_seriailizer
|
63
|
-
post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
|
64
|
-
post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
|
65
|
-
|
66
|
-
array = [ post1, post2 ]
|
67
|
-
|
68
|
-
serializer = array.active_model_serializer.new array, :each_serializer => CustomPostSerializer
|
69
|
-
|
70
|
-
assert_equal([
|
71
|
-
{ :title => "Post1" },
|
72
|
-
{ :title => "Post2" }
|
73
|
-
], serializer.as_json)
|
74
|
-
end
|
75
|
-
end
|