Commit Graph

461 Commits

Author SHA1 Message Date
L. Preston Sego III
7d7329bbcf Merge pull request #1426 from brigade/default-include
Add a default_include_tree config variable to ActiveModel::Serializer
2016-05-26 13:31:53 -04:00
Ben Mills
9cffc10208
Add Rails >= 5.0.beta3 JSON API params parsing (#1751)
This reverts commit 6288203277.
2016-05-26 11:19:23 -06:00
Ben Mills
6288203277 Revert "Add Rails >= 5.0.beta3 JSON API params parsing" (#1751) 2016-05-26 11:17:32 -06:00
Noah Silas
94db09b3f6 Fix RuboCop 0.40 linter errors (#1722)
These errors are breaking the build, which seems to use RuboCop 0.40 [1]
despite the Gemfile.lock pinning rubocop to 0.38.

New lints that I am updating the code style to reflect:

- Style/EmptyCaseCondition: Do not use empty case condition, instead use
  an if expression.

- Style/MultilineArrayBraceLayout: Closing array brace must be on the
  same line as the last array element when opening brace is on the same
  line as the first array element.

- Style/MultilineHashBraceLayout: Closing hash brace must be on the same
  line as the last hash element when opening brace is on the same line
  as the first hash element.

- Style/MultilineMethodCallBraceLayout: Closing method call brace must
  be on the line after the last argument when opening brace is on a
  separate line from the first argument.

[1] https://github.com/bbatsov/rubocop/releases/tag/v0.40.0
2016-05-26 12:58:05 -04:00
Benjamin Fleischer
8a3196d920 Improve jsonapi mime type registration for Rails 5 (#1747) 2016-05-26 10:55:12 -06:00
Ben Woosley
8c18d18cdb Add default_includes configuration
This is useful to set application-wide default behavior - e.g. in
previous versions of AMS the default behavior was to serialize the
full object graph by default - equivalent to the '**' include tree.

Currently just the global setting, but I think this could also work
on a per-serializer basis, with more attention.
2016-05-26 00:16:14 +00:00
Yohan Robert
a701777bd5 Prevent loading association when include_data is set to false (#1710)
This should fix #1707.
2016-05-24 20:46:22 -04:00
cgmckeever
bbed12864d adds polymorphic option to association definition which includes association type in serializer
regen gemlock

regen gemlock

better variable naming

rubocop fixes

adds to changelog

adds empty relationship and has_many polymorph tests

indent

test cleaning

-rubocop

rubocop

rubocop

rubocop

changelog

remove silly .DS

fix roque failure

fix
2016-05-17 12:30:59 -05:00
Benjamin Fleischer
6c321cd862 Assert Schema (#1677)
* Assert Schema

* Fix regression from #1695 where JSONAPI renders empty meta

* Add changelog
2016-05-17 13:22:38 -04:00
Benjamin Fleischer
93cad825b7 Include actual exception message with custom exceptions 2016-05-17 10:32:53 -05:00
Benjamin Fleischer
d0d7af470c Test::Schema exceptions should be Minitest::Assertions 2016-05-17 10:31:35 -05:00
Fumiaki MATSUSHIMA
aa087a22b5 String/Lambda support for conditional attributes/associations 2016-04-26 21:37:25 +09:00
L. Preston Sego III
8c8edd6457 Merge pull request #1700 from iamnader/master
Support pagination link for Kaminari when no data is returned
2016-04-22 13:05:18 -04:00
Nader Akhnoukh
37ca0c1f6c Support pagination link for Kaminari when no data is returned 2016-04-22 11:04:40 -06:00
Benjamin Fleischer
17d0759c97 Merge pull request #1698 from bf4/fix/benchmark
Fix CachingPostSerializer defining associations twice; add FragmentCaching benchmaking
2016-04-21 23:44:54 -05:00
Lucas Hosseini
b4e2ac300c Merge pull request #1695 from beauby/meta-madness
Meta no longer handled in Base adapter.
2016-04-22 01:13:06 +02:00
Lucas Hosseini
e804d37924 Meta no longer handled in Base adapter. 2016-04-21 18:25:17 +02:00
Benjamin Fleischer
335869ec0b Add FragmentCaching benchmark 2016-04-20 10:47:55 -05:00
Benjamin Fleischer
02ad8c26b0 Fix CachingPostSerializer defining associations twice 2016-04-20 10:13:02 -05:00
Lucas Hosseini
ebda34b3d3 Fix tests to comply with the JSON API spec. 2016-04-20 03:08:54 +02:00
Benjamin Fleischer
56662e9f34 Add missing unsubscribe from test 2016-04-18 14:45:27 -05:00
Benjamin Fleischer
e554ba23d2 Nicer debug; compare caching by serializer, grouped by caching on/off
bundle exec bin/bench_regression a5eaf6cd7a7fed42d9e64777753a1762e187eadc 1033b711c7d7c231bb5b832e7dfe7f99389f22c4 --pattern bm_caching

["perf/only_calc_associations_once", "a5eaf6cd7a7fed42d9e64777753a1762e187eadc", "1033b711c7d7c231bb5b832e7dfe7f99389f22c4", "a5eaf6c"]
  "version": "0.10.0.rc5",
  "rails_version": "4.2.6",
  "benchmark_run[environment]": "2.2.2p95",

Note: checking out 'a5eaf6cd7a7fed42d9e64777753a1762e187eadc'.

HEAD is now at a5eaf6c... Nicer debug; compare caching by serializer, grouped by caching on/off

caching on: caching serializers: gc off 783.6956866669746/ips; 1355 objects
caching on: non-caching serializers: gc off 798.8629770532652/ips; 1257 objects
caching off: caching serializers: gc off 682.3661326140281/ips; 1355 objects
caching off: non-caching serializers: gc off 721.2175067555897/ips; 1257 objects

HEAD is now at 1033b71... Merge pull request #1638 from bf4/caching_redux

caching on: caching serializers: gc off 570.6905948477781/ips; 1803 objects
caching on: non-caching serializers: gc off 822.8418206976623/ips; 1257 objects
caching off: caching serializers: gc off 523.4174806572001/ips; 1803 objects
caching off: non-caching serializers: gc off 747.6026493097758/ips; 1257 objects
2016-04-18 11:55:20 -05:00
Benjamin Fleischer
1e10c20ac0 Consolidate and simplify caching code 2016-04-13 01:08:09 -05:00
Benjamin Fleischer
929a5d0a51 Restrict serializable_hash to accepted options, only for tests 2016-04-13 00:40:57 -05:00
Benjamin Fleischer
aad7779a3f Restrict serializable_hash to accepted options (#1647)
Restrict tests/impl from passing AMS options into serializable_hash
2016-04-11 13:10:18 -05:00
L. Preston Sego III
74d172b4f6 Merge pull request #1662 from remear/drop-old-versions
Drop support for Rails 4.0 and Ruby 2.0.0
2016-04-04 18:10:01 -04:00
Benjamin Fleischer
b58f73ced1 Merge branch 'andreaseger-fix_missing_require_of_active_support_json' 2016-04-04 13:54:12 -05:00
Ben Mills
89e0a39fbb Drop support for Rails 4.0 and Ruby 2.0.0 2016-04-04 11:41:35 -06:00
Benjamin Fleischer
6370e5c72a Fix read_attribute_for_serialization not seeing parent serializer methods
Fixes #1653, #1658, #1660

Define "scope_name" on instance singleton, not all instances
2016-04-04 12:29:26 -05:00
Ben Mills
3498647d1a Apply key transforms to keys referenced in values 2016-04-04 10:16:04 -06:00
Andreas Eger
af2b38c43b add require for active_support/json to fix #1656 2016-04-04 09:26:48 +02:00
Benjamin Fleischer
22f88ef379 Merge pull request #1651 from NullVoxPopuli/deserialization-error-with-no-attributes
Fix for Deserialization erroring when a relationship is null in the json api document.
2016-04-03 14:32:24 -05:00
NullVoxPopuli
5be33afbfb Fix deserialization of nil relationships
failing test

use try for when the assoc_data is possibly nil

rubocop test/action_controller/json_api/deserialization_test.rb -a

attempt to work on rails-master

account for rails/master having  instead of nil for assoc_data

added changelog
2016-04-01 16:06:35 -04:00
Benjamin Fleischer
7acbb765bd Merge pull request #1650 from bf4/fix_serialization_scope
[FIX] serialization scope options
2016-04-01 14:17:19 -05:00
Benjamin Fleischer
2edd39d2c2 Need to teardown the dynamically added method 2016-04-01 12:05:44 -05:00
Benjamin Fleischer
a065bc28d1 Fix serialization scope options 2016-04-01 05:43:46 -05:00
Benjamin Fleischer
21b2eff2ab Improvements from Rails plugin template 2016-04-01 05:39:03 -05:00
kevintyll
16a3f93ce9 Include adapter in cache key
Confirm caching attributes with different key json_api vs. attributes adapter

Adapted from @kevintyll's original test
https://github.com/rails-api/active_model_serializers/pull/1644#issuecomment-204147094
2016-04-01 00:52:28 -05:00
Benjamin Fleischer
4ba4c298ec Prefer object.cache_key when available. 2016-03-31 22:29:13 -05:00
kevintyll
ab6bd600e3 When caching, return the object's cache_key up front if it's defined.
This will prevent objects PORO objects that don't have updated_at defined, from throwing an error.

Not as big a deal now that PORO objects can inherit ActiveModelSerializers::Model, but still necessary if it's not inherited for whatever reason.

Add the Adapter type to the cache key.

This prevents incorrect results when the same object is serialized with different adapters.

BF:

Cherry-pick of
040a97b9e9
which was a squash of
f89ed71058

from pr 1346
2016-03-31 22:29:13 -05:00
Ben Mills
b73b780b79 Merge pull request #1629 from lawitschka/key-transform-on-deserialization
Properly deserialize dasherized keys
2016-03-31 13:30:02 -06:00
Benjamin Fleischer
fa7b3afbfd Prefer explicitly yielding the serializer, per groyoh 2016-03-30 14:01:28 -05:00
Benjamin Fleischer
ae6805eacd Add serializer to association block context 2016-03-30 11:03:38 -05:00
Benjamin Fleischer
be9c1bd397 Add CHANGELOG [ci skip] 2016-03-30 10:00:24 -05:00
Yohan Robert
fb62fb39b2 Fix caching issue happening with memory_store
It seems that fecthing from memory_store returns a reference to the
object and not a copy. Since the Attributes adapter applies #merge! on
the Hash that is returned from the memory_store, the value in the cache
is also modified.
2016-03-30 09:53:21 -05:00
Benjamin Fleischer
d50d29b601 Cleaning up Caching Tests 2016-03-30 09:53:21 -05:00
Benjamin Fleischer
cdab6f2b8a The cache store needs to be the actually store, not e.g. :memory_store
Status quo in test app:
In Rails
    ActionController::Base.cache_store = :memory_store
and then AMS railtie does:
  ActiveModelSerializers.config.cache_store = config.action_controller.cache_store
then, in the Railtie
1. ActiveSupport.on_load(:action_controller) fires
  - ActiveModelSerializers.config.cache_store #=> nil
  - ActionController::Base.cache_store        #=> #<ActiveSupport::Cache::FileStore:0x007fe319256760...]
2. After set_configs fires
  - ActiveModelSerializers.config.cache_store #+> #<ActiveSupport::Cache::FileStore:0x007fe319256760 ,
3. Tests pass, but notice that we're using the FileStore, not memory store

When we change the config to the test app:
  ActionController::Base.cache_store = :memory_store
  config = Rails.configuration
  config.action_controller.cache_store = :memory_store
then, in the Railtie:
1. ActiveSupport.on_load(:action_controller) fires
  - ActiveModelSerializers.config.cache_store #=> nil
  - ActionController::Base.cache_store        #=> #ActiveSupport::Cache::MemoryStore entries=0, size=0, options={}>]
2. After set_configs fires
  - ActiveModelSerializers.config.cache_store #=> :memory_store
3. And we get a lot of failures:
  NoMethodError: undefined method `fetch' for :memory_store:Symbol

So, we see that when we set the ActionController::Base.cache_store
directly in our test app, we could set
ActiveModelSerializers.config.cache_store in the :action_controller load
hook, but that would never use the Rails config.

To fix the Rails config, we change the config to the test app:
  config = Rails.configuration
  config.action_controller.cache_store = :memory_store
and then AMS railtie does:
  ActiveModelSerializers.config.cache_store = ActiveSupport::Cache.lookup_store(config.action_controller.cache_store
  ActiveSupport.on_load(:action_controller) do
    ::ActiveModelSerializers.config.cache_store = cache_store
  end
then
1. After set_configs fires
  - ActiveModelSerializers.config.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207113611740
2. ActiveSupport.on_load(:action_controller) fires
  - ActionController::Base.cache_store        #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207106279660
  - ActiveModelSerializers.config.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207106279660
    (notice the object_id changed)
3. And we get a failure:

  1) Failure:
  ActiveModelSerializers::CacheTest#test_associations_cache_when_updated
  [active_model_serializers/test/cache_test.rb:141]:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -{:id=>"post", :title=>"New Post", :body=>"Body"}
  +{:id=>"post", :title=>"New Post", :body=>"Body", :comments=>[{:id=>2, :body=>"ZOMG A NEW COMMENT"}], :blog=>{:id=>999, :name=>"Custom blog"}, :author=>{:id=>"author", :name=>"Joao M. D. Moura"}}

If we take out the on_load(:action_controller) hook, we get a ton of
failures.  So clearly, our code expects the controller cache to be the
same as the serializer cache.

So, we make sure we use an on_load(:action_controller) hook that runs
after set_configs

And look at the test and see it is filled with direct calls to ActionController::Base.cache_store

    assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
    assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))

But that's not a problem in this case, since they're the same object.

For now, let's remove the :memory_store setting and use the default FileStore
2016-03-30 09:52:29 -05:00
Yohan Robert
21cb896802 Move SerializableResource to ActiveModelSerializers namespace
Ref. https://github.com/rails-api/active_model_serializers/pull/1310
2016-03-30 11:33:04 +02:00
Benjamin Fleischer
ff8c6f9dd4 Clean up test app 2016-03-30 00:23:04 -05:00
Benjamin Fleischer
355e0f6a37 Merge branch 'lserman-master'
Followup needed:
- Update code comments https://github.com/rails-api/active_model_serializers/pull/1622#discussion_r57750471
- Move test class into test scope https://github.com/rails-api/active_model_serializers/pull/1622#discussion_r57659150
2016-03-29 21:50:27 -05:00