Compare commits

...

181 Commits

Author SHA1 Message Date
Wasif Hossain
433c0c646f
Bump version to 0.10.10 2019-07-13 15:30:49 +06:00
Wasif Hossain
2224cc8b50
Merge pull request #2172 from AyogoHealth/superclass_namespace
Include namespace when looking up superclass
2019-07-11 22:59:32 +06:00
Rafael Gaspar
beffbb2bd3
Follows inheritance with a namespace
Co-authored-by: Rafael Gaspar <rafael.gaspar@me.com>
Co-authored-by: Darryl Pogue <darryl@dpogue.ca>
Co-authored-by: Artin Boghosian <artinboghosian@gmail.com>
2019-07-10 11:41:32 -07:00
Wasif Hossain
777fab04ae
Merge pull request #2337 from InteNs/fix-json-api-belongs-to-fk-on-object
fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank
2019-06-15 14:50:12 +06:00
Mark Havekes
22849c0399 fix include in failing test 2019-06-14 09:52:16 +02:00
Mark Havekes
4022337966 Revert "lock jsonapi renderer to < 0.2.1"
This reverts commit 58f4f98e41.
2019-06-13 15:49:55 +02:00
Mark Havekes
cee0ad4494 update changelog 2019-06-13 10:52:44 +02:00
Mark Havekes
58f4f98e41 lock jsonapi renderer to < 0.2.1 2019-06-11 23:23:41 +02:00
Mark Havekes
28a172e66a return nil if id is nil 2019-06-11 14:42:13 +02:00
Mark Havekes
4b0c230d76 add failing test and suggestion 2019-06-04 10:27:02 +02:00
Wasif Hossain
339f99fccc
Merge pull request #2334 from sikachu/0-10-stable-relax-rails-version
Allow AMS to run on Rails < 6.1
2019-06-02 09:32:11 +06:00
Prem Sichanugrist
a829bc05b0 Allow AMS to run on Rails 6.0.x 2019-05-31 14:45:14 +09:00
Wasif Hossain
1ff21bac18
Merge pull request #2321 from iggant/0-10-stable
Stick to bundler version < 2
2019-05-02 06:17:21 +06:00
Anton K
4a4a0f263e Stick to bundle < 2 2019-05-01 22:58:09 +03:00
Wasif Hossain
1ebdda331a
Merge pull request #2328 from rails-api/rails-6-rc
Add support for Rails 6.0.0.rc1 on Travis
2019-04-29 20:30:33 +06:00
Wasif Hossain
0f75837f9e
Keep rubygems version < 3.0 until we keep supporting ruby < 2.3 2019-04-29 11:58:08 +06:00
Wasif Hossain
ca43661ac0
Add support for Rails 6.0.0.rc1 on travis 2019-04-29 11:46:20 +06:00
Wasif Hossain
19a6188323
Merge pull request #2304 from greysteil/slim-down-gem
Slim down bundled gem by excluding test files and docs
2019-04-23 21:03:52 +06:00
Grey Baker
14113ba85b Slim down bundled gem by excluding test files and docs 2019-04-23 15:06:40 +01:00
Wasif Hossain
3419a01349
Merge pull request #2223 from rails-api/fields_support_in_all_adapters
Fieldset support in Attributes/JSON adapters
2019-04-23 15:55:43 +06:00
Wasif Hossain
c77fddadd4
Mention current rails version for #2327 ruby2.6/rails4.2 monkeypatch 2019-04-23 15:18:18 +06:00
Wasif Hossain
96303af45f
Add changelog entries for this PR and #2327 2019-04-23 15:14:35 +06:00
Wasif Hossain
807e160b9c
Add missing frozen string literal comment 2019-04-23 04:34:09 +06:00
Wasif Hossain
a9cb097109
Refactoring 2019-04-23 04:34:02 +06:00
Benjamin Fleischer
06fcd34807
Follows https://github.com/rails-api/active_model_serializers/pull/2100 2019-04-23 04:33:56 +06:00
Wasif Hossain
7896ad656a
Merge pull request #2327 from rails-api/ruby-2-6-rails-4-2-test-patch
Add support for Ruby 2.6, configure sqlite3 to use ~> 1.4 for rails 6+, patch for Rails 4.2 against Ruby 2.6
2019-04-23 02:50:47 +06:00
Wasif Hossain
5d122f2922
Fix rubocop offenses 2019-04-23 02:06:12 +06:00
Wasif Hossain
3715a101c2
Refactor travis.yml 2019-04-23 01:51:28 +06:00
Wasif Hossain
0aa1ed93a4
Configure sqlite3 to use ~> 1.4 for rails 6+ 2019-04-22 23:22:28 +06:00
Wasif Hossain
942e10fdb8
Merge pull request #2323 from feliperenan/fix-serializer-link
Fix serializer class link on guides
2019-03-21 22:35:39 +06:00
Felipe Renan
12ccb78fdb Fix serializer class link on guides
The link was pointing out to the master branch instead of the
v-10-stable branch.
2019-03-21 11:41:25 -03:00
Wasif Hossain
6fb4f7b111
Merge pull request #2319 from kylekeesling/rails6
Fix deprecation warnings on Rails 6
2019-03-14 21:51:04 +06:00
Kyle Keesling
912af7ef91
update test fixture schema to use timestamps instead of timestamp
- discovered via @wasifhossain
- Update CHANGELOG.md
2019-03-14 11:00:45 -04:00
Kyle Keesling
6fbeb9a93e
Fix deprecation warnings on Rails 6
takes into account suggestions from @bf4
2019-03-13 09:52:54 -04:00
Benjamin Fleischer
bb0f9d088d Bump version to 0.10.9 2019-02-08 11:25:53 -06:00
cintamani
15b7974fac
Merge pull request #2288 from cintamani/patch-1
Change the fetch method to deal with recyclable key cache strategy
2019-02-08 12:14:03 +00:00
cintamani
be7f083d02 Fix sqlite3 version to 1.3.13 as the new 1.4 version released last week is not compatible with the adapters currently in use 2019-02-08 11:17:36 +00:00
cintamani
2cc2a048c6
Update CHANGELOG.md
Run tests again
2019-02-08 11:09:05 +00:00
cintamani
26bba194d0 Ensure that CacheVersioning is setted up only in the context of relevant tests 2019-02-05 15:50:43 +00:00
Benjamin Fleischer
6fd5c66837
Update test/cache_test.rb
Co-Authored-By: cintamani <cintamani.puddu@gmail.com>
2019-02-05 15:42:21 +00:00
cintamani
7ff2ac64b7
Update test/cache_test.rb 2019-02-01 17:44:09 +00:00
cintamani
f815d3e9f8
Merge pull request #1 from wasifhossain/collection-cache-expiry
Test cache expiration in collection on update of a record
2019-02-01 16:05:02 +00:00
Wasif Hossain
3d0b5acc37
Test cache expiration in collection on update of a record 2019-02-01 10:04:49 +06:00
Krzysztof Rybka
209834dc49 Performance and memory usage fixes (#2309)
* Use select and reject instead of partition.map

* Sort adapters keys in place

* Just select from options

* Add changelog entry
2019-01-29 15:38:11 -06:00
cintamani
141feeb259 Revert "Update caching.rb"
This reverts commit f31430a8185533246638a7cfebba325f0ac9b0fa.
2019-01-29 16:00:18 +00:00
cintamani
427dd05a73 Revert "Update caching.rb"
This reverts commit c6f34eb9e9df86b568b9463e2ea8c26f0f43a838.
2019-01-29 16:00:18 +00:00
cintamani
192c86ab2a Update test/cache_test.rb 2019-01-29 16:00:18 +00:00
cintamani
7dbb583873 Update cache_test.rb
Re-run tests on appveyor. Based on https://github.com/rails-api/active_model_serializers/pull/1168/files looks like there are some config issue in that environment causing this.
2019-01-29 16:00:18 +00:00
cintamani
bd53d9b213 Update caching.rb 2019-01-29 16:00:18 +00:00
cintamani
b0039e3758 Update caching.rb 2019-01-29 16:00:18 +00:00
cintamani
70604bbae7 Only set cache_versioning to true on rails versions when relevant 2019-01-29 16:00:18 +00:00
cintamani
7f751fc1f7 Add test coverage and changelog 2019-01-29 16:00:18 +00:00
cintamani
be5fbf3d54 Use #cache_key_with_key when available
In order to keep compatibility between the AMS cache feature and with Rails > 5.1  cache versioning, we have to use the `cache_key_with_version`. 
**NOTE** - This is a quick fix to the issue, if there will be future plans a proper cache versioning with recyclable key needs to be implemented. 

More info: https://github.com/rails-api/active_model_serializers/issues/2287
2019-01-29 16:00:18 +00:00
Benjamin Fleischer
22f2cb76dd
Merge pull request #2310 from krzysiek1507/fix/bundler-on-travis
Install bundler ~> 1.17 on Travis
2019-01-29 09:48:19 -06:00
Krzysztof Rybka
a7915c80c2 Exclude Ruby 2.4.2 and Rails master from matrix 2019-01-25 19:32:25 +01:00
Krzysztof Rybka
96ab640eb9 Install rubygems 2.7.8 and bundler ~> 1.17 on Travis 2019-01-25 01:39:24 +01:00
Benjamin Fleischer
c6c3927e00
Merge pull request #2307 from faucct/bugfix/double-evaluation-of-object
Reevaluation of object
2019-01-10 11:58:27 -06:00
Benjamin Fleischer
9939d54289
Merge pull request #2306 from krzysiek1507/feature/add-frozen-string-literal
Add frozen_string_literal pragma to ruby files
2019-01-09 12:19:13 -06:00
Nikita Sokolov
a39794df5b LazyAssociation#object should not reevaluate reflection values 2019-01-09 09:52:38 +03:00
Krzysztof Rybka
89dcdace86 Enable frozen string literal comment for rubocop 2018-12-17 16:55:00 +01:00
Krzysztof Rybka
873cf4add2 Add frozen_string_literal pragma to ruby files 2018-12-17 16:54:56 +01:00
Benjamin Fleischer
17d0e6bdb7
Merge pull request #2300 from cookpad/c/travis-fixes-master
Updates to travis integration
2018-11-12 22:25:37 -06:00
cintamani
b76aae7f8e
Update .travis.yml 2018-11-07 11:27:40 +00:00
cintamani
f608aef83c
Add ruby 2.5.3
Add the latest stable ruby version in the list of RVM to test
2018-11-07 10:47:20 +00:00
Benjamin Fleischer
bc4805cbc1 Bump version to v0.10.8 2018-11-01 16:24:08 -05:00
Benjamin Fleischer
f1a2ec7f99 Add change to changelog 2018-11-01 16:19:43 -05:00
Benjamin Fleischer
09264da273 Merge branch 'fix_thread_safety_bug' into 0-10-stable 2018-11-01 16:17:02 -05:00
Benjamin Fleischer
238d7921ec Lint per rubocop 2018-11-01 14:51:18 -05:00
Benjamin Fleischer
b358271ef5 Note that we dup the entire reflection instance 2018-11-01 14:21:59 -05:00
LongCB
c7e847fc72 Fix thread unsafe behavior 2018-11-01 14:20:47 -05:00
Benjamin Fleischer
127b04ba33 Add failing test for reflection thread safety bug 2018-11-01 13:57:07 -05:00
Benjamin Fleischer
cb67434b46
Merge pull request #2279 from mkon/link-conditions
Support conditions in link statements
2018-10-31 13:27:28 -05:00
cintamani
b7bf03c127 Exclude ruby > 2.1 for Rails 5.2 build 2018-10-29 10:13:52 +00:00
cintamani
1b679d4c8c Test travis integration 2018-10-29 09:46:07 +00:00
Konstantin Munteanu
572f11b7e0 value is always a link 2018-10-25 09:54:36 +02:00
Konstantin Munteanu
ca6c009273 typos 2018-10-25 09:45:34 +02:00
Konstantin Munteanu
0d3999c36d Support conditions in link statements 2018-10-25 09:45:34 +02:00
Benjamin Fleischer
05e5ba6939
Merge pull request #2281 from alvincrespo/alvincrespo-patch-1
Fix documentation around class name comparison
2018-10-24 17:05:19 -05:00
Benjamin Fleischer
4b2b9ef730
Merge pull request #2297 from vnbrs/patch-1
Remove unnecessary line break from exception message
2018-10-24 17:02:19 -05:00
Benjamin Fleischer
01ecc5dea3 Update Changelog 2018-10-24 17:01:25 -05:00
Benjamin Fleischer
12724807e9 Merge branch 'Hirurg103-0-10-stable' into 0-10-stable 2018-10-24 16:59:59 -05:00
Vinicius Brasil
bcf21ea0a6
Remove unnecessary line break from exception message
This removes the unnecessary line break from the exception message. Example:

```
Cannot infer root key from collection type. Please\n
specify the root or each_serializer option, or render a JSON String
```
2018-10-24 14:10:59 -03:00
Dzmitry
db4e5267fd Fix the bug that serializer could not be found if the association's serializer is namespaced but the model is not
[fix #2295]
2018-10-20 15:42:11 +03:00
Benjamin Fleischer
eb865c276d Update Changelog 2018-10-11 15:03:44 -05:00
Benjamin Fleischer
506636abfb Merge branch 'f-mer-0-10-stable-eager_load' into 0-10-stable 2018-10-11 15:01:41 -05:00
Fabian Mersch
65313b901b Remove obsolete autoloads 2018-10-11 21:32:12 +02:00
Fabian Mersch
2a8b9f4105 Eager load modules on boot
Using ActiveModelSerializers with a threaded web server eg. Puma
uninitialized constant errors are thrown. Leaving this article for
reference:
http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/.
2018-10-11 21:32:12 +02:00
Benjamin Fleischer
fce8be0dc0
Merge pull request #2290 from rails-api/minitest_ci
Minitest 5.11 breaks; needs something like ::Minitest::Result.from
2018-10-10 10:59:37 -05:00
Benjamin Fleischer
f5f5d4db9e Fix Rails master 2018-10-10 09:39:48 -05:00
Benjamin Fleischer
ed97e94e71 Exclude deprecated rubies 2018-10-09 15:14:40 -05:00
Benjamin Fleischer
4f9d64096b Minitest 5.11 breaks; needs something like ::Minitest::Result.from 2018-10-09 14:39:41 -05:00
Alvin Crespo
3a200d7fe1
Minor doc update
Use model.class.name to do the correct comparison
2018-09-07 14:28:21 -04:00
Benjamin Fleischer
f3a19bd515
Merge pull request #2260 from abhaynikam/2258-fix-class-name-documentation
[docs] Fix class name documentation
2018-06-22 16:51:55 -05:00
Benjamin Fleischer
9c77bb6a60
Merge pull request #2263 from vthomas2007/fix-readme-lint-links
Fix Lint links in README
2018-06-22 16:50:53 -05:00
Vince Thomas
76936e7bb9 Fix Lint links in README 2018-06-21 09:46:23 -04:00
Abhay Nikam
4559774d60 Updated the defination for the class_name in documentation 2018-06-21 08:49:33 +05:30
Abhay Nikam
993a48fdcf wordsmitting 2018-06-07 10:05:21 +05:30
Abhay Nikam
65cf4836c8 Updated class_name defination. 2018-06-07 10:04:03 +05:30
Abhay Nikam
f3b202746b Added examples to use class_name options for association. 2018-06-07 09:58:48 +05:30
Benjamin Fleischer
41f2e11c4d
Merge pull request #2222 from rails-api/ci_fix
CI looks like JRuby is still failing sometimes
2017-11-19 16:21:26 -06:00
Benjamin Fleischer
5fe473fe0d JRuby AR adapter are now 5x.0 for Rails 5.x+ 2017-11-19 15:34:13 -06:00
Benjamin Fleischer
d274295ee5 Merge branch 'quake-collection_cache' into 0-10-stable 2017-11-19 11:50:41 -06:00
quake wang
a9ea81a831 resolve collection cache error 2017-11-19 11:33:48 -06:00
Benjamin Fleischer
ca531789c3 Bump to 0.10.7; update changelog 2017-11-14 20:50:51 -06:00
Benjamin Fleischer
c41e243ed8
Merge pull request #2218 from rails-api/opt_in_belongs_to_uses_foreign_key
Fix 0.10.6 regression; make using belongs_to on self opt-in
2017-11-13 09:40:37 -06:00
Benjamin Fleischer
b41451c9bf Make tests pass 2017-11-13 08:36:23 -06:00
Benjamin Fleischer
3dd6cccb4d Fix 0.10.6 regression; make using belongs_to on self opt-in 2017-11-13 08:36:23 -06:00
Benjamin Fleischer
00a47d3da4
Prove CI broken (#2220)
* Remove appveyor rubygems/bundler stuff
* Try specifying different jdbc adapter
* Attempt to fix failing JRuby test I cannot reproduce

```
bundle exec rake ci
Picked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
/home/travis/.rvm/rubies/jruby-9.1.9.0/bin/jruby -w -I"lib:lib:test" -r./test/test_helper.rb  -w -I"/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/rake-11.3.0/lib" "/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_test_loader.rb" "test/**/*_test.rb"

ActiveModelSerializers::Adapter::JsonApi::PaginationLinksTest#test_raises_descriptive_error_when_serialization_context_unset [/home/travis/build/rails-api/active_model_serializers/test/adapter/json_api/pagination_links_test.rb:186]:
[] exception expected, not
Class: <ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError>
Message: <" JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.\n Please pass a ':serialization_context' option or\n override CollectionSerializer#paginated? to return 'false'.\n">
---Backtrace---
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/adapter/json_api/pagination_links.rb:14:in `block in initialize'
org/jruby/RubyHash.java:1187:in `fetch'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/adapter/json_api/pagination_links.rb:13:in `initialize'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/adapter/json_api.rb:520:in `pagination_links_for'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/adapter/json_api.rb:147:in `success_document'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/adapter/json_api.rb:59:in `serializable_hash'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/adapter/base.rb:59:in `as_json'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/serializable_resource.rb:8:in `as_json'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:69:in `block in as_json'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:117:in `call'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:555:in `block in compile'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:505:in `call'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:498:in `block in around'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:343:in `block in simple'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:22:in `block in instrument_rendering'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:79:in `block in notify_render'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/notifications.rb:164:in `block in instrument'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/notifications.rb:164:in `instrument'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:78:in `notify_render'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:21:in `block in instrument_rendering'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:97:in `tag_logger'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:20:in `block in instrument_rendering'
org/jruby/RubyBasicObject.java:1724:in `instance_exec'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:441:in `block in make_lambda'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:342:in `block in simple'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:497:in `block in around'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:505:in `call'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:778:in `_run_render_callbacks'
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/travis/build/rails-api/active_model_serializers/lib/active_model_serializers/logging.rb:68:in `block in as_json'
/home/travis/build/rails-api/active_model_serializers/test/adapter/json_api/pagination_links_test.rb:187:in `block in test_raises_descriptive_error_when_serialization_context_unset'

Command failed with status (1): [ruby -w -I"lib:lib:test" -r./test/test_helper.rb  -w -I"/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/rake-11.3.0/lib" "/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/rake-11.3.0/lib/rake/rake_test_loader.rb" "test/**/*_test.rb" ]
/home/travis/build/rails-api/active_model_serializers/vendor/bundle/jruby/2.3.0/gems/rake-11.3.0/exe/rake:27:in `<main>'
Tasks: TOP => ci => default => test
(See full trace by running task with --trace)
```
2017-11-13 08:34:56 -06:00
Benjamin Fleischer
3c5e11bb0f
Merge pull request #2216 from rails-api/serialize_resource_with_nil_id
Fix: Serialize resource type for unpersisted records (blank id)
2017-11-01 10:36:22 -05:00
Benjamin Fleischer
33ec26fa12 Merge branch 'stratigos-patch-1' into 0-10-stable 2017-10-31 16:46:20 -05:00
Todd Morningstar
0bbeeb384e Update serializers.md
* Add note that any controller reference is acceptable for `serialization_scope`
 * Add note about `ActionController::API` and `view_context` set to `nil`, with respect to accessing helpers in a Serializer
 * refs #2144
 * Update CHANGELOG.md
2017-10-31 17:15:43 -04:00
Benjamin Fleischer
196216b250
Merge pull request #2212 from rails-api/remove_legacy_embeds
Remove legacy has_many_embed_ids test [ci skip]
2017-10-31 14:54:02 -05:00
Benjamin Fleischer
51f2643f40 Style 2017-10-31 14:48:27 -05:00
Benjamin Fleischer
9745a2f735 Fix: ResourceIdentifier.for_type_with_id can serialize unpersisted resources 2017-10-31 14:47:55 -05:00
Benjamin Fleischer
5916014b48 Fix: resource object identifier with nil id excludes id 2017-10-31 14:34:30 -05:00
Benjamin Fleischer
92dde58f5f Assert serializer-defined types are not inflected 2017-10-31 14:31:33 -05:00
Benjamin Fleischer
82e90091fd Put the similar jsonapi type tests together 2017-10-31 14:29:09 -05:00
Benjamin Fleischer
b439fe69c6 Refactor jsonapi type/id tests to be more explicit 2017-10-31 14:26:05 -05:00
Benjamin Fleischer
e3480345e3
Merge pull request #2211 from rails-api/polymorphic_relationships_require_serializer_instance
JSON:API relationship tests no longer show v0.10.5 regression
2017-10-31 13:49:18 -05:00
Benjamin Fleischer
54d40c79e8
Merge pull request #2214 from jmeredith16/undeterminable-json-root
Fail if collection type cannot be inferred with json adapter (#2210)
2017-10-30 16:11:08 -05:00
Joe Meredith
c6a14c9eac Fail if collection type cannot be inferred with json adapter (#2210) 2017-10-30 13:37:37 -04:00
Benjamin Fleischer
1fd324947f Remove legacy has_many_embed_ids test 2017-10-29 22:50:59 -05:00
Benjamin Fleischer
a0de45a4d8 Fix JSON:API: for_type_and_id should always inflect_type
Should Serializer._type ever be inflected?
Right now, it won't be, but association.serializer._type will be inflected.

That's the current behavior.
2017-10-29 21:23:48 -05:00
Benjamin Fleischer
cf29db34c6 Fix JSON:API polymorphic type regression from v0.10.5 2017-10-29 20:15:15 -05:00
Benjamin Fleischer
0fcb8a6cce Fix polymorphic belongs_to tests; passes on v0.10.5 2017-10-29 19:26:14 -05:00
Benjamin Fleischer
5e1e138d47 Refactor ResourceIdentifier 2017-10-29 17:13:34 -05:00
Vedant Agarwala
4076a480b7 Removed misleading line in README
As mentioned in the `master` branch's README, master will not be release ready for now.
Fixes https://github.com/rails-api/active_model_serializers/issues/2163
2017-10-29 16:26:32 -05:00
Nate Pinsky
88367da970 Add warnings about global adapter config to docs (#2176) 2017-10-24 17:30:56 -05:00
Dennis Stumm
4d7c2457d7 Fix deserialization of polymorphic relationships (#2200)
* Classify the relationship type for polymorphic associations
2017-10-07 17:03:38 -05:00
Benjamin Fleischer
715a702f55 Merge pull request #2189 from tagliala/update-jsonapi-renderer-dependency
Update version constraint for jsonapi-renderer
2017-09-19 15:32:44 -05:00
Benjamin Fleischer
4a995955ea Merge pull request #2198 from tagliala/improve-travis-configuration
Test against latest stable Ruby and Rails versions
2017-09-19 15:31:57 -05:00
Geremia Taglialatela
ab517d1a2d Test against latest stable Ruby and Rails versions
Also skips Rails 4.1 tests against Ruby 2.4.2 and ruby-head,
because Rails 4.2.8 is the first version of the 4.x series that
officially	support Ruby 2.4.
2017-09-19 17:47:52 +02:00
Geremia Taglialatela
1c9214d041
Update version constraint for jsonapi-renderer 2017-09-19 16:57:09 +02:00
Benjamin Fleischer
6a7d864605 Fix CI on 0-10-stable (#2181)
* Loosen pry, pry-byebug depencency

```
Resolving dependencies...
byebug-9.1.0 requires ruby version >= 2.2.0, which is
incompatible with the current version, ruby 2.1.10p492
```

* Adjust nokogiri version constraint for CI

Update appveyor Ruby to 2.3 to work around:

```
Gem::InstallError: nokogiri requires Ruby version < 2.5, >= 2.2.

An error occurred while installing nokogiri (1.8.0), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.8.0'` succeeds before bundling.
```

and not 2.4 since:

https://ci.appveyor.com/project/bf4/active-model-serializers/build/1.0.1052-fix_ci/job/0q3itabsnvnxr83u

```
nokogiri-1.6.8.1-x86-mingw32 requires ruby version < 2.4, which is incompatible with the current version, ruby 2.4.1p111
```

* Include rails gem in Gemfile

(For Rails5)

In Rails5, checking for Rails::Railtie is better

* Rails5 test env requires Rails.application.class.name

rails-42d09f6b49da/railties/lib/rails/application.rb

```ruby
def secret_key_base
  if Rails.env.test? || Rails.env.development?
  Digest::MD5.hexdigest self.class.name
```

* Reformat exclude matrix to be easier to read

* Simplify jruby-travis config per rails/rails

* Organize .travis.yml per rails/rails

* Allow JRuby failure on Rails 5+; try rails-5 db adapter branch

https://github.com/jruby/activerecord-jdbc-adapter/issues/708

```
uninitialized constant ActiveRecord::ConnectionAdapters::Column::Format
```

see https://travis-ci.org/rails-api/active_model_serializers/jobs/277112008
2017-09-19 05:27:02 -04:00
Benjamin Fleischer
c2fa01624d Merge pull request #2191 from tagliala/fix-rubocop-offence
Fix RuboCop offence
2017-09-17 12:59:12 -05:00
Geremia Taglialatela
4f78319219 Fix RuboCop offence 2017-09-17 19:33:05 +02:00
Benjamin Fleischer
b1de431731 Merge pull request #2179 from drn/enumerator-exception
Fix Enumerator.new ArgumentError.
2017-08-29 10:14:25 -05:00
Darren Cheng
967ff8dcc0 Patch Enumerator.new ArgumentError. 2017-08-28 19:38:08 -07:00
Alexandre de Oliveira
1e4d117b99 Merge pull request #2149 from mecampbellsoup/more-pagination-links-by-default
Always include self, first, last pagination links
2017-06-14 15:47:30 -03:00
mecampbellsoup
402883d84f Fix Travis lint offenses 2017-06-14 13:35:00 -04:00
mecampbellsoup
e4b3224c64 Fix indentation mismatch 2017-06-14 12:53:38 -04:00
mecampbellsoup
b7442e741c Use symbolized keys for json hash
They will be converted to strings when rendered as JSON.
2017-06-14 12:52:29 -04:00
Benjamin Fleischer
1539747a59 Merge pull request #2152 from tannerj/0-10-stable
Fix dead link in docs/general/rendering
2017-06-13 20:08:29 -05:00
Joel Tanner Jones
622872ae2b Fix dead link in docs/general/rendering
The add_pageination_links.md is dead in docs/general/rendering on the
0-10-stable branch.
2017-06-13 13:28:24 -04:00
mecampbellsoup
92e9a66e97 Amend tests to always include all pagination keys 2017-06-13 12:35:55 -04:00
mecampbellsoup
16e5204eab Always include pagination keys but set null values when not needed 2017-06-13 12:09:00 -04:00
mecampbellsoup
ff71ef26eb Amend pagination controller specs 2017-06-05 18:41:15 -04:00
mecampbellsoup
d8e983604b Clarify naming of expected response w/ pagination but empty data 2017-06-05 17:51:38 -04:00
mecampbellsoup
7387266c37 Always display self, first, last pagination links 2017-06-05 17:26:41 -04:00
Christian
a89e78c655 Allow referencing sideloaded include by key. (#2136)
* If a `key` is set on the reflection use the `key` instead of `name`.
This ensures that associations with a key set are still included.
2017-05-18 15:59:14 -05:00
Benjamin Fleischer
a5ab62fd18 Merge pull request #2130 from greysteil/allow-id-overwriting
Allow serialized ID to be overwritten for belongs-to relationships
2017-05-15 09:24:41 -05:00
Grey Baker
be7ee70376 Allow serialized ID to be overwritten for belongs-to relationships
If the `id` attribute for a class isn't taken directly from the object when
serializing it, it may be desirible for other classes that serialize a
relationship with that class to overwrite the relationship IDs they serialize.

For example, suppose we have:

```(ruby)
class Repo < Model
  attributes :id, :github_id, :name
  associations :configs
end

class Config < Model
  attributes :id
  belongs_to :repo
end

class RepoSerializer < ActiveModel::Serializer
  attributes :id, :name

  has_many :update_configs

  def id
    object.github_id
  end
end

class ConfigSerializer < ActiveModel::Serializer
  attributes :id
  belongs_to :repo
end
```

In the above example, serializing a list of `Repo`s will give the `github_id`
for each one, but serializing a `Config` will give the `id` for its parent repo.

Ideally AMS would inspect the `RepoSerializer` when serializing the `Config`,
and realise it can't just output the foreign key. Unfortunately, getting the
serialization class for the child repo currently requires loading the record
(via evaluating `lazy_assocation`), and loses the performance benefit of the
existing `belongs_to?` path. Instead, I've opted to use
`read_attribute_for_serialization` instead of `object.send` to fetch the
serialized foreign key. This allows the serialized relationship ID to be
overwritten using

```(ruby)
class ConfigSerializer < ActiveModel::Serializer
  ...

  def repo_id
    object.repo.github_id
  end
end
```
2017-05-13 15:22:18 +01:00
Benjamin Fleischer
b48aeeef1e Merge branch 'master' into 0-10-stable 2017-05-01 11:04:46 -05:00
Benjamin Fleischer
01d4f0464b Merge branch 'master' into 0-10-stable 2017-03-07 15:52:27 -06:00
Benjamin Fleischer
f353dedf28 Merge pull request #2038 from nsarno/patch-1
Fix small typo in README.md
2017-01-31 09:05:59 -06:00
Arnaud Mesureur
db617b8bf9 Fix small typo in README.md 2017-01-23 15:42:03 +11:00
Benjamin Fleischer
9a2f4894ed Merge pull request #1981 from groyoh/link_doc
Fix relationship links doc
Conflicts:
	CHANGELOG.md
2017-01-10 01:07:18 -06:00
Benjamin Fleischer
4c6f104ae9 Merge pull request #2022 from bf4/fix_model_attribute_accessors
Fix model attribute accessors
2017-01-10 01:05:27 -06:00
Benjamin Fleischer
1570437795 Add CHANGELOG 2017-01-10 00:41:09 -06:00
Benjamin Fleischer
ec1022ecb3 Test bugfix 2017-01-10 00:41:09 -06:00
Benjamin Fleischer
b5f886cb95 Randomize testing of compatibility layer against regressions 2017-01-10 00:41:09 -06:00
Benjamin Fleischer
ec905d8179 Fix typos 2017-01-10 00:41:08 -06:00
Benjamin Fleischer
c25f2f3863 Fix model attributes accessors 2017-01-10 00:41:08 -06:00
Benjamin Fleischer
4e6bd61350 Merge pull request #2021 from bf4/make_tests_explicit
Add Model#attributes helper; make test attributes explicit
2017-01-08 16:37:53 -05:00
Benjamin Fleischer
d5babdd060 Add Model#attributes helper; make test attributes explicit 2017-01-07 21:50:11 -06:00
Benjamin Fleischer
85dfef9072 Merge pull request #2012 from bf4/cleanup_isolated_jsonapi_renderer_tests_a_bit
Cleanup assertions in isolated jsonapi renderer tests a bit
2017-01-07 16:31:45 -06:00
Ankit Shah
59aed4d00e Updated isolated tests to assert correct behavior. (#2010)
* Updated isolated tests to assert correct behavior.
* Added check to get unsafe params if rails version is great than 5
2017-01-07 16:27:57 -06:00
Benjamin Fleischer
a5423dab20 Merge pull request #2017 from bf4/remove_warnings
Fix mt6 assert_nil warnings
2017-01-07 16:26:34 -06:00
Benjamin Fleischer
9a2e1e4743 Merge pull request #2020 from bf4/silence_grape_warnings
Silence Grape warnings
2017-01-07 16:26:24 -06:00
Benjamin Fleischer
0dd7680fe5 Merge pull request #2019 from bf4/fix_method_redefined_warning
Fix AMS warnings
2017-01-07 16:26:18 -06:00
Benjamin Fleischer
21bcfd891d Merge pull request #2018 from rails-api/bump_version
Bump to v0.10.4 [ci skip]
Conflicts:
	CHANGELOG.md
2017-01-07 16:25:50 -06:00
Benjamin Fleischer
82db1301f6 Bump to v0.10.4 2017-01-06 16:21:04 -06:00
Benjamin Fleischer
23f03ffd30 Merge pull request #2016 from rails-api/prepare_release
Prepare release of 0.10.4
2017-01-06 17:14:08 -05:00
Benjamin Fleischer
97b587b14c Merge pull request #2005 from kofronpi/support-ruby-2.4
Update jsonapi runtime dependency to 0.1.1.beta6
2017-01-06 14:17:37 -06:00
L. Preston Sego III
adf110f4df Swap out KeyTransform for CaseTransform (#1993)
* delete KeyTransform, use CaseTransform

* added changelog

Conflicts:
	CHANGELOG.md
2017-01-06 14:17:37 -06:00
Benjamin Fleischer
2c0b15d22f Merge pull request #2007 from bf4/check_ci
Test was failing due to change in JSON exception message when parsing empty string
2017-01-06 14:17:35 -06:00
Benjamin Fleischer
dfa6caac27 Merge pull request #2000 from berfarah/patch-1
Link to 0.10.3 tag instead of `master` branch
2017-01-05 21:36:32 -06:00
Benjamin Fleischer
49f2dca730 Merge pull request #1999 from bf4/typos
Fix typos [ci skip]
2017-01-05 21:36:25 -06:00
Benjamin Fleischer
4054f43309 Merge pull request #1994 from bf4/promote_architecture
Promote important architecture description that answers a lot of questions we get
Conflicts:
	docs/ARCHITECTURE.md
2017-01-05 21:35:57 -06:00
L. Preston Sego III
e036b71e0c Merge pull request #1992 from ojiry/bump_ruby_versions
Run tests by Ruby 2.2.6 and 2.3.3
2017-01-05 21:33:07 -06:00
Benjamin Fleischer
6e11d6234e Merge pull request #1990 from mxie/mx-result-typo
Fix typos and capitalization in Relationship Links docs [ci skip]
2017-01-05 21:32:31 -06:00
172 changed files with 1481 additions and 392 deletions

View File

@ -59,6 +59,10 @@ Style/MissingElse:
Style/EmptyElse: Style/EmptyElse:
EnforcedStyle: empty EnforcedStyle: empty
Style/FrozenStringLiteralComment:
Enabled: true
EnforcedStyle: always
Style/MultilineOperationIndentation: Style/MultilineOperationIndentation:
EnforcedStyle: indented EnforcedStyle: indented

View File

@ -1,55 +1,104 @@
language: ruby language: ruby
sudo: false sudo: false
rvm: ruby_supported_versions:
- 2.1 - &ruby_2_1 2.1.10
- 2.2.6 - &ruby_2_2 2.2.10
- 2.3.3 - &ruby_2_3 2.3.8
- ruby-head - &ruby_2_4 2.4.6
- jruby-9.1.5.0 # is precompiled per http://rubies.travis-ci.org/ - &ruby_2_5 2.5.5
- jruby-head - &ruby_2_6 2.6.3
- &ruby_head ruby-head
jdk: rails_supported_versions:
- oraclejdk8 - &rails_4_1 RAILS_VERSION=4.1
- &rails_4_2 RAILS_VERSION=4.2
- &rails_5_0 RAILS_VERSION=5.0
- &rails_5_1 RAILS_VERSION=5.1
- &rails_5_2 RAILS_VERSION=5.2
- &rails_6_0_0_rc1 RAILS_VERSION=6.0.0.rc1
- &rails_master RAILS_VERSION=master
before_install:
- gem update --system
- rvm @global do gem uninstall bundler -a -x
- rvm @global do gem install bundler -v 1.13.7
install: bundle install --path=vendor/bundle --retry=3 --jobs=3
cache: cache:
directories: directories:
- vendor/bundle - vendor/bundle
before_install:
- "travis_retry gem update --system 2.7.9"
- "travis_retry gem install bundler -v '1.17.3'"
install: BUNDLER_VERSION=1.17.3 bundle install --path=vendor/bundle --retry=3 --jobs=3
script: script:
- bundle exec rake ci - bundle exec rake ci
after_success: after_success:
- codeclimate-test-reporter - codeclimate-test-reporter
env: env:
global:
- "JRUBY_OPTS='--dev -J-Xmx1024M --debug'"
matrix: matrix:
- "RAILS_VERSION=4.1" - *rails_4_1
- "RAILS_VERSION=4.2" - *rails_4_2
- "RAILS_VERSION=5.0" - *rails_5_0
- "RAILS_VERSION=master" - *rails_5_1
- *rails_5_2
- *rails_6_0_0_rc1
- *rails_master
rvm:
- *ruby_2_1
- *ruby_2_2
- *ruby_2_3
- *ruby_2_4
- *ruby_2_5
- *ruby_2_6
- *ruby_head
branches:
only: 0-10-stable
matrix: matrix:
include:
- { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=4.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
- { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=4.2 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
- { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
# See JRuby currently failing on Rails 5+ https://github.com/jruby/activerecord-jdbc-adapter/issues/708
# - { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=5.0 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
# - { rvm: jruby-head, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
exclude: exclude:
- rvm: 2.1 - { rvm: *ruby_2_1, env: *rails_5_0 }
env: RAILS_VERSION=master - { rvm: *ruby_2_1, env: *rails_5_1 }
- rvm: jruby-9.1.5.0 - { rvm: *ruby_2_1, env: *rails_5_2 }
env: RAILS_VERSION=master
- rvm: jruby-head - { rvm: *ruby_2_1, env: *rails_6_0_0_rc1 }
env: RAILS_VERSION=master - { rvm: *ruby_2_2, env: *rails_6_0_0_rc1 }
- rvm: 2.1 - { rvm: *ruby_2_3, env: *rails_6_0_0_rc1 }
env: RAILS_VERSION=5.0 - { rvm: *ruby_2_4, env: *rails_6_0_0_rc1 }
- rvm: jruby-9.1.5.0
env: RAILS_VERSION=5.0 - { rvm: *ruby_2_1, env: *rails_master }
- rvm: jruby-head - { rvm: *ruby_2_2, env: *rails_master }
env: RAILS_VERSION=5.0 - { rvm: *ruby_2_3, env: *rails_master }
- { rvm: *ruby_2_4, env: *rails_master }
allow_failures: allow_failures:
- rvm: ruby-head - { rvm: *ruby_2_4, env: *rails_4_1 }
- { rvm: *ruby_2_5, env: *rails_4_1 }
- { rvm: *ruby_2_6, env: *rails_4_1 }
# allow RAILS_VERSION=master to fail against ruby 2.5+ until this gem supports RAILS_VERSION
# https://github.com/rails/rails/blob/master/RAILS_VERSION
# https://github.com/rails-api/active_model_serializers/blob/0-10-stable/active_model_serializers.gemspec#L24
- { rvm: *ruby_2_5, env: *rails_master }
- { rvm: *ruby_2_6, env: *rails_master }
- rvm: *ruby_head
# - { rvm: *ruby_head, env: *rails_4_1 }
# - { rvm: *ruby_head, env: *rails_4_2 }
# - { rvm: *ruby_head, env: *rails_5_0 }
# - { rvm: *ruby_head, env: *rails_5_1 }
# - { rvm: *ruby_head, env: *rails_5_2 }
# - { rvm: *ruby_head, env: *rails_6_0_0_rc1 }
# - { rvm: *ruby_head, env: *rails_master }
- rvm: jruby-head - rvm: jruby-head
# See JRuby currently failing on Rails 5+ https://github.com/jruby/activerecord-jdbc-adapter/issues/708
- { rvm: jruby-9.1.13.0, jdk: oraclejdk8, env: "RAILS_VERSION=5.1 JRUBY_OPTS='--dev -J-Xmx1024M --debug'" }
fast_finish: true fast_finish: true

View File

@ -1,6 +1,6 @@
## 0.10.x ## 0.10.x
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...master) ### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.10...0-10-stable)
Breaking changes: Breaking changes:
@ -10,6 +10,96 @@ Fixes:
Misc: Misc:
### [v0.10.10 (2019-07-13)](https://github.com/rails-api/active_model_serializers/compare/v0.10.9...v0.10.10)
Fixes:
- [#2319](https://github.com/rails-api/active_model_serializers/pull/2319) Fixes #2316. (@kylekeesling)
- Fix Rails 6.0 deprication warnings
- update test fixture schema to use `timestamps` instead of `timestamp`
- [#2223](https://github.com/rails-api/active_model_serializers/pull/2223) Support Fieldset in Attributes/JSON adapters documented in [docs/general/fields.md](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/fields.md) that worked partially before (@bf4)
- [#2337](https://github.com/rails-api/active_model_serializers/pull/2337) fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank (@InteNs)
- Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank
- [#2172](https://github.com/rails-api/active_model_serializers/pull/2172) Preserve the namespace when falling back to a superclass serializer
Misc:
- [#2327](https://github.com/rails-api/active_model_serializers/pull/2327) Add support for Ruby 2.6 on Travis CI (@wasifhossain)
- [#2304](https://github.com/rails-api/active_model_serializers/pull/2304) Slim down bundled gem by excluding test files and docs (@greysteil)
### [v0.10.9 (2019-02-08)](https://github.com/rails-api/active_model_serializers/compare/v0.10.8...v0.10.9)
Fixes:
- [#2288](https://github.com/rails-api/active_model_serializers/pull/2288)
Change the fetch method to deal with recyclable key cache strategy.
Fixes #2287. (@cintamani, @wasifhossain)
- [#2307](https://github.com/rails-api/active_model_serializers/pull/2307) Falsey attribute values should not be reevaluated.
Misc:
- [#2309](https://github.com/rails-api/active_model_serializers/pull/2309) Performance and memory usage fixes
### [v0.10.8 (2018-11-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.7...v0.10.8)
Features:
- [#2279](https://github.com/rails-api/active_model_serializers/pull/2279) Support condition options in serializer link statements
Fixes:
- [#2296](https://github.com/rails-api/active_model_serializers/pull/2296) Fixes #2295 (@Hirurg103)
- Fix finding of namespaced serializer and non-namespaced model.
- [#2289](https://github.com/rails-api/active_model_serializers/pull/2289) Fixes #2255 (@f-mer)
- Fix autoloading race condition, especially in Rails 5.
- [#2299](https://github.com/rails-api/active_model_serializers/pull/2299) Fixes #2270 (@chau-bao-long via #2276)
- Fix reflection thread-safety bug
### [v0.10.7 (2017-11-14)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...v0.10.7)
Regressions Fixed From v0.10.6:
- [#2211](https://github.com/rails-api/active_model_serializers/pull/2211). Fixes #2125, #2160. (@bf4)
- Fix polymorphic belongs_to tests; passes on v0.10.5, fails on v0.10.6
- Fix JSON:API polymorphic type regression from v0.10.5
- Fix JSON:API: for_type_and_id should always inflect_type
```
Should Serializer._type ever be inflected?
Right now, it won't be, but association.serializer._type will be inflected.
That's the current behavior.
```
- [#2216](https://github.com/rails-api/active_model_serializers/pull/2216). Fixes #2132, #2180. (@bf4)
- Fix JSON:API: Serialize resource type for unpersisted records (blank id)
- [#2218](https://github.com/rails-api/active_model_serializers/pull/2218). Fixes #2178. (@bf4)
- Fix JSON:API: Make using foreign key on belongs_to opt-in. No effect on polymorphic relationships.
```
# set to true to opt-in
ActiveModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
```
Features:
- [#2136](https://github.com/rails-api/active_model_serializers/pull/2136) Enable inclusion of sideloaded relationship objects by `key`. (@caomania)
- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4)
- [#2130](https://github.com/rails-api/active_model_serializers/pull/2130) Allow serialized ID to be overwritten for belongs-to relationships. (@greysteil)
- [#2189](https://github.com/rails-api/active_model_serializers/pull/2189)
Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.3']`
(@tagliala)
Fixes:
- [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
- [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)
- [#2214](https://github.com/rails-api/active_model_serializers/pull/2214) Fail if unable to infer collection type with json adapter. (@jmeredith16)
- [#2149](https://github.com/rails-api/active_model_serializers/pull/2149) Always include self, first, last pagination link. (@mecampbellsoup)
- [#2179](https://github.com/rails-api/active_model_serializers/pull/2179) Fixes #2173, Pass block to Enumerator.new. (@drn)
Misc:
- [#2176](https://github.com/rails-api/active_model_serializers/pull/2176) Documentation for global adapter config. (@mrpinsky)
- [#2215](https://github.com/rails-api/active_model_serializers/pull/2215) Update `serializers.md` documentation to denote alternate use cases for `scope`. (@stratigos)
- [#2212](https://github.com/rails-api/active_model_serializers/pull/2212) Remove legacy has_many_embed_ids test. (@bf4)
### [v0.10.6 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...v0.10.6) ### [v0.10.6 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...v0.10.6)
Fixes: Fixes:

32
Gemfile
View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
source 'https://rubygems.org' source 'https://rubygems.org'
# #
# Add a Gemfile.local to locally bundle gems outside of version control # Add a Gemfile.local to locally bundle gems outside of version control
@ -12,6 +14,7 @@ version = ENV['RAILS_VERSION'] || '4.2'
if version == 'master' if version == 'master'
gem 'rack', github: 'rack/rack' gem 'rack', github: 'rack/rack'
gem 'arel', github: 'rails/arel' gem 'arel', github: 'rails/arel'
gem 'rails', github: 'rails/rails'
git 'https://github.com/rails/rails.git' do git 'https://github.com/rails/rails.git' do
gem 'railties' gem 'railties'
gem 'activesupport' gem 'activesupport'
@ -23,6 +26,7 @@ if version == 'master'
end end
else else
gem_version = "~> #{version}.0" gem_version = "~> #{version}.0"
gem 'rails', gem_version
gem 'railties', gem_version gem 'railties', gem_version
gem 'activesupport', gem_version gem 'activesupport', gem_version
gem 'activemodel', gem_version gem 'activemodel', gem_version
@ -36,18 +40,38 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby]) gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
if ENV['CI']
if RUBY_VERSION < '2.4'
# Windows: An error occurred while installing nokogiri (1.8.0)
gem 'nokogiri', '< 1.7', platforms: @windows_platforms
end
end
group :bench do group :bench do
# https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76 # https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
end end
group :test do group :test do
gem 'sqlite3', platform: (@windows_platforms + [:ruby]) platforms(*(@windows_platforms + [:ruby])) do
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby if version == 'master' || version >= '6'
gem 'sqlite3', '~> 1.4'
else
gem 'sqlite3', '~> 1.3.13'
end
end
platforms :jruby do
if version == 'master' || version >= '5'
gem 'activerecord-jdbcsqlite3-adapter', '~> 50'
else
gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3.0'
end
end
gem 'codeclimate-test-reporter', require: false gem 'codeclimate-test-reporter', require: false
gem 'm', '~> 1.5' gem 'm', '~> 1.5'
gem 'pry', '~> 0.10' gem 'pry', '>= 0.10'
gem 'pry-byebug', '~> 3.4', platform: :ruby gem 'byebug', '~> 8.2' if RUBY_VERSION < '2.2'
gem 'pry-byebug', platforms: :ruby
end end
group :development, :test do group :development, :test do

View File

@ -49,8 +49,6 @@ Check how to change the adapter in the sections below.
`0.10.x` is based on the `0.8.0` code, but with a more flexible `0.10.x` is based on the `0.8.0` code, but with a more flexible
architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md) architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
It is generally safe and recommended to use the master branch.
## Installation ## Installation
Add this line to your application's Gemfile: Add this line to your application's Gemfile:
@ -187,7 +185,7 @@ and, if there is no serializer, primitives.
### ActiveModelSerializers::Adapter::Base ### ActiveModelSerializers::Adapter::Base
The **`ActiveModelSerializeres::Adapter::Base`** describes the structure of the JSON document generated from a The **`ActiveModelSerializers::Adapter::Base`** describes the structure of the JSON document generated from a
serializer. For example, the `Attributes` example represents each serializer as its serializer. For example, the `Attributes` example represents each serializer as its
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
API](http://jsonapi.org/) document. API](http://jsonapi.org/) document.
@ -268,8 +266,8 @@ to know about, but not part of ActiveModelSerializers.)
- An `ActiveRecord::Base` object. - An `ActiveRecord::Base` object.
- Any Ruby object that passes the - Any Ruby object that passes the
[Lint](http://www.rubydoc.info/github/rails-api/active_model_serializers/ActiveModel/Serializer/Lint/Tests) [Lint](https://www.rubydoc.info/gems/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
[code](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb). [(code)](lib/active_model/serializer/lint.rb).
ActiveModelSerializers provides a ActiveModelSerializers provides a
[`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb), [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb),

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
begin begin
require 'bundler/setup' require 'bundler/setup'
rescue LoadError rescue LoadError

View File

@ -1,3 +1,4 @@
# frozen_string_literal: true
# coding: utf-8 # coding: utf-8
lib = File.expand_path('../lib', __FILE__) lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
@ -14,14 +15,13 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/rails-api/active_model_serializers' spec.homepage = 'https://github.com/rails-api/active_model_serializers'
spec.license = 'MIT' spec.license = 'MIT'
spec.files = `git ls-files -z`.split("\x0") spec.files = Dir["CHANGELOG.md", "MIT-LICENSE", "README.md", "lib/**/*"]
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib'] spec.require_paths = ['lib']
spec.executables = [] spec.executables = []
spec.required_ruby_version = '>= 2.1' spec.required_ruby_version = '>= 2.1'
rails_versions = ['>= 4.1', '< 6'] rails_versions = ['>= 4.1', '< 6.1']
spec.add_runtime_dependency 'activemodel', rails_versions spec.add_runtime_dependency 'activemodel', rails_versions
# 'activesupport', rails_versions # 'activesupport', rails_versions
# 'builder' # 'builder'
@ -39,10 +39,10 @@ Gem::Specification.new do |spec|
# 'activesupport', rails_versions # 'activesupport', rails_versions
# 'i18n, # 'i18n,
# 'tzinfo' # 'tzinfo'
# 'minitest' spec.add_development_dependency 'minitest', ['~> 5.0', '< 5.11']
# 'thread_safe' # 'thread_safe'
spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2'] spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.3']
spec.add_runtime_dependency 'case_transform', '>= 0.2' spec.add_runtime_dependency 'case_transform', '>= 0.2'
spec.add_development_dependency 'activerecord', rails_versions spec.add_development_dependency 'activerecord', rails_versions

View File

@ -4,20 +4,22 @@ skip_tags: true
environment: environment:
JRUBY_OPTS: "--dev -J-Xmx1024M --debug" JRUBY_OPTS: "--dev -J-Xmx1024M --debug"
RAILS_VERSION: 5.2
matrix: matrix:
- ruby_version: "Ruby21" - ruby_version: "Ruby23"
- ruby_version: "Ruby21-x64" - ruby_version: "Ruby23-x64"
cache: cache:
- vendor/bundle - vendor/bundle
install: install:
- SET PATH=C:\%ruby_version%\bin;%PATH% - SET PATH=C:\%ruby_version%\bin;%PATH%
- gem update --system - gem uninstall bundler -a -x -I
- gem uninstall bundler -a -x - gem update --system 2.7.9
- gem install bundler -v 1.13.7 - # gem install bundler -v '1.17.3'
- bundle env - bundle env
- bundle install --path=vendor/bundle --retry=3 --jobs=3 - bundle check || bundle install --path=vendor/bundle --retry=3 --jobs=3
- bundle clean --force
before_test: before_test:
- ruby -v - ruby -v

View File

@ -36,6 +36,12 @@ The `Attributes` adapter does not include a root key. It is just the serialized
Use either the `JSON` or `JSON API` adapters if you want the response document to have a root key. Use either the `JSON` or `JSON API` adapters if you want the response document to have a root key.
***IMPORTANT***: Adapter configuration has *no effect* on a serializer instance
being used directly. That is, `UserSerializer.new(user).as_json` will *always*
behave as if the adapter were the 'Attributes' adapter. See [Outside Controller
Usage](../howto/outside_controller_use.md) for more details on recommended
usage.
## Built in Adapters ## Built in Adapters
### Attributes - Default ### Attributes - Default

View File

@ -57,6 +57,7 @@ still prefer the render option `:key_transform` over this setting.
application, setting `config.key_transform` to `:unaltered` will provide a performance boost.* application, setting `config.key_transform` to `:unaltered` will provide a performance boost.*
##### default_includes ##### default_includes
What relationships to serialize by default. Default: `'*'`, which includes one level of related What relationships to serialize by default. Default: `'*'`, which includes one level of related
objects. See [includes](adapters.md#included) for more info. objects. See [includes](adapters.md#included) for more info.
@ -162,6 +163,21 @@ Default: `{}`.
*Used when `jsonapi_include_toplevel_object` is `true`* *Used when `jsonapi_include_toplevel_object` is `true`*
##### jsonapi_use_foreign_key_on_belongs_to_relationship
When true, the relationship will determine its resource object identifier
without calling the association or its serializer. This can be useful when calling
the association object is triggering unnecessary queries.
For example, if a `comment` belongs to a `post`, and the comment
uses the foreign key `post_id`, we can determine the resource object
identifier `id` as `comment.post_id` and the `type` from the association options.
Or quite simply, it behaves as `belongs_to :post, type: :posts, foreign_key: :post_id`.
Note: This option has *no effect* on polymorphic associations as we cannot reliably
determine the associated object's type without instantiating it.
Default: `false`.
## Hooks ## Hooks

View File

@ -290,4 +290,4 @@ See [Usage outside of a controller](../howto/outside_controller_use.md#serializi
## Pagination ## Pagination
See [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md). See [How to add pagination links](../howto/add_pagination_links.md).

View File

@ -65,7 +65,7 @@ Where:
- `virtual_value:` - `virtual_value:`
- `polymorphic:` defines if polymorphic relation type should be nested in serialized association. - `polymorphic:` defines if polymorphic relation type should be nested in serialized association.
- `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship. - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship.
- `class_name:` used to determine `type` when `type` not given - `class_name:` the (String) model name used to determine `type`, when `type` is not given. e.g. `class_name: "Comment"` would imply the type `comments`
- `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object. - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object.
- `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details. - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details.
- optional: `&block` is a context that returns the association's attributes. - optional: `&block` is a context that returns the association's attributes.
@ -81,6 +81,7 @@ e.g.
```ruby ```ruby
has_one :bio has_one :bio
has_one :blog, key: :site has_one :blog, key: :site
has_one :blog, class_name: "Blog"
has_one :maker, virtual_value: { id: 1 } has_one :maker, virtual_value: { id: 1 }
has_one :blog do |serializer| has_one :blog do |serializer|
@ -114,6 +115,7 @@ e.g.
has_many :comments has_many :comments
has_many :comments, key: :reviews has_many :comments, key: :reviews
has_many :comments, serializer: CommentPreviewSerializer has_many :comments, serializer: CommentPreviewSerializer
has_many :comments, class_name: "Comment"
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }] has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
has_many :comments, key: :last_comments do has_many :comments, key: :last_comments do
last(1) last(1)
@ -127,6 +129,7 @@ e.g.
```ruby ```ruby
belongs_to :author, serializer: AuthorPreviewSerializer belongs_to :author, serializer: AuthorPreviewSerializer
belongs_to :author, key: :writer belongs_to :author, key: :writer
belongs_to :author, class_name: "Author"
belongs_to :post belongs_to :post
belongs_to :blog belongs_to :blog
def blog def blog
@ -235,6 +238,15 @@ link :other, 'https://example.com/resource'
link(:posts) { link_author_posts_url(object) } link(:posts) { link_author_posts_url(object) }
``` ```
Just like attributes, links also support conditions in options
```ruby
link(:secret, if: :internal?) { object.secret_link }
def internal?
instance_options[:context] == :internal
end
```
#### #object #### #object
The object being serialized. The object being serialized.
@ -294,6 +306,8 @@ end
Whether you write the method as above or as `object.comments.where(created_by: scope)` Whether you write the method as above or as `object.comments.where(created_by: scope)`
is a matter of preference (assuming `scope_name` has been set). is a matter of preference (assuming `scope_name` has been set).
Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers.
##### Controller Authorization Context ##### Controller Authorization Context
In the controller, the scope/scope_name options are equal to In the controller, the scope/scope_name options are equal to
@ -311,7 +325,7 @@ current authorization scope when you call `render :json`.
called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477) called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope). in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
We can change the scope from `current_user` to `view_context`. We can change the scope from `current_user` to `view_context`, which is included in subclasses of `ActionController::Base`.
```diff ```diff
class SomeController < ActionController::Base class SomeController < ActionController::Base
@ -379,6 +393,7 @@ class PostsController < ActionController::Base
end end
end end
``` ```
Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`.
#### #read_attribute_for_serialization(key) #### #read_attribute_for_serialization(key)
@ -437,7 +452,7 @@ serializer classes.
## More Info ## More Info
For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb) For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model/serializer.rb)
## Overriding association methods ## Overriding association methods
@ -471,7 +486,7 @@ the `ActiveModel::Serializer.serializer_for` method to return a serializer class
```ruby ```ruby
class MySerializer < ActiveModel::Serializer class MySerializer < ActiveModel::Serializer
def self.serializer_for(model, options) def self.serializer_for(model, options)
return SparseAdminSerializer if model.class == 'Admin' return SparseAdminSerializer if model.class.name == 'Admin'
super super
end end

View File

@ -18,6 +18,13 @@ In order to add the root key you need to use the ```JSON``` Adapter, you can cha
ActiveModelSerializers.config.adapter = :json ActiveModelSerializers.config.adapter = :json
``` ```
Note that adapter configuration has no effect on a serializer that is called
directly, e.g. in a serializer unit test. Instead, something like
`UserSerializer.new(user).as_json` will *always* behave as if the adapter were
the 'Attributes' adapter. See [Outside Controller
Usage](../howto/outside_controller_use.md) for more details on recommended
usage.
You can also specify a class as adapter, as long as it complies with the ActiveModelSerializers adapters interface. You can also specify a class as adapter, as long as it complies with the ActiveModelSerializers adapters interface.
It will add the root key to all your serialized endpoints. It will add the root key to all your serialized endpoints.

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/class/attribute'
require 'active_model_serializers/serialization_context' require 'active_model_serializers/serialization_context'
@ -21,7 +23,15 @@ module ActionController
end end
def namespace_for_serializer def namespace_for_serializer
@namespace_for_serializer ||= self.class.parent unless self.class.parent == Object @namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
end
def namespace_for_class(klass)
if Module.method_defined?(:module_parent)
klass.module_parent
else
klass.parent
end
end end
def serialization_scope def serialization_scope

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'set' require 'set'
module ActiveModel module ActiveModel

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'thread_safe' require 'thread_safe'
require 'jsonapi/include_directive' require 'jsonapi/include_directive'
require 'active_model/serializer/collection_serializer' require 'active_model/serializer/collection_serializer'
@ -18,16 +20,17 @@ module ActiveModel
# @see #serializable_hash for more details on these valid keys. # @see #serializable_hash for more details on these valid keys.
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
eager_autoload do
autoload :Adapter autoload :Adapter
autoload :Null autoload :Null
autoload :Attribute autoload :Attribute
autoload :Link
autoload :Association autoload :Association
autoload :Reflection autoload :Reflection
autoload :SingularReflection
autoload :CollectionReflection
autoload :BelongsToReflection autoload :BelongsToReflection
autoload :HasOneReflection autoload :HasOneReflection
autoload :HasManyReflection autoload :HasManyReflection
end
include ActiveSupport::Configurable include ActiveSupport::Configurable
include Caching include Caching
@ -91,7 +94,7 @@ module ActiveModel
if serializer_class if serializer_class
serializer_class serializer_class
elsif klass.superclass elsif klass.superclass
get_serializer_for(klass.superclass) get_serializer_for(klass.superclass, namespace)
else else
nil # No serializer found nil # No serializer found
end end
@ -142,6 +145,7 @@ module ActiveModel
# Make JSON API top-level jsonapi member opt-in # Make JSON API top-level jsonapi member opt-in
# ref: http://jsonapi.org/format/#document-top-level # ref: http://jsonapi.org/format/#document-top-level
config.jsonapi_include_toplevel_object = false config.jsonapi_include_toplevel_object = false
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
config.include_data_default = true config.include_data_default = true
# For configuring how serializers are found. # For configuring how serializers are found.
@ -274,9 +278,14 @@ module ActiveModel
# link(:self) { "http://example.com/resource/#{object.id}" } # link(:self) { "http://example.com/resource/#{object.id}" }
# @example # @example
# link :resource, "http://example.com/resource" # link :resource, "http://example.com/resource"
# @example
# link(:callback, if: :internal?), { "http://example.com/callback" }
# #
def self.link(name, value = nil, &block) def self.link(name, *args, &block)
_links[name] = block || value options = args.extract_options!
# For compatibility with the use case of passing link directly as string argument
# without block, we are creating a wrapping block
_links[name] = Link.new(name, options, block || ->(_serializer) { args.first })
end end
# Set the JSON API meta attribute of a serializer. # Set the JSON API meta attribute of a serializer.
@ -337,10 +346,10 @@ module ActiveModel
# @return [Enumerator<Association>] # @return [Enumerator<Association>]
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil) def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
include_slice ||= include_directive include_slice ||= include_directive
return Enumerator.new unless object return Enumerator.new {} unless object
Enumerator.new do |y| Enumerator.new do |y|
self.class._reflections.each do |key, reflection| (self.instance_reflections ||= self.class._reflections.deep_dup).each do |key, reflection|
next if reflection.excluded?(self) next if reflection.excluded?(self)
next unless include_directive.key?(key) next unless include_directive.key?(key)
@ -356,6 +365,9 @@ module ActiveModel
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance) def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
adapter_options ||= {} adapter_options ||= {}
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options) options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
if (fieldset = adapter_options[:fieldset])
options[:fields] = fieldset.fields_for(json_key)
end
resource = attributes_hash(adapter_options, options, adapter_instance) resource = attributes_hash(adapter_options, options, adapter_instance)
relationships = associations_hash(adapter_options, options, adapter_instance) relationships = associations_hash(adapter_options, options, adapter_instance)
resource.merge(relationships) resource.merge(relationships)
@ -370,7 +382,12 @@ module ActiveModel
# Used by adapter as resource root. # Used by adapter as resource root.
def json_key def json_key
root || _type || object.class.model_name.to_s.underscore root || _type ||
begin
object.class.model_name.to_s.underscore
rescue ArgumentError
'anonymous_object'
end
end end
def read_attribute_for_serialization(attr) def read_attribute_for_serialization(attr)
@ -404,6 +421,6 @@ module ActiveModel
protected protected
attr_accessor :instance_options attr_accessor :instance_options, :instance_reflections
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model_serializers/adapter' require 'active_model_serializers/adapter'
require 'active_model_serializers/deprecate' require 'active_model_serializers/deprecate'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
module Adapter module Adapter

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
module Adapter module Adapter

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
module Adapter module Adapter

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
module Adapter module Adapter

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
module Adapter module Adapter

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model/serializer/collection_serializer' require 'active_model/serializer/collection_serializer'
module ActiveModel module ActiveModel

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model/serializer/lazy_association' require 'active_model/serializer/lazy_association'
module ActiveModel module ActiveModel

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model/serializer/field' require 'active_model/serializer/field'
module ActiveModel module ActiveModel

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
# @api private # @api private

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
class CollectionSerializer class CollectionSerializer
@ -19,11 +21,10 @@ module ActiveModel
# @api private # @api private
def serializable_hash(adapter_options, options, adapter_instance) def serializable_hash(adapter_options, options, adapter_instance)
include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options) options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive) options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, options[:include_directive])
adapter_opts = adapter_options.merge(include_directive: include_directive)
serializers.map do |serializer| serializers.map do |serializer|
serializer.serializable_hash(adapter_opts, options, adapter_instance) serializer.serializable_hash(adapter_options, options, adapter_instance)
end end
end end
@ -46,7 +47,9 @@ module ActiveModel
# 3. get from collection name, if a named collection # 3. get from collection name, if a named collection
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
# 4. key may be nil for empty collection and no serializer option # 4. key may be nil for empty collection and no serializer option
key && key.pluralize key &&= key.pluralize
# 5. fail if the key cannot be determined
key || fail(ArgumentError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
end end
# rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/CyclomaticComplexity

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
UndefinedCacheKey = Class.new(StandardError) UndefinedCacheKey = Class.new(StandardError)
@ -281,7 +283,9 @@ module ActiveModel
# Use object's cache_key if available, else derive a key from the object # Use object's cache_key if available, else derive a key from the object
# Pass the `key` option to the `cache` declaration or override this method to customize the cache key # Pass the `key` option to the `cache` declaration or override this method to customize the cache key
def object_cache_key def object_cache_key
if object.respond_to?(:cache_key) if object.respond_to?(:cache_key_with_version)
object.cache_key_with_version
elsif object.respond_to?(:cache_key)
object.cache_key object.cache_key
elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key])) elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
object_time_safe = object.updated_at object_time_safe = object.updated_at

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
class ErrorSerializer < ActiveModel::Serializer class ErrorSerializer < ActiveModel::Serializer

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model/serializer/error_serializer' require 'active_model/serializer/error_serializer'
module ActiveModel module ActiveModel

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
# Holds all the meta-data about a field (i.e. attribute or association) as it was # Holds all the meta-data about a field (i.e. attribute or association) as it was

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
class Fieldset class Fieldset

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
# @api private # @api private

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
# @api private # @api private

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
# @api private # @api private
@ -7,11 +9,12 @@ module ActiveModel
delegate :collection?, to: :reflection delegate :collection?, to: :reflection
def reflection_options def reflection_options
@reflection_options ||= reflection.options.dup.reject { |k, _| !REFLECTION_OPTIONS.include?(k) } @reflection_options ||= reflection.options.select { |k, _| REFLECTION_OPTIONS.include?(k) }
end end
def object def object
@object ||= reflection.value( return @object if defined?(@object)
@object = reflection.value(
association_options.fetch(:parent_serializer), association_options.fetch(:parent_serializer),
association_options.fetch(:include_slice) association_options.fetch(:include_slice)
) )
@ -76,6 +79,7 @@ module ActiveModel
serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
serializer = reflection_options.fetch(:serializer, nil) serializer = reflection_options.fetch(:serializer, nil)
serializer_options[:serializer] = serializer if serializer serializer_options[:serializer] = serializer if serializer
serializer_options[:namespace] = reflection_options[:namespace] if reflection_options[:namespace]
serializer_class.new(object, serializer_options) serializer_class.new(object, serializer_options)
end end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'active_model/serializer/field'
module ActiveModel
class Serializer
# Holds all the data about a serializer link
#
# @example
# class PostSerializer < ActiveModel::Serializer
# link :callback, if: :internal? do
# object.callback_link
# end
#
# def internal?
# instance_options[:internal] == true
# end
# end
#
class Link < Field
end
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
module Lint module Lint

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
class Null < Serializer class Null < Serializer

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model/serializer/field' require 'active_model/serializer/field'
require 'active_model/serializer/association' require 'active_model/serializer/association'
@ -140,7 +142,7 @@ module ActiveModel
def include_data?(include_slice) def include_data?(include_slice)
include_data_setting = options[:include_data_setting] include_data_setting = options[:include_data_setting]
case include_data_setting case include_data_setting
when :if_sideloaded then include_slice.key?(name) when :if_sideloaded then include_slice.key?(options.fetch(:key, name))
when true then true when true then true
when false then false when false then false
else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'" else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'"
@ -151,6 +153,9 @@ module ActiveModel
# @yield [ActiveModel::Serializer] # @yield [ActiveModel::Serializer]
# @return [:nil, associated resource or resource collection] # @return [:nil, associated resource or resource collection]
def value(serializer, include_slice) def value(serializer, include_slice)
# NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe
# Therefore, when we build associations from reflections, we dup the entire reflection instance.
# Better solutions much appreciated!
@object = serializer.object @object = serializer.object
@scope = serializer.scope @scope = serializer.scope

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
module ActiveModel module ActiveModel
class Serializer class Serializer
VERSION = '0.10.6'.freeze VERSION = '0.10.10'.freeze
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_model' require 'active_model'
require 'active_support' require 'active_support'
require 'active_support/core_ext/object/with_options' require 'active_support/core_ext/object/with_options'
@ -5,16 +7,19 @@ require 'active_support/core_ext/string/inflections'
require 'active_support/json' require 'active_support/json'
module ActiveModelSerializers module ActiveModelSerializers
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
eager_autoload do
autoload :Model autoload :Model
autoload :Callbacks autoload :Callbacks
autoload :Deserialization
autoload :SerializableResource autoload :SerializableResource
autoload :SerializationContext
autoload :Logging autoload :Logging
autoload :Test autoload :Test
autoload :Adapter autoload :Adapter
autoload :JsonPointer autoload :JsonPointer
autoload :Deprecate autoload :Deprecate
autoload :LookupChain autoload :LookupChain
autoload :Deserialization
end
class << self; attr_accessor :logger; end class << self; attr_accessor :logger; end
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
@ -46,8 +51,13 @@ module ActiveModelSerializers
$VERBOSE = original_verbose $VERBOSE = original_verbose
end end
def self.eager_load!
super
ActiveModel::Serializer.eager_load!
end
require 'active_model/serializer/version' require 'active_model/serializer/version'
require 'active_model/serializer' require 'active_model/serializer'
require 'active_model/serializable_resource' require 'active_model/serializable_resource'
require 'active_model_serializers/railtie' if defined?(::Rails) require 'active_model_serializers/railtie' if defined?(::Rails::Railtie)
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
UnknownAdapterError = Class.new(ArgumentError) UnknownAdapterError = Class.new(ArgumentError)
@ -35,7 +37,7 @@ module ActiveModelSerializers
# @return [Array<Symbol>] list of adapter names # @return [Array<Symbol>] list of adapter names
def adapters def adapters
adapter_map.keys.sort adapter_map.keys.sort!
end end
# Adds an adapter 'klass' with 'name' to the 'adapter_map' # Adds an adapter 'klass' with 'name' to the 'adapter_map'

View File

@ -1,6 +1,13 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class Attributes < Base class Attributes < Base
def initialize(*)
super
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
end
def serializable_hash(options = nil) def serializable_hash(options = nil)
options = serialization_options(options) options = serialization_options(options)
options[:fields] ||= instance_options[:fields] options[:fields] ||= instance_options[:fields]
@ -8,6 +15,22 @@ module ActiveModelSerializers
self.class.transform_key_casing!(serialized_hash, instance_options) self.class.transform_key_casing!(serialized_hash, instance_options)
end end
private
def fields_to_fieldset(fields)
return fields if fields.nil?
resource_fields = []
relationship_fields = {}
fields.each do |field|
case field
when Symbol, String then resource_fields << field
when Hash then relationship_fields.merge!(field)
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
end
end
relationship_fields.merge!(serializer.json_key.to_sym => resource_fields)
end
end end
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'case_transform' require 'case_transform'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class Json < Base class Json < Base

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# {http://jsonapi.org/format/ JSON API specification} # {http://jsonapi.org/format/ JSON API specification}
# rubocop:disable Style/AsciiComments # rubocop:disable Style/AsciiComments
# TODO: implement! # TODO: implement!
@ -22,14 +24,16 @@ module ActiveModelSerializers
module Adapter module Adapter
class JsonApi < Base class JsonApi < Base
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
eager_autoload do
autoload :Jsonapi autoload :Jsonapi
autoload :ResourceIdentifier autoload :ResourceIdentifier
autoload :Relationship
autoload :Link autoload :Link
autoload :PaginationLinks autoload :PaginationLinks
autoload :Meta autoload :Meta
autoload :Error autoload :Error
autoload :Deserialization autoload :Deserialization
autoload :Relationship
end
def self.default_key_transform def self.default_key_transform
:dash :dash
@ -480,7 +484,8 @@ module ActiveModelSerializers
# }.reject! {|_,v| v.nil? } # }.reject! {|_,v| v.nil? }
def links_for(serializer) def links_for(serializer)
serializer._links.each_with_object({}) do |(name, value), hash| serializer._links.each_with_object({}) do |(name, value), hash|
result = Link.new(serializer, value).as_json next if value.excluded?(serializer)
result = Link.new(serializer, value.block).as_json
hash[name] = result if result hash[name] = result if result
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
@ -189,7 +191,7 @@ module ActiveModelSerializers
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym) polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
if polymorphic if polymorphic
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'] : nil hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'].classify : nil
end end
hash hash

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi < Base class JsonApi < Base

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi < Base class JsonApi < Base

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi < Base class JsonApi < Base
@ -20,12 +22,13 @@ module ActiveModelSerializers
end end
def as_json def as_json
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size {
pages_from.each_with_object({}) do |(key, value), hash| self: location_url,
params = query_parameters.merge(page: { number: value, size: per_page }).to_query first: first_page_url,
prev: prev_page_url,
hash[key] = "#{url(adapter_options)}?#{params}" next: next_page_url,
end last: last_page_url
}
end end
protected protected
@ -34,25 +37,39 @@ module ActiveModelSerializers
private private
def pages_from def location_url
return {} if collection.total_pages <= FIRST_PAGE url_for_page(collection.current_page)
{}.tap do |pages|
pages[:self] = collection.current_page
unless collection.current_page == FIRST_PAGE
pages[:first] = FIRST_PAGE
pages[:prev] = collection.current_page - FIRST_PAGE
end end
unless collection.current_page == collection.total_pages def first_page_url
pages[:next] = collection.current_page + FIRST_PAGE url_for_page(1)
pages[:last] = collection.total_pages
end end
def last_page_url
if collection.total_pages == 0
url_for_page(FIRST_PAGE)
else
url_for_page(collection.total_pages)
end end
end end
def url(options) def prev_page_url
return nil if collection.current_page == FIRST_PAGE
url_for_page(collection.current_page - FIRST_PAGE)
end
def next_page_url
return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
url_for_page(collection.next_page)
end
def url_for_page(number)
params = query_parameters.dup
params[:page] = { size: per_page, number: number }
"#{url(adapter_options)}?#{params.to_query}"
end
def url(options = {})
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url @url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
end end
@ -63,6 +80,10 @@ module ActiveModelSerializers
def query_parameters def query_parameters
@query_parameters ||= context.query_parameters @query_parameters ||= context.query_parameters
end end
def per_page
@per_page ||= collection.try(:per_page) || collection.try(:limit_value) || collection.size
end
end end
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
@ -43,10 +45,16 @@ module ActiveModelSerializers
end end
def data_for_one(association) def data_for_one(association)
if association.belongs_to? && if belongs_to_id_on_self?(association)
parent_serializer.object.respond_to?(association.reflection.foreign_key) id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
id = parent_serializer.object.send(association.reflection.foreign_key) type =
type = association.reflection.type.to_s if association.polymorphic?
# We can't infer resource type for polymorphic relationships from the serializer.
# We can ONLY know a polymorphic resource type by inspecting each resource.
association.lazy_association.serializer.json_key
else
association.reflection.type.to_s
end
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options) ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
else else
# TODO(BF): Process relationship without evaluating lazy_association # TODO(BF): Process relationship without evaluating lazy_association
@ -86,6 +94,12 @@ module ActiveModelSerializers
meta = association.meta meta = association.meta
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
end end
def belongs_to_id_on_self?(association)
parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship &&
association.belongs_to? &&
parent_serializer.object.respond_to?(association.reflection.foreign_key)
end
end end
end end
end end

View File

@ -1,33 +1,37 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
class ResourceIdentifier class ResourceIdentifier
def self.type_for(class_name, serializer_type = nil, transform_options = {}) def self.type_for(serializer, serializer_type = nil, transform_options = {})
if serializer_type raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
raw_type = serializer_type
else
inflection =
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
:singularize
else
:pluralize
end
raw_type = class_name.underscore
raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
raw_type
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
raw_type
end
JsonApi.send(:transform_key_casing!, raw_type, transform_options) JsonApi.send(:transform_key_casing!, raw_type, transform_options)
end end
def self.for_type_with_id(type, id, options) def self.for_type_with_id(type, id, options)
return nil if id.blank? type = inflect_type(type)
{ type = type_for(:no_class_needed, type, options)
id: id.to_s, if id.blank?
type: type_for(:no_class_needed, type, options) nil
} else
{ id: id.to_s, type: type }
end
end
def self.raw_type_from_serializer_object(object)
class_name = object.class.name # should use model_name
raw_type = class_name.underscore
raw_type = inflect_type(raw_type)
raw_type
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
raw_type
end
def self.inflect_type(type)
singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
inflection = singularize ? :singularize : :pluralize
ActiveSupport::Inflector.public_send(inflection, type)
end end
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects} # {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
@ -37,8 +41,11 @@ module ActiveModelSerializers
end end
def as_json def as_json
return nil if id.blank? if id.blank?
{ id: id, type: type } { type: type }
else
{ id: id.to_s, type: type }
end
end end
protected protected
@ -48,7 +55,8 @@ module ActiveModelSerializers
private private
def type_for(serializer, transform_options) def type_for(serializer, transform_options)
self.class.type_for(serializer.object.class.name, serializer._type, transform_options) serializer_type = serializer._type
self.class.type_for(serializer, serializer_type, transform_options)
end end
def id_for(serializer) def id_for(serializer)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class Null < Base class Null < Base

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Adapted from # Adapted from
# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb # https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
require 'active_support/callbacks' require 'active_support/callbacks'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
## ##
# Provides a single method +deprecate+ to be used to declare when # Provides a single method +deprecate+ to be used to declare when
# something is going away. # something is going away.

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Deserialization module Deserialization
module_function module_function

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module JsonPointer module JsonPointer
module_function module_function

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
## ##
# ActiveModelSerializers::Logging # ActiveModelSerializers::Logging
# #

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module LookupChain module LookupChain
# Standard appending of Serializer to the resource name. # Standard appending of Serializer to the resource name.

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# ActiveModelSerializers::Model is a convenient superclass for making your models # ActiveModelSerializers::Model is a convenient superclass for making your models
# from Plain-Old Ruby Objects (PORO). It also serves as a reference implementation # from Plain-Old Ruby Objects (PORO). It also serves as a reference implementation
# that satisfies ActiveModel::Serializer::Lint::Tests. # that satisfies ActiveModel::Serializer::Lint::Tests.

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails/railtie' require 'rails/railtie'
require 'action_controller' require 'action_controller'
require 'action_controller/railtie' require 'action_controller/railtie'
@ -5,6 +7,8 @@ require 'action_controller/serialization'
module ActiveModelSerializers module ActiveModelSerializers
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
config.eager_load_namespaces << ActiveModelSerializers
config.to_prepare do config.to_prepare do
ActiveModel::Serializer.serializers_cache.clear ActiveModel::Serializer.serializers_cache.clear
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Based on discussion in https://github.com/rails/rails/pull/23712#issuecomment-184977238, # Based on discussion in https://github.com/rails/rails/pull/23712#issuecomment-184977238,
# the JSON API media type will have its own format/renderer. # the JSON API media type will have its own format/renderer.
# #

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'set' require 'set'
module ActiveModelSerializers module ActiveModelSerializers
@ -14,8 +16,8 @@ module ActiveModelSerializers
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash. # @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
def initialize(resource, options = {}) def initialize(resource, options = {})
@resource = resource @resource = resource
@adapter_opts, @serializer_opts = @adapter_opts = options.select { |k, _| ADAPTER_OPTION_KEYS.include? k }
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } @serializer_opts = options.reject { |k, _| ADAPTER_OPTION_KEYS.include? k }
end end
def serialization_scope=(scope) def serialization_scope=(scope)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/extract_options'
module ActiveModelSerializers module ActiveModelSerializers
class SerializationContext class SerializationContext

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Test module Test
extend ActiveSupport::Autoload extend ActiveSupport::Autoload

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ActiveModelSerializers module ActiveModelSerializers
module Test module Test
module Schema module Schema

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'set' require 'set'
module ActiveModelSerializers module ActiveModelSerializers
module Test module Test

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails/generators' require 'rails/generators'
require 'rails/generators/rails/resource/resource_generator' require 'rails/generators/rails/resource/resource_generator'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Rails module Rails
module Generators module Generators
class SerializerGenerator < NamedBase class SerializerGenerator < NamedBase

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# To add Grape support, require 'grape/active_model_serializers' in the base of your Grape endpoints # To add Grape support, require 'grape/active_model_serializers' in the base of your Grape endpoints
# Then add 'include Grape::ActiveModelSerializers' to enable the formatter and helpers # Then add 'include Grape::ActiveModelSerializers' to enable the formatter and helpers
require 'active_model_serializers' require 'active_model_serializers'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# A Grape response formatter that can be used as 'formatter :json, Grape::Formatters::ActiveModelSerializers' # A Grape response formatter that can be used as 'formatter :json, Grape::Formatters::ActiveModelSerializers'
# #
# Serializer options can be passed as a hash from your Grape endpoint using env[:active_model_serializer_options], # Serializer options can be passed as a hash from your Grape endpoint using env[:active_model_serializer_options],

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Helpers can be included in your Grape endpoint as: helpers Grape::Helpers::ActiveModelSerializers # Helpers can be included in your Grape endpoint as: helpers Grape::Helpers::ActiveModelSerializers
module Grape module Grape

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
begin begin
require 'rubocop' require 'rubocop'
require 'rubocop/rake_task' require 'rubocop/rake_task'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController
@ -45,7 +47,7 @@ module ActionController
response = JSON.parse(@response.body) response = JSON.parse(@response.body)
expected = { expected = {
'restriction_for_id' => '67', 'restriction_for_id' => '67',
'restriction_for_type' => 'discounts', 'restriction_for_type' => 'Discount',
'restricted_to_id' => nil, 'restricted_to_id' => nil,
'restricted_to_type' => nil 'restricted_to_type' => nil
} }

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
require 'will_paginate/array' require 'will_paginate/array'
require 'kaminari' require 'kaminari'
@ -58,8 +60,10 @@ module ActionController
assert_equal expected_links, response['links'] assert_equal expected_links, response['links']
end end
def test_render_only_last_and_next_pagination_links def test_render_only_first_last_and_next_pagination_links
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2", expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
'prev' => nil,
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2", 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" } 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } } get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
@ -78,17 +82,21 @@ module ActionController
assert_equal expected_links, response['links'] assert_equal expected_links, response['links']
end end
def test_render_only_prev_and_first_pagination_links def test_render_only_prev_first_and_last_pagination_links
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" } 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'next' => nil,
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } } get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
response = JSON.parse(@response.body) response = JSON.parse(@response.body)
assert_equal expected_links, response['links'] assert_equal expected_links, response['links']
end end
def test_render_only_last_and_next_pagination_links_with_additional_params def test_render_only_first_last_and_next_pagination_links_with_additional_params
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional", expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
'prev' => nil,
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional", 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" } 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' } get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
@ -96,10 +104,12 @@ module ActionController
assert_equal expected_links, response['links'] assert_equal expected_links, response['links']
end end
def test_render_only_prev_and_first_pagination_links_with_additional_params def test_render_only_prev_first_and_last_pagination_links_with_additional_params
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional", expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional", 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" } 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional",
'next' => nil,
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional" }
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' } get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
response = JSON.parse(@response.body) response = JSON.parse(@response.body)
assert_equal expected_links, response['links'] assert_equal expected_links, response['links']

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController
@ -123,7 +125,12 @@ module ActionController
tests Api::V3::LookupTestController tests Api::V3::LookupTestController
setup do setup do
@test_namespace = self.class.parent @test_namespace =
if Module.method_defined?(:module_parent)
self.class.module_parent
else
self.class.parent
end
end end
test 'uses request headers to determine the namespace' do test 'uses request headers to determine the namespace' do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module SerializationScopeTesting module SerializationScopeTesting

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActionController module ActionController
@ -457,13 +459,19 @@ module ActionController
end end
def test_render_event_is_emitted def test_render_event_is_emitted
subscriber = ::ActiveSupport::Notifications.subscribe('render.active_model_serializers') do |name| subscriber = ::ActiveSupport::Notifications.subscribe('render.active_model_serializers') do |subscribed_event|
@name = name @subscribed_event = subscribed_event
end end
get :render_using_implicit_serializer get :render_using_implicit_serializer
assert_equal 'render.active_model_serializers', @name subscribed_event_name =
if @subscribed_event.is_a?(String)
@subscribed_event
else
@subscribed_event.name # is a ActiveSupport::Notifications::Event
end
assert_equal 'render.active_model_serializers', subscribed_event_name
ensure ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModel module ActiveModel

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Execute this test in isolation # Execute this test in isolation
require 'support/isolated_unit' require 'support/isolated_unit'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'support/isolated_unit' require 'support/isolated_unit'
require 'minitest/mock' require 'minitest/mock'
require 'action_dispatch' require 'action_dispatch'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Execute this test in isolation # Execute this test in isolation
require 'support/isolated_unit' require 'support/isolated_unit'
require 'minitest/mock' require 'minitest/mock'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
class ActiveRecordTest < ActiveSupport::TestCase class ActiveRecordTest < ActiveSupport::TestCase

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModelSerializers module ActiveModelSerializers

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
module ActiveModel module ActiveModel
class Serializer class Serializer

Some files were not shown because too many files have changed in this diff Show More