NoMethodError is current_user is nil, so nil.admin?
NameError is a superclass of NoMethodError (which Rails 4.0 won't allow)
and means current_user might not be defined
- Setup dummy app files in `test/dummy`
- Setup dummy test server `bin/serve_dummy
- Note: Serializer caching can be completely disabled by passing in
`CACHE_ON=off bin/serve_dummy start` since Serializer#_cache is only
set at boot.
- run with
- ./bin/bench
- `bin/bench` etc adapted from ruby-bench-suite
- target files are `test/dummy/bm_*.rb`. Just add another to run it.
- benchmark cache/no cache
- remove rake dependency that loads unnecessary files
- remove git gem dependency
- Running over revisions to be added in subsequent PR
Adding a benchmak test structure to help contributors to keep track
of how their PR will impact overall performance.
It enables developers to create test inside of tests/benchmark.
This implementation adds a rake task: ```rake benchmark``` that checkout
one commit before, run the test of tests/benchmark, then mover back to
the last commit and run it again. By comparing the benchmark results between
both commits the contributor will notice if and how much his contribution
will impact overall performance.
- The removed classes and modules were added back with deprecation
warning and deprecation test were added for them.
- One test was renamed because it contained `__`.
- Some tests were refactored.
- The ActiveModelSerializers::Deserialization module is now called
Adapter instead of ActiveModelSerializers::Adapter.
- The changelog was added for #1535
Idea per remear (Ben Mills) in the slack:
https://amserializers.slack.com/archives/general/p1455140474000171
remear:
just so i understand, the adapter in `render json: resource, status: 422, adapter: 'json_api/error',
serializer: ActiveModel::Serializer::ErrorSerializer` is a different one than, say what i’ve
specified in a base serializer with `ActiveModel::Serializer.config.adapter = :json_api`. correct?
and a followup question of, why not same adapter but different serializer?
me:
With the way the code is written now, it might be possible to not require a special jsonapi adapter.
However, the behavior is pretty different from the jsonapi adapter.
this first draft of the PR had it automatically set the adapter if there were errors. since that
requires more discussion, I took a step back and made it explicit for this PR
If I were to re-use the json api adapter and remove the error one, it think the serializable hash
method would look like
```
def serializable_hash(options = nil)
return { errors: JsonApi::Error.collection_errors } if serializer.is_a?(ErrorsSerializer)
return { errors: JsonApi::Error.resource_errors(serializer) } if serializer.is_a?(ErrorSerializer)
options ||= {}
```
I suppose it could be something more duckish like
```
def serializable_hash(options = nil)
if serializer.errors? # object.errors.any? || object.any? {|o| o.errors.any? }
JsonApi::Error.new(serializer).serializable_hash
else
# etc
```
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
The ActiveModel::Serializer.type method now accepts symbol as paremeter:
class AuthorSerializer < ActiveModel::Serializer
type :profile
end
The test file for the type was also refactored.
Fix the "Calling deprecated ArraySerializer... Please use
CollectionSerializer" warning that appeared when running the specs. This
happened because on of the test called ArraySerializer instead of
CollectionSerializer.
When using the relationship link with a block, calling "meta" without "href", e.g.
has_one :bio do
link :related do
meta id: 1
end
end
results in in a nil "href", e.g.
{ links: { posts: { related: { href: nil, meta: { id: 1 } } } } }.
According to JSONAPI, we should be able to use meta without href
(http://jsonapi.org/format/#document-links).
When using the relationships DSL with a block e.g.
has_one :bio do
link :self, "some_link"
end
the "data" field would be rendered with a nil value even though the bio
is not nil. This happened because the block return value was set to nil
but used as a value for the "data" field.
PR #1454 was merged with some missing fixes. All these fixes are
addressed by this commit:
- Rename ActiveModel::Serializer::Adapter::JsonApi::Association to
ActiveModel::Serializer::Adapter::JsonApi::Relationship
- Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to
ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module
- Add unit test for
ActiveModel::Serializer::Adapter::JsonApi::Relationship
- Add unit test for
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
We were not previously cloning the type setting into the dynamically
generated cached/non-cached serializers for a given fragment-cached
serializer. This led to the type generated for JsonApi having the wrong
value when fragment caching is enabled by adding either :except or :only
options to cache.
This pulls the type setting from the fragment-cached serializer forward
onto the dynamic caching classes so it is preserved in the output.
http://jsonapi.org/format/#document-top-level
fix failing tests
support for top-level links limited to jsonapi adapter
Move docs from README to docs/ dir
move links to json-api adapter & create Links class to hold links data
this uses the configuration settings rather than calling ActionController::Base to get the configured values.
after the "action_controller.set_configs" initializer has run, the configuration option holds the value Base will get when it loads.
- use hook_for to hook in the serializer and remove load_generators
- move generators so they can be found by rails
- move to_prepare block to railtie config
This commit improves the way the generators are loaded and how
they extend the resource generator.
* The initializer block has been changed to a `generator` block which is only executed when generators are needed.
* The call to `app.load_generators` has been removed. There is no need to load *all* generators.
* The `resource_override.rb` has been changed to use `hook_for` to extend the resource generator.
* The directory for the generators has been moved to match the way Rails looks to load generators.
With `hook_for` it would now be possible for a user to pass `--no-serializer` to skip that option.
The `--serialize` option also now shows up in the generator help with `rails g resource --help`.
These changes follow the way the Draper gem extends the `controller` generator.
Rails 5 removed this assertion after considering it not
a good testing practice. https://github.com/rails/rails/issues/18950
Rather that add a gem to our Rails 5 matrix to support it,
the assertion is made that the template is rendering using
active support notifications.
Also, to clarify that the action 'render_template' is unrelated to the
event name '!render_template.action_view', I renamed the actions
so that would not look like event names.
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
```
- 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