cheap_ams 0.10.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +26 -0
  4. data/CHANGELOG.md +13 -0
  5. data/CONTRIBUTING.md +31 -0
  6. data/Gemfile +35 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +348 -0
  9. data/Rakefile +12 -0
  10. data/appveyor.yml +25 -0
  11. data/cheap_ams.gemspec +49 -0
  12. data/docs/README.md +27 -0
  13. data/docs/general/adapters.md +51 -0
  14. data/docs/general/getting_started.md +73 -0
  15. data/docs/howto/add_pagination_links.md +112 -0
  16. data/docs/howto/add_root_key.md +51 -0
  17. data/lib/action_controller/serialization.rb +62 -0
  18. data/lib/active_model/serializable_resource.rb +84 -0
  19. data/lib/active_model/serializer/adapter/flatten_json.rb +19 -0
  20. data/lib/active_model/serializer/adapter/fragment_cache.rb +82 -0
  21. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +16 -0
  22. data/lib/active_model/serializer/adapter/json.rb +53 -0
  23. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +24 -0
  24. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
  25. data/lib/active_model/serializer/adapter/json_api.rb +183 -0
  26. data/lib/active_model/serializer/adapter/null.rb +11 -0
  27. data/lib/active_model/serializer/adapter.rb +98 -0
  28. data/lib/active_model/serializer/array_serializer.rb +35 -0
  29. data/lib/active_model/serializer/association.rb +21 -0
  30. data/lib/active_model/serializer/associations.rb +97 -0
  31. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  32. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  33. data/lib/active_model/serializer/configuration.rb +14 -0
  34. data/lib/active_model/serializer/fieldset.rb +42 -0
  35. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  36. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  37. data/lib/active_model/serializer/lint.rb +131 -0
  38. data/lib/active_model/serializer/railtie.rb +9 -0
  39. data/lib/active_model/serializer/reflection.rb +74 -0
  40. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  41. data/lib/active_model/serializer/version.rb +5 -0
  42. data/lib/active_model/serializer.rb +205 -0
  43. data/lib/active_model_serializers.rb +29 -0
  44. data/lib/generators/serializer/USAGE +6 -0
  45. data/lib/generators/serializer/resource_override.rb +12 -0
  46. data/lib/generators/serializer/serializer_generator.rb +37 -0
  47. data/lib/generators/serializer/templates/serializer.rb +8 -0
  48. data/test/action_controller/adapter_selector_test.rb +53 -0
  49. data/test/action_controller/explicit_serializer_test.rb +134 -0
  50. data/test/action_controller/json_api/linked_test.rb +180 -0
  51. data/test/action_controller/json_api/pagination_test.rb +116 -0
  52. data/test/action_controller/serialization_scope_name_test.rb +67 -0
  53. data/test/action_controller/serialization_test.rb +426 -0
  54. data/test/adapter/fragment_cache_test.rb +37 -0
  55. data/test/adapter/json/belongs_to_test.rb +47 -0
  56. data/test/adapter/json/collection_test.rb +82 -0
  57. data/test/adapter/json/has_many_test.rb +47 -0
  58. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  59. data/test/adapter/json_api/collection_test.rb +96 -0
  60. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  61. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  62. data/test/adapter/json_api/has_many_test.rb +145 -0
  63. data/test/adapter/json_api/has_one_test.rb +81 -0
  64. data/test/adapter/json_api/json_api_test.rb +38 -0
  65. data/test/adapter/json_api/linked_test.rb +283 -0
  66. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  67. data/test/adapter/json_api/resource_type_config_test.rb +59 -0
  68. data/test/adapter/json_test.rb +47 -0
  69. data/test/adapter/null_test.rb +25 -0
  70. data/test/adapter_test.rb +52 -0
  71. data/test/array_serializer_test.rb +97 -0
  72. data/test/capture_warnings.rb +57 -0
  73. data/test/fixtures/active_record.rb +57 -0
  74. data/test/fixtures/poro.rb +266 -0
  75. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  76. data/test/generators/serializer_generator_test.rb +56 -0
  77. data/test/lint_test.rb +44 -0
  78. data/test/poro_test.rb +9 -0
  79. data/test/serializable_resource_test.rb +27 -0
  80. data/test/serializers/adapter_for_test.rb +50 -0
  81. data/test/serializers/association_macros_test.rb +36 -0
  82. data/test/serializers/associations_test.rb +150 -0
  83. data/test/serializers/attribute_test.rb +62 -0
  84. data/test/serializers/attributes_test.rb +63 -0
  85. data/test/serializers/cache_test.rb +164 -0
  86. data/test/serializers/configuration_test.rb +15 -0
  87. data/test/serializers/fieldset_test.rb +26 -0
  88. data/test/serializers/meta_test.rb +121 -0
  89. data/test/serializers/options_test.rb +21 -0
  90. data/test/serializers/root_test.rb +23 -0
  91. data/test/serializers/serializer_for_test.rb +65 -0
  92. data/test/serializers/urls_test.rb +26 -0
  93. data/test/support/rails_app.rb +21 -0
  94. data/test/support/stream_capture.rb +49 -0
  95. data/test/support/test_case.rb +5 -0
  96. data/test/test_helper.rb +41 -0
  97. metadata +287 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91327127509c7d851bb339e9ba020fbbc76264ba
