active_model_serializers 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +4 -4
- data/lib/action_controller/serialization.rb +1 -1
- data/lib/active_model/serializer.rb +23 -5
- data/lib/active_model/serializers/version.rb +1 -1
- data/lib/active_model_serializers.rb +3 -0
- data/lib/generators/serializer/serializer_generator.rb +1 -1
- data/lib/generators/serializer/templates/serializer.rb +2 -3
- data/test/association_test.rb +0 -18
- data/test/generators_test.rb +2 -2
- data/test/serialization_test.rb +11 -1
- data/test/serializer_test.rb +14 -6
- metadata +17 -8
data/README.markdown
CHANGED
@@ -126,12 +126,12 @@ class PostSerializer < ActiveModel::Serializer
|
|
126
126
|
|
127
127
|
# only let the user see comments he created.
|
128
128
|
def comments
|
129
|
-
post.comments.where(:created_by =>
|
129
|
+
post.comments.where(:created_by => options[:scope])
|
130
130
|
end
|
131
131
|
end
|
132
132
|
```
|
133
133
|
|
134
|
-
In a serializer,
|
134
|
+
In a serializer, `options[:scope]` is the current authorization scope (usually
|
135
135
|
`current_user`), which the controller gives to the serializer when you call
|
136
136
|
`render :json`
|
137
137
|
|
@@ -219,11 +219,11 @@ this:
|
|
219
219
|
"id": 1,
|
220
220
|
"title": "New post",
|
221
221
|
"body": "A body!",
|
222
|
-
"comments": [ 1 ]
|
222
|
+
"comments": [ 1, 2 ]
|
223
223
|
},
|
224
224
|
"comments": [
|
225
225
|
{ "id": 1, "body": "what a dumb post", "tags": [ 1, 2 ] },
|
226
|
-
{ "id":
|
226
|
+
{ "id": 2, "body": "i liked it", "tags": [ 1, 3 ] },
|
227
227
|
],
|
228
228
|
"tags": [
|
229
229
|
{ "id": 1, "name": "short" },
|
@@ -41,7 +41,7 @@ module ActionController
|
|
41
41
|
|
42
42
|
def _render_option_json(json, options)
|
43
43
|
if json.respond_to?(:to_ary)
|
44
|
-
options[:root] ||= controller_name
|
44
|
+
options[:root] ||= controller_name unless options[:root] == false
|
45
45
|
end
|
46
46
|
|
47
47
|
serializer = options.delete(:serializer) ||
|
@@ -52,7 +52,15 @@ module ActiveModel
|
|
52
52
|
@options[:hash] = hash = {}
|
53
53
|
@options[:unique_values] = {}
|
54
54
|
|
55
|
-
array = serializable_array.map
|
55
|
+
array = serializable_array.map do |item|
|
56
|
+
if item.is_a?(Hash)
|
57
|
+
item
|
58
|
+
elsif item.respond_to?(:serializable_hash)
|
59
|
+
item.serializable_hash
|
60
|
+
else
|
61
|
+
item.as_json
|
62
|
+
end
|
63
|
+
end
|
56
64
|
|
57
65
|
if root = @options[:root]
|
58
66
|
hash.merge!(root => array)
|
@@ -91,7 +99,7 @@ module ActiveModel
|
|
91
99
|
# end
|
92
100
|
#
|
93
101
|
# def author?
|
94
|
-
# post.author == scope
|
102
|
+
# post.author == options[:scope]
|
95
103
|
# end
|
96
104
|
# end
|
97
105
|
#
|
@@ -402,9 +410,13 @@ module ActiveModel
|
|
402
410
|
# Returns a hash representation of the serializable
|
403
411
|
# object without the root.
|
404
412
|
def serializable_hash
|
405
|
-
|
406
|
-
|
407
|
-
|
413
|
+
instrument(:serialize, :serializer => self.class.name) do
|
414
|
+
node = attributes
|
415
|
+
instrument :associations do
|
416
|
+
include_associations!(node) if _embed
|
417
|
+
end
|
418
|
+
node
|
419
|
+
end
|
408
420
|
end
|
409
421
|
|
410
422
|
def include_associations!(node)
|
@@ -505,6 +517,12 @@ module ActiveModel
|
|
505
517
|
end
|
506
518
|
|
507
519
|
alias :read_attribute_for_serialization :send
|
520
|
+
|
521
|
+
# Use ActiveSupport::Notifications to send events to external systems.
|
522
|
+
# The event name is: name.class_name.serializer
|
523
|
+
def instrument(name, payload = {}, &block)
|
524
|
+
ActiveSupport::Notifications.instrument("#{name}.serializer", payload, &block)
|
525
|
+
end
|
508
526
|
end
|
509
527
|
end
|
510
528
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "active_support"
|
2
2
|
require "active_support/core_ext/string/inflections"
|
3
|
+
require "active_support/notifications"
|
3
4
|
require "active_model"
|
4
5
|
require "active_model/serializer"
|
5
6
|
|
@@ -7,6 +8,8 @@ if defined?(Rails)
|
|
7
8
|
module ActiveModel
|
8
9
|
class Railtie < Rails::Railtie
|
9
10
|
generators do |app|
|
11
|
+
app ||= Rails.application # Rails 3.0.x does not yield `app`
|
12
|
+
|
10
13
|
Rails::Generators.configure!(app.config.generators)
|
11
14
|
require "generators/resource_override"
|
12
15
|
end
|
@@ -1,9 +1,8 @@
|
|
1
1
|
<% module_namespacing do -%>
|
2
2
|
class <%= class_name %>Serializer < <%= parent_class_name %>
|
3
|
-
|
4
|
-
<% end -%>
|
3
|
+
attributes <%= attributes_names.map(&:inspect).join(", ") %>
|
5
4
|
<% association_names.each do |attribute| -%>
|
6
5
|
has_one :<%= attribute %>
|
7
6
|
<% end -%>
|
8
7
|
end
|
9
|
-
<% end -%>
|
8
|
+
<% end -%>
|
data/test/association_test.rb
CHANGED
@@ -185,24 +185,6 @@ class AssociationTest < ActiveModel::TestCase
|
|
185
185
|
}, @root_hash)
|
186
186
|
end
|
187
187
|
|
188
|
-
def test_with_default_has_one_with_custom_key
|
189
|
-
@post_serializer_class.class_eval do
|
190
|
-
has_one :comment, :key => :custom_comment
|
191
|
-
end
|
192
|
-
|
193
|
-
include! :comment
|
194
|
-
|
195
|
-
assert_equal({
|
196
|
-
:custom_comment => 1
|
197
|
-
}, @hash)
|
198
|
-
|
199
|
-
assert_equal({
|
200
|
-
:custom_comments => [
|
201
|
-
{ :id => 1, :body => "ZOMG A COMMENT" }
|
202
|
-
]
|
203
|
-
}, @root_hash)
|
204
|
-
end
|
205
|
-
|
206
188
|
def test_embed_objects_for_has_many_associations
|
207
189
|
@post_serializer_class.class_eval do
|
208
190
|
has_many :comments, :embed => :objects
|
data/test/generators_test.rb
CHANGED
@@ -55,13 +55,13 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
|
|
55
55
|
def test_generates_attributes_and_associations
|
56
56
|
run_generator
|
57
57
|
assert_file "app/serializers/account_serializer.rb" do |serializer|
|
58
|
-
assert_match(/^ attributes :name, :description$/, serializer)
|
58
|
+
assert_match(/^ attributes :id, :name, :description$/, serializer)
|
59
59
|
assert_match(/^ has_one :business$/, serializer)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_with_no_attributes_does_not_add_extra_space
|
64
64
|
run_generator ["account"]
|
65
|
-
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer\nend/
|
65
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer\n attributes :id\nend/
|
66
66
|
end
|
67
67
|
end
|
data/test/serialization_test.rb
CHANGED
@@ -140,6 +140,11 @@ class RenderJsonTest < ActionController::TestCase
|
|
140
140
|
render :json => HypermediaSerializable.new
|
141
141
|
end
|
142
142
|
|
143
|
+
def render_json_array_with_no_root
|
144
|
+
render :json => [], :root => false
|
145
|
+
end
|
146
|
+
|
147
|
+
|
143
148
|
private
|
144
149
|
def default_serializer_options
|
145
150
|
if params[:check_defaults]
|
@@ -217,7 +222,7 @@ class RenderJsonTest < ActionController::TestCase
|
|
217
222
|
assert_match '"object":{"serializable_object":true}', @response.body
|
218
223
|
end
|
219
224
|
|
220
|
-
def
|
225
|
+
def test_render_json_with_serializer_checking_defaults
|
221
226
|
get :render_json_with_serializer, :check_defaults => true
|
222
227
|
assert_match '"scope":{"current_user":true}', @response.body
|
223
228
|
assert_match '"object":{"serializable_object":true}', @response.body
|
@@ -250,4 +255,9 @@ class RenderJsonTest < ActionController::TestCase
|
|
250
255
|
get :render_json_with_links
|
251
256
|
assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
|
252
257
|
end
|
258
|
+
|
259
|
+
def test_render_json_array_with_no_root
|
260
|
+
get :render_json_array_with_no_root
|
261
|
+
assert_equal '[]', @response.body
|
262
|
+
end
|
253
263
|
end
|
data/test/serializer_test.rb
CHANGED
@@ -71,8 +71,8 @@ class SerializerTest < ActiveModel::TestCase
|
|
71
71
|
end
|
72
72
|
|
73
73
|
class CommentSerializer
|
74
|
-
def initialize(comment,
|
75
|
-
@comment
|
74
|
+
def initialize(comment, options={})
|
75
|
+
@comment = comment
|
76
76
|
end
|
77
77
|
|
78
78
|
def serializable_hash
|
@@ -375,21 +375,22 @@ class SerializerTest < ActiveModel::TestCase
|
|
375
375
|
}, serializer.as_json)
|
376
376
|
end
|
377
377
|
|
378
|
+
# serialize different typed objects
|
378
379
|
def test_array_serializer
|
379
380
|
model = Model.new
|
380
381
|
user = User.new
|
381
382
|
comments = Comment.new(:title => "Comment1", :id => 1)
|
382
383
|
|
383
384
|
array = [model, user, comments]
|
384
|
-
serializer = array.active_model_serializer.new(array, {:scope => true})
|
385
|
+
serializer = array.active_model_serializer.new(array, :scope => {:scope => true})
|
385
386
|
assert_equal([
|
386
387
|
{ :model => "Model" },
|
387
|
-
{ :
|
388
|
-
{ :
|
388
|
+
{ :last_name => "Valim", :ok => true, :first_name => "Jose", :scope => true },
|
389
|
+
{ :title => "Comment1" }
|
389
390
|
], serializer.as_json)
|
390
391
|
end
|
391
392
|
|
392
|
-
def
|
393
|
+
def test_array_serializer_with_root
|
393
394
|
comment1 = Comment.new(:title => "Comment1", :id => 1)
|
394
395
|
comment2 = Comment.new(:title => "Comment2", :id => 2)
|
395
396
|
|
@@ -403,6 +404,13 @@ class SerializerTest < ActiveModel::TestCase
|
|
403
404
|
]}, serializer.as_json)
|
404
405
|
end
|
405
406
|
|
407
|
+
def test_array_serializer_with_hash
|
408
|
+
hash = {:value => "something"}
|
409
|
+
array = [hash]
|
410
|
+
serializer = array.active_model_serializer.new(array, :root => :items)
|
411
|
+
assert_equal({ :items => [ hash ]}, serializer.as_json)
|
412
|
+
end
|
413
|
+
|
406
414
|
class CustomBlog < Blog
|
407
415
|
attr_accessor :public_posts, :public_user
|
408
416
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-05
|
13
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: '3.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '3.0'
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: rails
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ~>
|
@@ -33,7 +38,12 @@ dependencies:
|
|
33
38
|
version: '3.0'
|
34
39
|
type: :development
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.0'
|
37
47
|
description: Making it easy to serialize models for client-side use
|
38
48
|
email:
|
39
49
|
- jose.valim@gmail.com
|
@@ -85,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
95
|
version: '0'
|
86
96
|
requirements: []
|
87
97
|
rubyforge_project:
|
88
|
-
rubygems_version: 1.8.
|
98
|
+
rubygems_version: 1.8.24
|
89
99
|
signing_key:
|
90
100
|
specification_version: 3
|
91
101
|
summary: Bringing consistency and object orientation to model serialization. Works
|
@@ -97,4 +107,3 @@ test_files:
|
|
97
107
|
- test/serializer_support_test.rb
|
98
108
|
- test/serializer_test.rb
|
99
109
|
- test/test_helper.rb
|
100
|
-
has_rdoc:
|