active_model_serializers 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +13 -13
- data/{gemfiles/Gemfile.edge-rails → Gemfile.edge} +1 -1
- data/README.md +141 -16
- data/RELEASE_NOTES.md +1 -1
- data/lib/action_controller/serialization.rb +8 -1
- data/lib/active_model/array_serializer.rb +21 -13
- data/lib/active_model/serializer.rb +60 -199
- data/lib/active_model/serializer/associations.rb +226 -0
- data/lib/active_model/serializers/version.rb +1 -1
- data/lib/active_model_serializers.rb +3 -5
- data/test/array_serializer_test.rb +55 -0
- data/test/association_test.rb +151 -36
- data/test/generators_test.rb +4 -0
- data/test/serialization_test.rb +80 -3
- data/test/serializer_support_test.rb +11 -0
- data/test/serializer_test.rb +153 -231
- data/test/test_fakes.rb +182 -0
- metadata +41 -28
- data/lib/active_model/ordered_set.rb +0 -25
data/test/test_fakes.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
class Model
|
2
|
+
def initialize(hash={})
|
3
|
+
@attributes = hash
|
4
|
+
end
|
5
|
+
|
6
|
+
def read_attribute_for_serialization(name)
|
7
|
+
@attributes[name]
|
8
|
+
end
|
9
|
+
|
10
|
+
def as_json(*)
|
11
|
+
{ :model => "Model" }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class User
|
16
|
+
include ActiveModel::SerializerSupport
|
17
|
+
|
18
|
+
attr_accessor :superuser
|
19
|
+
|
20
|
+
def initialize(hash={})
|
21
|
+
@attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password")
|
22
|
+
end
|
23
|
+
|
24
|
+
def read_attribute_for_serialization(name)
|
25
|
+
@attributes[name]
|
26
|
+
end
|
27
|
+
|
28
|
+
def super_user?
|
29
|
+
@superuser
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Post < Model
|
34
|
+
def initialize(attributes)
|
35
|
+
super(attributes)
|
36
|
+
self.comments ||= []
|
37
|
+
self.comments_disabled = false
|
38
|
+
self.author = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_accessor :comments, :comments_disabled, :author
|
42
|
+
def active_model_serializer; PostSerializer; end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Comment < Model
|
46
|
+
def active_model_serializer; CommentSerializer; end
|
47
|
+
end
|
48
|
+
|
49
|
+
class UserSerializer < ActiveModel::Serializer
|
50
|
+
attributes :first_name, :last_name
|
51
|
+
|
52
|
+
def serializable_hash
|
53
|
+
attributes.merge(:ok => true).merge(options[:scope])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class UserAttributesWithKeySerializer < ActiveModel::Serializer
|
58
|
+
attributes :first_name => :f_name, :last_name => :l_name
|
59
|
+
|
60
|
+
def serializable_hash
|
61
|
+
attributes.merge(:ok => true).merge(options[:scope])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class UserAttributesWithSomeKeySerializer < ActiveModel::Serializer
|
66
|
+
attributes :first_name, :last_name => :l_name
|
67
|
+
|
68
|
+
def serializable_hash
|
69
|
+
attributes.merge(:ok => true).merge(options[:scope])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class DefaultUserSerializer < ActiveModel::Serializer
|
74
|
+
attributes :first_name, :last_name
|
75
|
+
end
|
76
|
+
|
77
|
+
class MyUserSerializer < ActiveModel::Serializer
|
78
|
+
attributes :first_name, :last_name
|
79
|
+
|
80
|
+
def serializable_hash
|
81
|
+
hash = attributes
|
82
|
+
hash = hash.merge(:super_user => true) if object.super_user?
|
83
|
+
hash
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class CommentSerializer
|
88
|
+
def initialize(comment, options={})
|
89
|
+
@object = comment
|
90
|
+
end
|
91
|
+
|
92
|
+
attr_reader :object
|
93
|
+
|
94
|
+
def serializable_hash
|
95
|
+
{ :title => @object.read_attribute_for_serialization(:title) }
|
96
|
+
end
|
97
|
+
|
98
|
+
def as_json(options=nil)
|
99
|
+
options ||= {}
|
100
|
+
if options[:root] == false
|
101
|
+
serializable_hash
|
102
|
+
else
|
103
|
+
{ :comment => serializable_hash }
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class PostSerializer < ActiveModel::Serializer
|
109
|
+
attributes :title, :body
|
110
|
+
has_many :comments, :serializer => CommentSerializer
|
111
|
+
end
|
112
|
+
|
113
|
+
class PostWithConditionalCommentsSerializer < ActiveModel::Serializer
|
114
|
+
root :post
|
115
|
+
attributes :title, :body
|
116
|
+
has_many :comments, :serializer => CommentSerializer
|
117
|
+
|
118
|
+
def include_associations!
|
119
|
+
include! :comments unless object.comments_disabled
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class PostWithMultipleConditionalsSerializer < ActiveModel::Serializer
|
124
|
+
root :post
|
125
|
+
attributes :title, :body, :author
|
126
|
+
has_many :comments, :serializer => CommentSerializer
|
127
|
+
|
128
|
+
def include_comments?
|
129
|
+
!object.comments_disabled
|
130
|
+
end
|
131
|
+
|
132
|
+
def include_author?
|
133
|
+
scope.super_user?
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class Blog < Model
|
138
|
+
attr_accessor :author
|
139
|
+
end
|
140
|
+
|
141
|
+
class AuthorSerializer < ActiveModel::Serializer
|
142
|
+
attributes :first_name, :last_name
|
143
|
+
end
|
144
|
+
|
145
|
+
class BlogSerializer < ActiveModel::Serializer
|
146
|
+
has_one :author, :serializer => AuthorSerializer
|
147
|
+
end
|
148
|
+
|
149
|
+
class CustomPostSerializer < ActiveModel::Serializer
|
150
|
+
attributes :title
|
151
|
+
end
|
152
|
+
|
153
|
+
class CustomBlog < Blog
|
154
|
+
attr_accessor :public_posts, :public_user
|
155
|
+
end
|
156
|
+
|
157
|
+
class CustomBlogSerializer < ActiveModel::Serializer
|
158
|
+
has_many :public_posts, :key => :posts, :serializer => PostSerializer
|
159
|
+
has_one :public_user, :key => :user, :serializer => UserSerializer
|
160
|
+
end
|
161
|
+
|
162
|
+
class SomeSerializer < ActiveModel::Serializer
|
163
|
+
attributes :some
|
164
|
+
end
|
165
|
+
|
166
|
+
class SomeObject < Struct.new(:some)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Set up some classes for polymorphic testing
|
170
|
+
class Attachment < Model
|
171
|
+
def attachable
|
172
|
+
@attributes[:attachable]
|
173
|
+
end
|
174
|
+
|
175
|
+
def readable
|
176
|
+
@attributes[:readable]
|
177
|
+
end
|
178
|
+
|
179
|
+
def edible
|
180
|
+
@attributes[:edible]
|
181
|
+
end
|
182
|
+
end
|
metadata
CHANGED
@@ -1,8 +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
|
-
prerelease:
|
4
|
+
version: 0.7.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- José Valim
|
@@ -10,52 +9,64 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2013-03-06 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: activemodel
|
17
|
-
requirement:
|
18
|
-
none: false
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
19
17
|
requirements:
|
20
|
-
- -
|
18
|
+
- - '>='
|
21
19
|
- !ruby/object:Gem::Version
|
22
20
|
version: '3.0'
|
23
21
|
type: :runtime
|
24
22
|
prerelease: false
|
25
|
-
version_requirements:
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '3.0'
|
26
28
|
- !ruby/object:Gem::Dependency
|
27
29
|
name: rails
|
28
|
-
requirement:
|
29
|
-
none: false
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
|
-
- -
|
32
|
+
- - '>='
|
32
33
|
- !ruby/object:Gem::Version
|
33
34
|
version: '3.0'
|
34
35
|
type: :development
|
35
36
|
prerelease: false
|
36
|
-
version_requirements:
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '3.0'
|
37
42
|
- !ruby/object:Gem::Dependency
|
38
43
|
name: pry
|
39
|
-
requirement:
|
40
|
-
none: false
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
41
45
|
requirements:
|
42
|
-
- -
|
46
|
+
- - '>='
|
43
47
|
- !ruby/object:Gem::Version
|
44
48
|
version: '0'
|
45
49
|
type: :development
|
46
50
|
prerelease: false
|
47
|
-
version_requirements:
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
48
56
|
- !ruby/object:Gem::Dependency
|
49
57
|
name: simplecov
|
50
|
-
requirement:
|
51
|
-
none: false
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
52
59
|
requirements:
|
53
|
-
- -
|
60
|
+
- - '>='
|
54
61
|
- !ruby/object:Gem::Version
|
55
62
|
version: '0'
|
56
63
|
type: :development
|
57
64
|
prerelease: false
|
58
|
-
version_requirements:
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
59
70
|
description: Making it easy to serialize models for client-side use
|
60
71
|
email:
|
61
72
|
- jose.valim@gmail.com
|
@@ -68,61 +79,63 @@ files:
|
|
68
79
|
- .travis.yml
|
69
80
|
- DESIGN.textile
|
70
81
|
- Gemfile
|
82
|
+
- Gemfile.edge
|
71
83
|
- MIT-LICENSE.txt
|
72
84
|
- README.md
|
73
85
|
- RELEASE_NOTES.md
|
74
86
|
- Rakefile
|
75
87
|
- active_model_serializers.gemspec
|
76
88
|
- cruft.md
|
77
|
-
- gemfiles/Gemfile.edge-rails
|
78
89
|
- lib/action_controller/serialization.rb
|
79
90
|
- lib/active_model/array_serializer.rb
|
80
|
-
- lib/active_model/ordered_set.rb
|
81
91
|
- lib/active_model/serializer.rb
|
92
|
+
- lib/active_model/serializer/associations.rb
|
82
93
|
- lib/active_model/serializers/version.rb
|
83
94
|
- lib/active_model_serializers.rb
|
84
95
|
- lib/generators/resource_override.rb
|
85
96
|
- lib/generators/serializer/USAGE
|
86
97
|
- lib/generators/serializer/serializer_generator.rb
|
87
98
|
- lib/generators/serializer/templates/serializer.rb
|
99
|
+
- test/array_serializer_test.rb
|
88
100
|
- test/association_test.rb
|
89
101
|
- test/generators_test.rb
|
90
102
|
- test/no_serialization_scope_test.rb
|
91
103
|
- test/serialization_test.rb
|
92
104
|
- test/serializer_support_test.rb
|
93
105
|
- test/serializer_test.rb
|
106
|
+
- test/test_fakes.rb
|
94
107
|
- test/test_helper.rb
|
95
108
|
homepage: https://github.com/josevalim/active_model_serializers
|
96
109
|
licenses: []
|
110
|
+
metadata: {}
|
97
111
|
post_install_message:
|
98
112
|
rdoc_options: []
|
99
113
|
require_paths:
|
100
114
|
- lib
|
101
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
-
none: false
|
103
116
|
requirements:
|
104
|
-
- -
|
117
|
+
- - '>='
|
105
118
|
- !ruby/object:Gem::Version
|
106
119
|
version: '0'
|
107
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
121
|
requirements:
|
110
|
-
- -
|
122
|
+
- - '>='
|
111
123
|
- !ruby/object:Gem::Version
|
112
124
|
version: '0'
|
113
125
|
requirements: []
|
114
126
|
rubyforge_project:
|
115
|
-
rubygems_version:
|
127
|
+
rubygems_version: 2.0.0
|
116
128
|
signing_key:
|
117
|
-
specification_version:
|
129
|
+
specification_version: 4
|
118
130
|
summary: Bringing consistency and object orientation to model serialization. Works
|
119
131
|
great for client-side MVC frameworks!
|
120
132
|
test_files:
|
133
|
+
- test/array_serializer_test.rb
|
121
134
|
- test/association_test.rb
|
122
135
|
- test/generators_test.rb
|
123
136
|
- test/no_serialization_scope_test.rb
|
124
137
|
- test/serialization_test.rb
|
125
138
|
- test/serializer_support_test.rb
|
126
139
|
- test/serializer_test.rb
|
140
|
+
- test/test_fakes.rb
|
127
141
|
- test/test_helper.rb
|
128
|
-
has_rdoc:
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class OrderedSet
|
3
|
-
def initialize(array)
|
4
|
-
@array = array
|
5
|
-
@hash = {}
|
6
|
-
|
7
|
-
array.each do |item|
|
8
|
-
@hash[item] = true
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def merge!(other)
|
13
|
-
other.each do |item|
|
14
|
-
next if @hash.key?(item)
|
15
|
-
|
16
|
-
@hash[item] = true
|
17
|
-
@array.push item
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_a
|
22
|
-
@array
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|