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
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
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
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.
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
The JSON API adapater dasherizes every key, but the deserializer left the keys
unaltered. Thus, the client had to send underscored keys in the request body in
order for Rails to properly match sent values to model attributes.
This commit adds automatic key transformation on deserialization. Per default the
deserializer transforms the keys to underscore, but this behaviour can also be
changed by including `key_transform` in the deserializer options.