Squashed commits:
Add Logging
Generates logging when renders a serializer.
Tunning performance on notify_active_support
- Use yield over block.call
- Freeze the event name string
Organize the logger architeture
* Keep only the `ActiveModel::Serializer.logger` to follow the same public API we
have for example to config, like `ActiveModel::Serializer.config.adapter` and
remove the `ActiveModelSerializers.logger` API.
* Define the logger on the load of the AMS, following the Rails convention on
Railties [1], [2] and [3].
This way on non Rails apps we have a default logger and on Rails apps we will
use the `Rails.logger` the same way that Active Job do [4].
[1]: 2ad9afe4ff/activejob/lib/active_job/railtie.rb (L9-L11)
[2]: 2ad9afe4ff/activerecord/lib/active_record/railtie.rb (L75-L77)
[3]: 2ad9afe4ff/actionview/lib/action_view/railtie.rb (L19-L21)
[4]: 2ad9afe4ff/activejob/lib/active_job/logging.rb (L10-L11)
Performance tunning on LogSubscriber#render
Move the definition of locals to inside the `info` block this way the code is
executed only when the logger is called.
Remove not needed check on SerializableResource
Use SerializableResource on ActionController integration
On the ActionController was using a adapter, and since the instrumentation is
made on the SerializableResource we need to use the SerializableResource over
the adapter directly. Otherwise the logger is not called on a Rails app.
Use SerializableResource on the ActionController, since this is the main
interface to create and call a serializer.
Using always the SerializableResource we can keep the adapter code more easy to
mantain since no Adapter will need to call the instrumentation, only the
SerializableResource care about this.
Add docs about logging
Add a CHANGELOG entry
Keep the ActiveModelSerializers.logger
Better wording on Logging docs
[ci skip]
Add doc about instrumentation
[ci skip]
Use ActiveModel::Callbacks on the SerializableResource
I was seeing transient failures where adapters may not be registered.
e.g. https://travis-ci.org/rails-api/active_model_serializers/builds/77735382
Since we're using the Adapter, JsonApi, and Json classes
as namespaces, some of the conventions we use for modules don't apply.
Basically, we don't want to define the class anywhere besides itself.
Otherwise, the inherited hooks may not run, and some adapters may not
be registered.
For example:
If we have a class Api `class Api; end`
And Api is also used as a namespace for `Api::Product`
And the classes are defined in different files.
In one file:
```ruby
class Api
autoload :Product
def self.inherited(subclass)
puts
p [:inherited, subclass.name]
puts
end
end
```
And in another:
```ruby
class Api
class Product < Api
def sell_sell_sell!
# TODO: sell
end
end
end
```
If we load the Api class file first, the inherited hook will be defined on the class
so that when we load the Api::Product class, we'll see the output:
```plain
[ :inherited, Api::Product]
```
However, if we load the Api::Product class first, since it defines the `Api` class
and then inherited from it, the Api file was never loaded, the hook never defined,
and thus never run.
By defining the class as `class Api::Product < Api` We ensure the the Api class
MUST be defined, and thus, the hook will be defined and run and so sunshine and unicorns.
Appendix:
The below would work, but triggers a circular reference warning.
It's also not recommended to mix require with autoload.
```ruby
require 'api'
class Api
class Product < Api
def sell_sell_sell!
# TODO: sell
end
end
end
```
This failure scenario was introduced by removing the circular reference warnings in
https://github.com/rails-api/active_model_serializers/pull/1067
Style note:
To make diffs on the adapters smalleer and easier to read, I've maintained the same
identention that was in the original file. I've decided to prefer ease of reading
the diff over style, esp. since we may later return to the preferred class declaration style.
with '#' will be ignored, and an empty message aborts the commit.
Pagination links will be included in your response automatically as long
as the resource is paginated using Kaminari or WillPaginate
and if you are using a JSON-API adapter. The others adapters does not have this feature.
A number of test were defining and using the same controller
MyController = Class.new(ActionController::Base)
which was causing some state to leak across tests.
Fixes#870
Commit af81a40 introduced passing a serializer's 'options'
along to its associated model serializers.
Thus, an explicit 'each_serializer' passed to render for a
singular resource would be passed on as the implicit 'serializer'
for its associations.
With @bf4
It's an upgrade based on the new Cache implementation #693.
It allows to use the Rails conventions to cache
specific attributes or associations.
It's based on the Cache Composition implementation.