forestadmin-jsonapi-serializers 2.0.0.pre.beta.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require 'factory_girl'
2
+ require './lib/jsonapi-serializers'
3
+ require './spec/support/serializers'
4
+
5
+ RSpec.configure do |config|
6
+ config.include FactoryGirl::Syntax::Methods
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.verify_partial_doubles = true
10
+ end
11
+
12
+ config.before(:each) do
13
+ # Force FactoryGirl sequences to be fully reset before each test run to simplify ID testing
14
+ # since we are not using a database or real fixtures. Inside of each test case, IDs will
15
+ # increment per type starting at 1.
16
+ FactoryGirl.reload
17
+ load './spec/support/factory.rb'
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ require 'factory_girl'
2
+
3
+ FactoryGirl.define do
4
+ factory :post, class: MyApp::Post do
5
+ skip_create
6
+ sequence(:id) {|n| n }
7
+ sequence(:title) {|n| "Title for Post #{n}" }
8
+ sequence(:body) {|n| "Body for Post #{n}" }
9
+
10
+ trait :with_author do
11
+ association :author, factory: :user
12
+ end
13
+ end
14
+
15
+ # Post with some added property to test inheritance in serializer.
16
+ factory :tagged_post, class: MyApp::TaggedPost do
17
+ skip_create
18
+ sequence(:id) {|n| n }
19
+ sequence(:title) {|n| "Title for TaggedPost #{n}" }
20
+ sequence(:body) {|n| "Body for TaggedPost #{n}" }
21
+ sequence(:tag) {|n| "Tag for TaggedPost #{n}" }
22
+
23
+ trait :with_author do
24
+ association :author, factory: :user
25
+ end
26
+ end
27
+
28
+ factory :long_comment, class: MyApp::LongComment do
29
+ skip_create
30
+ sequence(:id) {|n| n }
31
+ sequence(:body) {|n| "Body for LongComment #{n}" }
32
+ end
33
+
34
+ factory :user, class: MyApp::User do
35
+ skip_create
36
+ sequence(:id) {|n| n }
37
+ sequence(:name) {|n| "User ##{n}"}
38
+ end
39
+
40
+ factory :underscore_test, class: MyApp::UnderscoreTest do
41
+ skip_create
42
+ sequence(:id) {|n| n }
43
+ end
44
+ end
@@ -0,0 +1,264 @@
1
+ module MyApp
2
+ class Post
3
+ attr_accessor :id
4
+ attr_accessor :title
5
+ attr_accessor :body
6
+ attr_accessor :author
7
+ attr_accessor :long_comments
8
+ end
9
+
10
+ class TaggedPost
11
+ attr_accessor :id
12
+ attr_accessor :title
13
+ attr_accessor :body
14
+ attr_accessor :tag
15
+ attr_accessor :author
16
+ attr_accessor :long_comments
17
+ end
18
+
19
+ class LongComment
20
+ attr_accessor :id
21
+ attr_accessor :body
22
+ attr_accessor :user
23
+ attr_accessor :post
24
+
25
+ # Just a copy of the default implementation, we need this to exist to be able to stub in tests.
26
+ def jsonapi_serializer_class_name
27
+ 'MyApp::LongCommentSerializer'
28
+ end
29
+ end
30
+
31
+ class User
32
+ attr_accessor :id
33
+ attr_accessor :name
34
+
35
+ def jsonapi_serializer_class_name
36
+ 'MyAppOtherNamespace::UserSerializer'
37
+ end
38
+ end
39
+
40
+ class UnderscoreTest
41
+ attr_accessor :id
42
+
43
+ def tagged_posts
44
+ []
45
+ end
46
+ end
47
+
48
+ class PostSerializer
49
+ include ForestAdmin::JSONAPI::Serializer
50
+
51
+ attribute :title
52
+ attribute :long_content do
53
+ object.body
54
+ end
55
+
56
+ has_one :author
57
+ has_many :long_comments
58
+ end
59
+
60
+ class UnderscoreTestSerializer
61
+ include ForestAdmin::JSONAPI::Serializer
62
+
63
+ has_many :tagged_posts
64
+
65
+ def format_name(attribute_name)
66
+ attribute_name.to_s.underscore
67
+ end
68
+ end
69
+
70
+ class LongCommentSerializer
71
+ include ForestAdmin::JSONAPI::Serializer
72
+
73
+ attribute :body
74
+ has_one :user
75
+
76
+ # Circular-reference back to post.
77
+ has_one :post
78
+ end
79
+
80
+ # More customized, one-off serializers to test particular behaviors:
81
+
82
+ class SimplestPostSerializer
83
+ include ForestAdmin::JSONAPI::Serializer
84
+
85
+ attribute :title
86
+ attribute :long_content do
87
+ object.body
88
+ end
89
+
90
+ def type
91
+ :posts
92
+ end
93
+ end
94
+
95
+ class PostSerializerWithMetadata
96
+ include ForestAdmin::JSONAPI::Serializer
97
+
98
+ attribute :title
99
+ attribute :long_content do
100
+ object.body
101
+ end
102
+
103
+ def type
104
+ 'posts' # Intentionally test string type.
105
+ end
106
+
107
+ def meta
108
+ {
109
+ 'copyright' => 'Copyright 2015 Example Corp.',
110
+ 'authors' => ['Aliens'],
111
+ }
112
+ end
113
+ end
114
+
115
+ class PostSerializerWithoutIncludeLinks
116
+ include ForestAdmin::JSONAPI::Serializer
117
+
118
+ attribute :title
119
+
120
+ has_one :author, include_links: false
121
+ has_many :long_comments, include_links: false
122
+
123
+ def type
124
+ :posts
125
+ end
126
+ end
127
+
128
+ class PostSerializerWithIncludeData
129
+ include ForestAdmin::JSONAPI::Serializer
130
+
131
+ attribute :title
132
+
133
+ has_one :author, include_data: true
134
+ has_many :long_comments, include_data: true
135
+
136
+ def type
137
+ :posts
138
+ end
139
+ end
140
+
141
+ class PostSerializerWithContext < PostSerializer
142
+ attribute :body, if: :show_body?, unless: :hide_body?
143
+
144
+ def show_body?
145
+ context.fetch(:show_body, true)
146
+ end
147
+
148
+ def hide_body?
149
+ context.fetch(:hide_body, false)
150
+ end
151
+ end
152
+
153
+ class LongCommentsSerializerWithContext
154
+ include ForestAdmin::JSONAPI::Serializer
155
+
156
+ attribute :body, if: :show_body?
157
+ has_one :user, if: :show_comments_user?
158
+
159
+ def show_body?
160
+ context.fetch(:show_body, true)
161
+ end
162
+
163
+ def show_comments_user?
164
+ context.fetch(:show_comments_user, true)
165
+ end
166
+ end
167
+
168
+
169
+ class PostSerializerWithoutLinks
170
+ include ForestAdmin::JSONAPI::Serializer
171
+
172
+ attribute :title
173
+ attribute :long_content do
174
+ object.body
175
+ end
176
+
177
+ has_one :author
178
+ has_many :long_comments
179
+
180
+ def self_link
181
+ nil
182
+ end
183
+
184
+ def relationship_self_link(attribute_name)
185
+ nil
186
+ end
187
+
188
+ def relationship_related_link(attribute_name)
189
+ nil
190
+ end
191
+ end
192
+
193
+ class PostSerializerWithBaseUrl
194
+ include ForestAdmin::JSONAPI::Serializer
195
+
196
+ def base_url
197
+ 'http://example.com'
198
+ end
199
+
200
+ attribute :title
201
+ attribute :long_content do
202
+ object.body
203
+ end
204
+
205
+ has_one :author
206
+ has_many :long_comments
207
+ end
208
+
209
+ class EmptySerializer
210
+ include ForestAdmin::JSONAPI::Serializer
211
+ end
212
+
213
+ class MultipleAttributesSerializer
214
+ include ForestAdmin::JSONAPI::Serializer
215
+
216
+ attributes :title, :body
217
+ end
218
+
219
+ class PostSerializerWithInheritedProperties < PostSerializer
220
+ # Add only :tag, inherit the rest.
221
+ attribute :tag
222
+ end
223
+ end
224
+
225
+ # Test the `jsonapi_serializer_class_name` override method for serializers in different namespaces.
226
+ # There is no explicit test for this, just implicit tests that correctly serialize User objects.
227
+ module MyAppOtherNamespace
228
+ class UserSerializer
229
+ include ForestAdmin::JSONAPI::Serializer
230
+
231
+ attribute :name
232
+ end
233
+ end
234
+
235
+ module Api
236
+ module V1
237
+ module MyApp
238
+ class UserSerializer
239
+ include ForestAdmin::JSONAPI::Serializer
240
+
241
+ attribute :name
242
+ end
243
+
244
+ class PostSerializer
245
+ include ForestAdmin::JSONAPI::Serializer
246
+
247
+ attribute :title
248
+
249
+ has_one :author
250
+ has_many :long_comments
251
+ end
252
+
253
+ class LongCommentSerializer
254
+ include ForestAdmin::JSONAPI::Serializer
255
+
256
+ attribute :body
257
+ has_one :user
258
+
259
+ # Circular-reference back to post.
260
+ has_one :post
261
+ end
262
+ end
263
+ end
264
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forestadmin-jsonapi-serializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0.pre.beta.2
5
+ platform: ruby
6
+ authors:
7
+ - Mike Fotinakis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activemodel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Pure Ruby readonly serializers for the JSON:API spec.
112
+ email:
113
+ - mike@fotinakis.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - Gemfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - forestadmin-jsonapi-serializers.gemspec
125
+ - lib/jsonapi-serializers.rb
126
+ - lib/jsonapi-serializers/attributes.rb
127
+ - lib/jsonapi-serializers/serializer.rb
128
+ - lib/jsonapi-serializers/version.rb
129
+ - spec/serializer_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/factory.rb
132
+ - spec/support/serializers.rb
133
+ homepage: https://github.com/forestadmin/jsonapi-serializers
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">"
149
+ - !ruby/object:Gem::Version
150
+ version: 1.3.1
151
+ requirements: []
152
+ rubygems_version: 3.1.4
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Pure Ruby readonly serializers for the JSON:API spec.
156
+ test_files:
157
+ - spec/serializer_spec.rb
158
+ - spec/spec_helper.rb
159
+ - spec/support/factory.rb
160
+ - spec/support/serializers.rb