* Make assocations asserts easier to understand
* Refactor Association into Field like everything else
* Make assocation serializer/links/meta lazier
* Push association deeper into relationship
* Simplify association usage in relationships
* Better naming of reflection parent serializer
* Easier to read association method
* replace reflection collection type with hash to prevent duplicated associations in some cases
* include tests
* Fix robucup offenses
* Improve test
* Remove usless requirement
* improve tests
* remove custom_options option from Post and InheritedPost serializer
* Improve tests
* update changelog
* update changelog
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
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
Changed the namespace in adapters and folder to active_model_serializers from active_model::serializer
Changed namespace of adapters in serializers and other folders
Moved adapter_for_test file to active_model_serializers folder and changed namespace of adapter inside the test file
Require ActiveSupport's string/inflections
We depend on string/inflections to define String#underscore.
Refactor JsonApi adapter to avoid redundant computations.
Update readme.md to link to v0.10.0.rc4
changed namespace of adapter folder testcases
Changed all namespaces of adapter under active_moder_serializers
Namespaced IncludeTree which is from serializer module, so needed to namespace it properly
Fixed wrong namsepacing of fieldset
namespace change in deserializer json_api
Fixed the namespace for collection serializer when used inside adapter, changed namespace for adapter to new namespace which I had forgotten previously
Modified logging test and adapter test cases to make the testcases pass
Changed the yardoc links,as old links are not taking to documentation pages,proper links for 0.10,0.9 and 0.8 in rubydoc
Rubocop errors are fixed by underscore naming unused variables
Moved the require of adapter to serializable resource
Remoeved adapter dependency inside serializer and added warning to Serializer::adapter method
Fixed frament cache test which is calling Serializer.adapter
Changed the name of lookup_adapter_from_config to configured_adapter
Changed the docs which will show the new namespace of adapters
Rubocop fix
* Use assert_nil where appropriate
* Lead with the expected value in collection_serializer_test.rb, etc
so that expected/actual in test failure messages are not reversed
For discussion:
Consider evaluating association in serializer context
That way, associations are really just anything that
can be conditionally included. They no longer
have to actually be methods on the object or serializer.
e.g.
```diff
has_many :comments do
- last(1)
+ Comment.active.for_serialization(object).last(1)
end
```
As an example, all serializers implement `#object` as a reference to the
object being esrialized, but this was preventing adding a key to the
serialized representation with the `object` name.
Instead of having attributes directly map to methods on the serializer,
we introduce one layer of abstraction: the `_attributes_map`. This hash
maps the key names expected in the output to the names of the
implementing methods.
This simplifies some things (removing the need to maintain both
`_attributes` and `_attribute_keys`), but does add some complexity in
order to support overriding attributes by defining methods on the
serializer. It seems that with the addition of the inline-block format,
we may want to remove the usage of programatically defining methods on
the serializer for this kind of customization.