4
+ data.tar.gz: 49a6e2089f5e063f3f67860ad948379d5225bc12
5
+ SHA512:
6
+ metadata.gz: 11ed694fdb6ada6f3c09693d9fe3d3e9c7de739f8ebfcb3f8e2abaf09bd5f0f81a44808b45c29e905acd35ed36efb9244b53f914562b41fc5f762fb540042b97
7
+ data.tar.gz: ce444233a29dfb860c2194b34065ef9c553340ce03bb046afb5b5305d53e0aa59191d872642574e042b9f117105ad1109809dbf0ccf002b7694c7db1c844aeca
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ Vagrantfile
14
+ .vagrant
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.swp
21
+ .ruby-version
22
+ .ruby-gemset
data/.travis.yml ADDED
@@ -0,0 +1,26 @@
1
+ language: ruby
2
+
3
+ sudo: false
4
+
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - 2.1
9
+ - 2.2
10
+ - jruby-19mode
11
+ - rbx-2
12
+ - ruby-head
13
+
14
+ install:
15
+ - bundle install --retry=3
16
+
17
+ env:
18
+ - "RAILS_VERSION=4.0"
19
+ - "RAILS_VERSION=4.1"
20
+ - "RAILS_VERSION=4.2"
21
+ - "RAILS_VERSION=master"
22
+
23
+ matrix:
24
+ allow_failures:
25
+ - rvm: ruby-head
26
+ - env: "RAILS_VERSION=master"
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ ### 0.10.0
2
+
3
+ * adds adapters pattern
4
+ * adds support for `meta` and `meta_key` [@kurko]
5
+ * adds method to override association [@kurko]
6
+ * adds `has_one` attribute for backwards compatibility [@ggordon]
7
+ * adds JSON API support 1.0 [@benedikt]
8
+ * adds fragment cache support [@joaomdmoura]
9
+ * adds cache support to attributes and associations [@joaomdmoura]
10
+ * uses model name to determine the type [@lsylvester]
11
+ * remove root key option and split JSON adapter [@joaomdmoura]
12
+ * adds FlattenJSON as default adapter [@joaomdmoura]
13
+ * adds support for `pagination links` at top level of JsonApi adapter [@bacarini]
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,31 @@
1
+ ## How can I help?
2
+
3
+ Everyone is encouraged to open issues that are affecting you: bugs, ideas, performance problems – everything helps!
4
+
5
+ The first place to start is by looking at our [GitHub Issues](https://github.com/rails-api/active_model_serializers/issues).
6
+
7
+ The vast majority of development is happening under the `master` branch, currently slated for release as `0.10.x`. This is where we would suggest you start.
8
+
9
+ Fixing bugs is extraordinarily helpful and requires the least familiarity with AMS. Look for issues labeled [**Needs Bug Verification**](https://github.com/rails-api/active_model_serializers/labels/Needs%20Bug%20Verification) and [**Bug**](https://github.com/rails-api/active_model_serializers/labels/bug).
10
+
11
+ We are also actively working to identify tasks under the label [**Good for New Contributors**](https://github.com/rails-api/active_model_serializers/labels/Good%20for%20New%20Contributors). Some bugs are expressly not good for new contributors, so don't expect 100% overlap between the two.
12
+
13
+ If you want to work on new feature development, look for the label [**Feature**](https://github.com/rails-api/active_model_serializers/labels/Feature).
14
+
15
+ We are also encouraging comments to substantial changes (larger than bugfixes and simple features) under an "RFC" (Request for Comments) process before we start active development. Look for the [**RFC**](https://github.com/rails-api/active_model_serializers/labels/RFC) label.
16
+
17
+ ## Issue Labeling
18
+
19
+ AMS uses a subset of [StandardIssueLabels](https://github.com/wagenet/StandardIssueLabels) for Github Issues. You can [see our labels here](https://github.com/rails-api/active_model_serializers/labels).
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it ( https://github.com/rails-api/active_model_serializers/fork )
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Write tests for your feature, or regression tests highlighting a bug
26
+ 4. Write the feature itself, or fix your bug
27
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 6. Push to the branch (`git push origin my-new-feature`)
29
+ 7. Create a new Pull Request
30
+
31
+ Remember to squash your commits and rebase off `master`.
data/Gemfile ADDED
@@ -0,0 +1,35 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_model_serializers.gemspec
4
+ gemspec
5
+
6
+ version = ENV['RAILS_VERSION'] || '4.2'
7
+
8
+ if version == 'master'
9
+ gem 'rack', github: 'rack/rack'
10
+ git 'https://github.com/rails/rails.git' do
11
+ gem 'railties'
12
+ gem 'activesupport'
13
+ gem 'activemodel'
14
+ gem 'actionpack'
15
+ # Rails 5
16
+ gem 'actionview'
17
+ end
18
+ # Rails 5
19
+ gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
20
+ else
21
+ gem_version = "~> #{version}.0"
22
+ gem 'railties', gem_version
23
+ gem 'activesupport', gem_version
24
+ gem 'activemodel', gem_version
25
+ gem 'actionpack', gem_version
26
+ end
27
+
28
+ group :test do
29
+ gem 'activerecord'
30
+ gem 'sqlite3', platform: :ruby
31
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
32
+ end
33
+
34
+ # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
35
+ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Steve Klabnik
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,348 @@
1
+ # ActiveModel::Serializer
2
+
3
+ [![Build Status](https://travis-ci.org/rails-api/active_model_serializers.svg)](https://travis-ci.org/rails-api/active_model_serializers)
4
+
5
+ _Windows Build Status -_ ![Build Status](https://ci.appveyor.com/api/projects/status/1dly7uj4l69bchmu)
6
+
7
+ ActiveModel::Serializer brings convention over configuration to your JSON generation.
8
+
9
+ AMS does this through two components: **serializers** and **adapters**.
10
+ Serializers describe _which_ attributes and relationships should be serialized.
11
+ Adapters describe _how_ attributes and relationships should be serialized.
12
+
13
+ By default AMS will use the **Flatten Json Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
14
+ Check how to change the adapter in the sections bellow.
15
+
16
+ # RELEASE CANDIDATE, PLEASE READ
17
+
18
+ This is the master branch of AMS. It will become the `0.10.0` release when it's
19
+ ready. Currently this is a release candidate. This is **not** backward
20
+ compatible with `0.9.0` or `0.8.0`.
21
+
22
+ `0.10.x` will be based on the `0.8.0` code, but with a more flexible
23
+ architecture. We'd love your help. [Learn how you can help here.](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)
24
+
25
+ ## Example
26
+
27
+ Given two models, a `Post(title: string, body: text)` and a
28
+ `Comment(name: string, body: text, post_id: integer)`, you will have two
29
+ serializers:
30
+
31
+ ```ruby
32
+ class PostSerializer < ActiveModel::Serializer
33
+ cache key: 'posts', expires_in: 3.hours
34
+ attributes :title, :body
35
+
36
+ has_many :comments
37
+
38
+ url :post
39
+ end
40
+ ```
41
+
42
+ and
43
+
44
+ ```ruby
45
+ class CommentSerializer < ActiveModel::Serializer
46
+ attributes :name, :body
47
+
48
+ belongs_to :post
49
+
50
+ url [:post, :comment]
51
+ end
52
+ ```
53
+
54
+ Generally speaking, you as a user of AMS will write (or generate) these
55
+ serializer classes. If you want to use a different adapter, such as a JsonApi, you can
56
+ change this in an initializer:
57
+
58
+ ```ruby
59
+ ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
60
+ ```
61
+
62
+ or
63
+
64
+ ```ruby
65
+ ActiveModel::Serializer.config.adapter = :json_api
66
+ ```
67
+
68
+ You won't need to implement an adapter unless you wish to use a new format or
69
+ media type with AMS.
70
+
71
+ If you want to have a root key on your responses you should use the Json adapter, instead of the default FlattenJson:
72
+
73
+ ```ruby
74
+ ActiveModel::Serializer.config.adapter = :json
75
+ ```
76
+
77
+ If you would like the key in the outputted JSON to be different from its name in ActiveRecord, you can use the :key option to customize it:
78
+
79
+ ```ruby
80
+ class PostSerializer < ActiveModel::Serializer
81
+ attributes :id, :body
82
+
83
+ # look up :subject on the model, but use +title+ in the JSON
84
+ attribute :subject, :key => :title
85
+ has_many :comments
86
+ end
87
+ ```
88
+
89
+ In your controllers, when you use `render :json`, Rails will now first search
90
+ for a serializer for the object and use it if available.
91
+
92
+ ```ruby
93
+ class PostsController < ApplicationController
94
+ def show
95
+ @post = Post.find(params[:id])
96
+
97
+ render json: @post
98
+ end
99
+ end
100
+ ```
101
+
102
+ In this case, Rails will look for a serializer named `PostSerializer`, and if
103
+ it exists, use it to serialize the `Post`.
104
+
105
+ ### Specify a serializer
106
+
107
+ If you wish to use a serializer other than the default, you can explicitly pass it to the renderer.
108
+
109
+ #### 1. For a resource:
110
+
111
+ ```ruby
112
+ render json: @post, serializer: PostPreviewSerializer
113
+ ```
114
+
115
+ #### 2. For an array resource:
116
+
117
+ ```ruby
118
+ # Use the default `ArraySerializer`, which will use `each_serializer` to
119
+ # serialize each element
120
+ render json: @posts, each_serializer: PostPreviewSerializer
121
+
122
+ # Or, you can explicitly provide the collection serializer as well
123
+ render json: @posts, serializer: CollectionSerializer, each_serializer: PostPreviewSerializer
124
+ ```
125
+
126
+ ### Meta
127
+
128
+ If you want a `meta` attribute in your response, specify it in the `render`
129
+ call:
130
+
131
+ ```ruby
132
+ render json: @post, meta: { total: 10 }
133
+ ```
134
+
135
+ The key can be customized using `meta_key` option.
136
+
137
+ ```ruby
138
+ render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
139
+ ```
140
+
141
+ `meta` will only be included in your response if you are using an Adapter that supports `root`, as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`.
142
+
143
+ ### Overriding association methods
144
+
145
+ If you want to override any association, you can use:
146
+
147
+ ```ruby
148
+ class PostSerializer < ActiveModel::Serializer
149
+ attributes :id, :body
150
+
151
+ has_many :comments
152
+
153
+ def comments
154
+ object.comments.active
155
+ end
156
+ end
157
+ ```
158
+
159
+ ### Overriding attribute methods
160
+
161
+ If you want to override any attribute, you can use:
162
+
163
+ ```ruby
164
+ class PostSerializer < ActiveModel::Serializer
165
+ attributes :id, :body
166
+
167
+ has_many :comments
168
+
169
+ def body
170
+ object.body.downcase
171
+ end
172
+ end
173
+ ```
174
+
175
+ ### Built in Adapters
176
+
177
+ #### FlattenJSON
178
+
179
+ It's the default adapter, it generates a json response without a root key.
180
+ Doesn't follow any specifc convention.
181
+
182
+ #### JSON
183
+
184
+ It also generates a json response but always with a root key. The root key **can't be overridden**, and will be automatically defined accordingly with the objects being serialized.
185
+ Doesn't follow any specifc convention.
186
+
187
+ #### JSONAPI
188
+
189
+ This adapter follows 1.0 of the format specified in
190
+ [jsonapi.org/format](http://jsonapi.org/format). It will include the associated
191
+ resources in the `"included"` member when the resource names are included in the
192
+ `include` option.
193
+
194
+ ```ruby
195
+ render @posts, include: ['authors', 'comments']
196
+ # or
197
+ render @posts, include: 'authors,comments'
198
+ ```
199
+
200
+ ## Installation
201
+
202
+ Add this line to your application's Gemfile:
203
+
204
+ ```
205
+ gem 'active_model_serializers'
206
+ ```
207
+
208
+ And then execute:
209
+
210
+ ```
211
+ $ bundle
212
+ ```
213
+
214
+ ## Creating a Serializer
215
+
216
+ The easiest way to create a new serializer is to generate a new resource, which
217
+ will generate a serializer at the same time:
218
+
219
+ ```
220
+ $ rails g resource post title:string body:string
221
+ ```
222
+
223
+ This will generate a serializer in `app/serializers/post_serializer.rb` for
224
+ your new model. You can also generate a serializer for an existing model with
225
+ the serializer generator:
226
+
227
+ ```
228
+ $ rails g serializer post
229
+ ```
230
+
231
+ The generated seralizer will contain basic `attributes` and
232
+ `has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
233
+
234
+ ```ruby
235
+ class PostSerializer < ActiveModel::Serializer
236
+ attributes :title, :body
237
+
238
+ has_many :comments
239
+ has_one :author
240
+
241
+ url :post
242
+ end
243
+ ```
244
+
245
+ and
246
+
247
+ ```ruby
248
+ class CommentSerializer < ActiveModel::Serializer
249
+ attributes :name, :body
250
+
251
+ belongs_to :post_id
252
+
253
+ url [:post, :comment]
254
+ end
255
+ ```
256
+
257
+ The attribute names are a **whitelist** of attributes to be serialized.
258
+
259
+ The `has_many`, `has_one`, and `belongs_to` declarations describe relationships between
260
+ resources. By default, when you serialize a `Post`, you will get its `Comments`
261
+ as well.
262
+
263
+ You may also use the `:serializer` option to specify a custom serializer class, for example:
264
+
265
+ ```ruby
266
+ has_many :comments, serializer: CommentPreviewSerializer
267
+ ```
268
+
269
+ And you can change the JSON key that the serializer should use for a particular association:
270
+
271
+ ```ruby
272
+ has_many :comments, key: :reviews
273
+ ```
274
+
275
+ The `url` declaration describes which named routes to use while generating URLs
276
+ for your JSON. Not every adapter will require URLs.
277
+ ## Pagination
278
+
279
+ Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter.
280
+
281
+ Although the others adapters does not have this feature, it is possible to implement pagination links to `JSON` adapter. For more information about it, please see in our docs [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md)
282
+
283
+ ## Caching
284
+
285
+ To cache a serializer, call ```cache``` and pass its options.
286
+ The options are the same options of ```ActiveSupport::Cache::Store```, plus
287
+ a ```key``` option that will be the prefix of the object cache
288
+ on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```.
289
+
290
+ The cache support is optimized to use the cached object in multiple request. An object cached on a ```show``` request will be reused at the ```index```. If there is a relationship with another cached serializer it will also be created and reused automatically.
291
+
292
+ **[NOTE] Every object is individually cached.**
293
+
294
+ **[NOTE] The cache is automatically expired after update an object but it's not deleted.**
295
+
296
+ ```ruby
297
+ cache(options = nil) # options: ```{key, expires_in, compress, force, race_condition_ttl}```
298
+ ```
299
+
300
+ Take the example bellow:
301
+
302
+ ```ruby
303
+ class PostSerializer < ActiveModel::Serializer
304
+ cache key: 'post', expires_in: 3.hours
305
+ attributes :title, :body
306
+
307
+ has_many :comments
308
+
309
+ url :post
310
+ end
311
+ ```
312
+
313
+ On this example every ```Post``` object will be cached with
314
+ the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want,
315
+ but in this case it will be automatically expired after 3 hours.
316
+
317
+ ### Fragmenting Caching
318
+
319
+ If there is some API endpoint that shouldn't be fully cached, you can still optimise it, using Fragment Cache on the attributes and relationships that you want to cache.
320
+
321
+ You can define the attribute by using ```only``` or ```except``` option on cache method.
322
+
323
+ **[NOTE] Cache serializers will be used at their relationships**
324
+
325
+ Example:
326
+
327
+ ```ruby
328
+ class PostSerializer < ActiveModel::Serializer
329
+ cache key: 'post', expires_in: 3.hours, only: [:title]
330
+ attributes :title, :body
331
+
332
+ has_many :comments
333
+
334
+ url :post
335
+ end
336
+ ```
337
+
338
+ ## Getting Help
339
+
340
+ If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new).
341
+
342
+ If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
343
+
344
+ Thanks!
345
+
346
+ # Contributing
347
+
348
+ See [CONTRIBUTING.md](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ t.ruby_opts = ['-r./test/test_helper.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
data/appveyor.yml ADDED
@@ -0,0 +1,25 @@
1
+ version: '{build}'
2
+
3
+ skip_tags: true
4
+
5
+ environment:
6
+ matrix:
7
+ - ruby_version: "193"
8
+ - ruby_version: "193-x64"
9
+ - ruby_version: "200"
10
+ - ruby_version: "200-x64"
11
+ - ruby_version: "21"
12
+ - ruby_version: "21-x64"
13
+
14
+ install:
15
+ - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
16
+ - ruby --version
17
+ - gem --version
18
+ - gem install bundler
19
+ - bundler --version
20
+ - bundle install --retry=3
21
+
22
+ test_script:
23
+ - bundle exec rake
24
+
25
+ build: off
data/cheap_ams.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model/serializer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cheap_ams'
8
+ spec.version = ActiveModel::Serializer::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ['Steve Klabnik']
11
+ spec.email = ['steve@steveklabnik.com']
12
+ spec.summary = %q{Conventions-based JSON generation for Rails.}
13
+ spec.description = %q{ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.}
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+
22
+ rails_versions = '>= 4.0'
23
+ spec.add_runtime_dependency 'activemodel', rails_versions
24
+ # 'activesupport', rails_versions
25
+ # 'builder'
26
+
27
+ spec.add_runtime_dependency 'actionpack', rails_versions
28
+ # 'activesupport', rails_versions
29
+ # 'rack'
30
+ # 'rack-test', '~> 0.6.2'
31
+
32
+ spec.add_runtime_dependency 'railties', rails_versions
33
+ # 'activesupport', rails_versions
34
+ # 'actionpack', rails_versions
35
+ # 'rake', '>= 0.8.7'
36
+
37
+ # 'activesupport', rails_versions
38
+ # 'i18n,
39
+ # 'tzinfo'
40
+ # 'minitest'
41
+ # 'thread_safe'
42
+
43
+ # Soft dependency for pagination
44
+ spec.add_development_dependency 'kaminari'
45
+ spec.add_development_dependency 'will_paginate'
46
+
47
+ spec.add_development_dependency 'bundler', '~> 1.6'
48
+ spec.add_development_dependency 'timecop', '>= 0.7'
49
+ end
data/docs/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Docs - ActiveModel::Serializer 0.10.x
2
+
3
+ This is the documentation of AMS, it's focused on the **0.10.x version.**
4
+
5
+ -----
6
+
7
+ ## General
8
+
9
+ - [Getting Started](general/getting_started.md)
10
+ - [Adapters](general/adapters.md)
11
+
12
+ ## How to
13
+
14
+ - [How to add root key](howto/add_root_key.md)
15
+ - [How to add pagination links](howto/add_pagination_links.md)
16
+
17
+ ## Getting Help
18
+
19
+ If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new).
20
+
21
+ If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
22
+
23
+ Thanks!
24
+
25
+ ## Contributing
26
+
27
+ See [CONTRIBUTING.md](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)
@@ -0,0 +1,51 @@
1
+ # Adapters
2
+
3
+ AMS does this through two components: **serializers** and **adapters**.
4
+ Serializers describe _which_ attributes and relationships should be serialized.
5
+ Adapters describe _how_ attributes and relationships should be serialized.
6
+ You can use one of the built-in adapters (```FlattenJSON``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.
7
+
8
+ ## Built in Adapters
9
+
10
+ ### FlattenJSON - Default
11
+
12
+ It's the default adapter, it generates a json response without a root key.
13
+ Doesn't follow any specifc convention.
14
+
15
+ ### JSON
16
+
17
+ It also generates a json response but always with a root key. The root key **can't be overridden**, and will be automatically defined accordingly to the objects being serialized.
18
+ Doesn't follow any specifc convention.
19
+
20
+ ### JSONAPI
21
+
22
+ This adapter follows **version 1.0** of the format specified in
23
+ [jsonapi.org/format](http://jsonapi.org/format). It will include the associated
24
+ resources in the `"included"` member when the resource names are included in the
25
+ `include` option.
26
+
27
+ ```ruby
28
+ render @posts, include: ['authors', 'comments']
29
+ # or
30
+ render @posts, include: 'authors,comments'
31
+ ```
32
+
33
+ ## Choosing an adapter
34
+
35
+ If you want to use a different adapter, such as JsonApi, you can change this in an initializer:
36
+
37
+ ```ruby
38
+ ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
39
+ ```
40
+
41
+ or
42
+
43
+ ```ruby
44
+ ActiveModel::Serializer.config.adapter = :json_api
45
+ ```
46
+
47
+ If you want to have a root key in your responses you should use the Json adapter, instead of the default FlattenJson:
48
+
49
+ ```ruby
50
+ ActiveModel::Serializer.config.adapter = :json
51
+ ```