In 0.9 (which this implementation is based on), the instrumentation
was `!serialize.active_model_serializers`.
https://github.com/rails-api/active_model_serializers/pull/596/
The '!' in the event name meant the event wasn't meant for
production.
https://github.com/rails/rails/pull/10446/files#r4075679
Since we intend the event for production and have a log subscriber,
if we unsubscribe from `render.active_model_serializers`, we'll
break other tests that are relying on that event being subscribed.
The `assert_serializer` test helper was added in 0.9.0.apha1[1],
and was not included in 0.10.
This patch brings back the `assert_serializer` test helper. This is the last
revision[2] that has the helper. The original helper was used as base.
[1]: https://github.com/rails-api/active_model_serializers/pull/596
[2]: 610aeb2e92
- Create the AssertSerializer
- Use the Test namespace
- Make the tests pass on the Rails master
- Rails 5 does not include `assert_template` but we need this on the tests of
the helper.
- This add the `rails-controller-testing` to keep support on `assert_template`.
- Only load test helpers in the test environment
One of three constituents is used to provide the
CollectionSerializer's #json_key:
1) the :root option - controlled by the caller
2) the #name of the first resource serializer - the root or
underscored model name
3) the underscored #name of the resources object - generally
equivalent to the underscored model name of #2
Of the three, only the latter 2 are out of the callers control, and
only the latter two are expected to be singular by default. Not
pluralizing the root gives the caller additional flexibility in
defining the desired root, whether conventionally plural,
unconventionally plural (e.g. objects_received:) or singular.
* 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
```
The ActiveModelSerializers.silence_warnings was used to avoid warnings on the
Ruby interpreter when define a private attr_acessor. This method is not used in
any part of the code and the recommend way to handle this case is to use
protected instead the silence_warnings [1].
This patch remove the method from the project, this way we avoid people using
this by mistake.
[1]: https://bugs.ruby-lang.org/issues/10967
- adds handling for when the returned resource is not serializable via ams
- fix for when resource is an Array
- Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
- Add integration test
- Refine scope of Grape version dependency
- Assert that the response is equal to a manually defined JSON string
- Add single module to include in Grape projects
- Create a Serializable Resource to test rails-api from Grape
- Update docs
- Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
- Updated Changelog
- Remove parens from `render`, use `serializable` in all tests.
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.
Also
- Add reference to config from ActiveModelSerializers.config
- correctly call super in FragmentCacheTest#setup
- rename test rails app from Foo to ActiveModelSerializers::RailsApplication
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
Breaking change:
- Adapters now inherit Adapter::Base
- 'Adapter' is now a module, no longer a class
Why?
- using a class as a namespace that you also inherit from is complicated and circular at time i.e.
buggy (see https://github.com/rails-api/active_model_serializers/pull/1177)
- The class methods on Adapter aren't necessarily related to the instance methods, they're more
Adapter functions
- named `Base` because it's a Rails-ism
- It helps to isolate and highlight what the Adapter interface actually is
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.
Changes:
- Introduce Adapter::get for use by Serializer.adapter
- Move Adapter-finding logic from Adapter::adapter_class into Adapter::get
Introduced interfaces:
- non-inherited methods
```ruby
ActiveModel::Serializer::Adapter.adapter_map # a Hash<adapter_name, adapter_class>
ActiveModel::Serializer::Adapter.adapters # an Array<adapter_name>
ActiveModel::Serializer::Adapter.register(name, klass) # adds an adapter to the adapter_map
ActiveModel::Serializer::Adapter.get(name_or_klass) # raises Argument error when adapter not found
```
- Automatically register adapters when subclassing
```ruby
def self.inherited(subclass)
ActiveModel::Serializer::Adapter.register(subclass.to_s.demodulize, subclass)
end
```
- Preserves subclass method `::adapter_class(adapter)`
```ruby
def self.adapter_class(adapter)
ActiveModel::Serializer::Adapter.get(adapter)
end
```
- Serializer.adapter now uses `Adapter.get(config.adapter)` rather than have duplicate logic
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.
So we will got full file path instead of only c if caller.first is: c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'
CALLER_FILE = /
/A # start of string
\S+ # one or more non-spaces
(?= # stop previous match when
:\d+:in # a colon is followed by one or more digits
# followed by a colon followed by in
)
/x
credit from https://gist.github.com/mikezter/540132 and @bf4
Due to the fact that users need to switch from the released version to `master` occasionally to pull in upstream bugfixes, it's important that this version number stays in sync with the released version.
* Move all associations related code from Serializer class to Associations module
* Introduce Reflection class hierarchy
* Introduce Association class
* Rid off Serializer#each_association
* Introduce Serializer#associations enumerator
When the resource is a zero result query,
i.e. post_comments = PostComment.where("1=0")
the json_key will become 'postcomments' rather than 'post_comments'.
Using 'underscore' instead of 'downcase' fixes the error.
per ActiveModel::Serialization#serializable_hash
96bb004fc6/activemodel/lib/active_model/serialization.rb
def serializable_hash(options = nil)
options ||= {}
Otherwise, passing in nil to `as_json` or `serializable_hash`
makes things blow up when passing nil into attributes