Commit Graph

135 Commits

Author SHA1 Message Date
Benjamin Fleischer
25c9df0b97 Merge branch 'master' into domitian-move-namespace-of-adapter-to-active-model-serializers
Conflicts:
	CHANGELOG.md
	lib/active_model/serializer/adapter/attributes.rb
	lib/active_model/serializer/adapter/cached_serializer.rb
	lib/active_model/serializer/adapter/fragment_cache.rb
	lib/active_model/serializer/adapter/json_api.rb
	lib/active_model/serializer/adapter/json_api/link.rb
	test/adapter/fragment_cache_test.rb
	test/adapter/json_api/links_test.rb
	test/adapter/json_api/resource_type_config_test.rb
2016-02-23 23:21:49 -06:00
Leandro Cesquini Pereira
37e4f1c30c Update top-level link with PR #1247
update according rubocop rules
2016-02-03 10:16:22 -02:00
Leandro Cesquini Pereira
1844c162f1 Adds support for top-level links to JsonApi adapter
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
2016-02-03 10:16:22 -02:00
Alexandre de Oliveira
ef58efdf73 Merge pull request #1418 from brigade/collection-pluralize
Don't pluralize the CollectionSerializer#root for #json_key
2016-01-25 19:46:47 -02:00
Benjamin Fleischer
14a62a2405 Fix Rails 5 warnings (uses Rails5Shim) 2016-01-13 21:35:45 -06:00
Lucas Hosseini
20a58d7f5c Add support for JSON API deserialization (experimental). 2016-01-13 05:48:06 +01:00
Ben Woosley
251e33a0a1 Don't pluralize the CollectionSerializer#root for #json_key
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.
2016-01-12 09:32:43 -08:00
Benjamin Fleischer
6588dee22c Rails5 requires x_action 2015-11-25 22:06:20 -06:00
Benjamin Fleischer
c56d49f26b Test keyword args in requests for Rails 5; fallback for earlier versions
kwargs: params,session,flash,method,body

Fix Rails 5 DEPRECATION WARNING
2015-11-25 21:56:02 -06:00
Mauro George
51424963da ActiveSupport::Notifications render.active_model_serializers
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
2015-11-10 03:09:24 -06:00
Benjamin Fleischer
124faaa829 Add PORO serializable base class: ActiveModelSerializers::Model 2015-10-14 21:33:02 -05:00
Benjamin Fleischer
dc44b136aa Remove unnecessarily silenced warnings 2015-10-07 05:54:57 -05:00
Liam Bowen
f8323fc9e5 Fixes #1211 - retrieve the key from the reflection options when building associations 2015-10-02 08:52:21 -04:00
Lucas Hosseini
606e2ae337 Merge pull request #1127 from NullVoxPopuli/support-nested-associations-for-json-adapter
Support nested associations for Json and Attributes adapters + Refactor Attributes adapter
2015-09-21 17:20:29 +02:00
L. Preston Sego III
a74ea189cd Refactors of the Attribute adapter. Adds support for nested associations specified from the include key in the controller. Adds some tests and some method documentation 2015-09-21 10:35:47 -04:00
Benjamin Fleischer
ca6b193fcb Enforce Rails-style (line-count-based) block style 2015-09-21 00:00:53 -05:00
Lucas Hosseini
ac06013aeb Add support for wildcard includes + improve perfs on JsonApi includes. 2015-09-21 05:58:00 +02:00
Benjamin Fleischer
3f0794bd39 Merge pull request #1168 from bf4/fix_appveyor
Fix appveyor failure cache not being expired
2015-09-17 10:05:40 -05:00
Benjamin Fleischer
c43e8e2a32 Fix or skip appveyor failure on cache expiration 2015-09-17 09:30:29 -05:00
Benjamin Fleischer
26277ea1f9 Remove duplicate test helper 2015-09-16 08:54:33 -05:00
Lucas Hosseini
ce7a839f3d Extended format for JSONAPI include option. 2015-09-13 22:45:47 +02:00
Benjamin Fleischer
af99c0d9e6 Ensure inheritance hooks run
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.
2015-09-09 08:55:20 -05:00
Lucas Hosseini
ebb05959d3 Merge remote-tracking branch 'upstream/master' into improve-tests 2015-09-07 09:03:17 +02:00
Benjamin Fleischer
228cc1c92a Rubocop: Consistent spacing 2015-09-03 20:51:40 -05:00
Benjamin Fleischer
bdfe13c527 Style/StringLiterals single quote all the things 2015-09-03 20:50:45 -05:00
Benjamin Fleischer
09c97de90d Add Style enforcer (via Rubocop)
It will fail the build, but as it is currently,
most of the cops are 'todos'. Great for new contributors.. :)
2015-09-03 20:50:45 -05:00
Lucas Hosseini
4bba16bf4e Factor with_adapter + force cache clear before each test. 2015-08-28 17:00:24 +02:00
Benjamin Fleischer
d315151e8a Fix warnings
JRuby-specific: fix 'warning: (...) interpreted as grouped expression'
2015-08-26 09:22:02 -05:00
Bruno Bacarini
5031eb9f96 add test to prev and first with additional params 2015-08-18 19:04:04 -03:00
Bruno Bacarini
2c2f948fa0 Add pagination links automatically
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.
2015-08-18 19:04:04 -03:00
Bruno Bacarini
a41d90cce4 add self to pagination links 2015-08-18 19:04:04 -03:00
Bruno Bacarini
7be25fef14 include query_parameters on pagination links as well 2015-08-18 19:04:03 -03:00
Bruno Bacarini
e62a7d6f34 return complete URIs on pagination links 2015-08-18 19:04:03 -03:00
Bruno Bacarini
e040d6fcce add action test to pagination links 2015-08-18 19:04:02 -03:00
João M. D. Moura
88eabdf6ac fixing tests by using a Profile intance to avoid unrelated warning 2015-07-31 03:28:50 -03:00
Benjamin Fleischer
df140293d3 Remove unused controller rescue_with_handler
Per
https://github.com/rails-api/active_model_serializers/pull/954#discussion_r32589882

Ref 917, 918
2015-07-23 11:06:11 -05:00
Benjamin Fleischer
a66df3009a Encapsulate serialization in ActiveModel::SerializableResource
Usage: ActiveModel::SerializableResource.serialize(resource, options)
2015-07-23 11:05:23 -05:00
vyrak bunleang
1d31096600 include old implicit serialization custom root tests with failing empty array test 2015-07-16 15:32:09 -06:00
Rodrigo Ra
7412c8d1c8 Fix transient tests failures 2015-06-26 09:33:53 -03:00
João Moura
d589268f95 adding new tests to cover array and object rendering 2015-06-26 02:15:42 -03:00
Aaron Lerch
6892ca39c9 Default the generated cache key to use custom #strftime instead of raw #to_s to achieve more accurate precision 2015-06-25 23:40:18 -04:00
João Moura
f67fd976ec Removing/Updating tests based on new FlattenJson adapter 2015-06-18 19:18:11 -03:00
João Moura
329691276a disabling custom root option 2015-06-15 15:58:39 -03:00
João Moura
1ea5608e78 updating tests to match new adapters structure 2015-06-15 13:39:36 -03:00
Benjamin Fleischer
14439aada4 Use model that doesn't fail with race condition
For some reason, the post would sometimes be serialized as

  "{\"id\":\"1\",
+ \"type\":\"posts\", \"attributes\":{\"title\":\"New Post\",\"body\":\"Body\"},
  \"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}],
  \"blog\":{\"id\":999,\"name\":\"Custom blog\"},
  \"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}"

instead of:

  "{\"id\":1,
-  \"title\":\"New Post\",\"body\":\"Body\",
   \"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}],
   \"blog\":{\"id\":999,\"name\":\"Custom blog\"},\
   "author\":{\"id\":1,\"name\":\"Joao Moura.\"}}"

To reproduce prior to this PR:
SEED=55284 rake
  1) Failure:
  ActionController::Serialization::ExplicitSerializerTest#test_render_using_explicit_each_serializer
  [active_model_serializers/test/action_controller/explicit_serializer_test.rb:139]:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -"{\"id\":1,\"title\":\"New
  Post\",\"body\":\"Body\",\"comments\":[{\"id\":1,\"body\":\"ZOMG A
  COMMENT\"}],\"blog\":{\"id\":999,\"name\":\"Custom
  blog\"},\"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}"
  +"{\"id\":\"1\",\"type\":\"posts\",\"attributes\":{\"title\":\"New
  Post\",\"body\":\"Body\"},\"comments\":[{\"id\":1,\"body\":\"ZOMG A
  COMMENT\"}],\"blog\":{\"id\":999,\"name\":\"Custom
  blog\"},\"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}"

  137 runs, 211 assertions, 1 failures, 0 errors, 0 skips
  rake aborted!
  Command failed with status (1): [ruby -I"lib:test"
  -r./test/test_helper.rb
  "/$HOME/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_test_loader.rb"
  "test/action_controller/adapter_selector_test.rb"
  "test/action_controller/explicit_serializer_test.rb"
  "test/action_controller/json_api_linked_test.rb"
  "test/action_controller/rescue_from_test.rb"
  "test/action_controller/serialization_scope_name_test.rb"
  "test/action_controller/serialization_test.rb"
  "test/adapter/fragment_cache_test.rb"
  "test/adapter/json/belongs_to_test.rb"
  "test/adapter/json/collection_test.rb"
  "test/adapter/json/has_many_test.rb"
  "test/adapter/json_api/belongs_to_test.rb"
  "test/adapter/json_api/collection_test.rb"
  "test/adapter/json_api/has_many_embed_ids_test.rb"
  "test/adapter/json_api/has_many_explicit_serializer_test.rb"
  "test/adapter/json_api/has_many_test.rb"
  "test/adapter/json_api/has_one_test.rb"
  "test/adapter/json_api/linked_test.rb" "test/adapter/json_test.rb"
  "test/adapter/null_test.rb" "test/adapter_test.rb"
  "test/array_serializer_test.rb" "test/serializers/adapter_for_test.rb"
  "test/serializers/associations_test.rb"
  "test/serializers/attribute_test.rb"
  "test/serializers/attributes_test.rb" "test/serializers/cache_test.rb"
  "test/serializers/configuration_test.rb"
  "test/serializers/fieldset_test.rb"
  "test/serializers/generators_test.rb" "test/serializers/meta_test.rb"
  "test/serializers/options_test.rb"
  "test/serializers/serializer_for_test.rb"
  "test/serializers/urls_test.rb" ]
  /$HOME/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in
  `eval'
  /$HOME/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in
  `<main>'
  Tasks: TOP => default => test
  (See full trace by running task with --trace)
2015-06-12 11:27:28 -05:00
Benjamin Fleischer
a5554e0d9f Use a different controller in different tests
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.
2015-06-12 11:27:20 -05:00
Edward Loveall
0f0ef2baf5 Don't pass serializer option to associated serializers
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
2015-06-10 18:20:34 -04:00
Chris Branson
d34bba07b9 Ensure the adapters honor a custom root option and include meta when required 2015-06-04 17:33:41 +01:00
João Moura
a40df8fd3d reverting PR #909 and adding json api usage advise on readme 2015-05-27 18:35:00 -03:00
Benedikt Deicke
4f576a1463 Adjusts JsonApi adapter to serialize relationships in a nested relationships hash 2015-05-21 16:35:35 +02:00
Benedikt Deicke
ca41901fb8 Adjusts JsonApi adapter to serialize attributes in a nested attributes hash 2015-05-21 16:23:01 +02:00
João Moura
5f05944826 Merge pull request #918 from aceofsales/rescue_from
Adding rescue_with_handler to clear state
2015-05-20 23:39:45 -03:00
Ryan Schlesinger
9355416ad0 Add rescue_from handler to clear state
Fixes #917
2015-05-19 17:23:29 -07:00
João Moura
9b502a4ae0 changing tests name to support new default adapter 2015-05-11 16:23:38 -03:00
João Moura
fc6276cab8 Merge pull request #888 from groyoh/fix-action-controller-test-name
Changed duplicated test name in action controller test
2015-04-28 12:09:02 -03:00
groyoh
1ef2badb32 Changed duplicated test name in action controller test 2015-04-27 21:22:28 +02:00
João Moura
49e41cb83c adding tests order config 2015-04-27 15:45:49 -03:00
João Moura
792fb8a905 Adding Fragment Cache to AMS
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.
2015-04-05 18:19:57 -03:00
Mateo Murphy
9480b567ef Refactor TestUser in SerializationScopeNameTest
Use the same base class we use for other test models
2015-03-23 13:44:30 -04:00
Mateo Murphy
33f3a88ba0 Implement included and id and type as per spec 2015-03-22 20:43:56 -04:00
Mateo Murphy
d82c599c68 Always use plural for linked types
Although spec is agnostic about inflection rules, examples given are plural
2015-03-22 20:43:11 -04:00
Mateo Murphy
0f55f21266 Update format of links 2015-03-22 20:41:20 -04:00
Mateo Murphy
da86747a3e Use symbol for root in jsonapi, fix tests 2015-03-22 20:38:29 -04:00
Guillermo Iguaran
b68d7f4826 Merge pull request #852 from mateomurphy/serializer-options-fix
Fix options merge order in `each_association`
2015-03-22 18:54:07 -05:00
Mateo Murphy
03372ea61d Fix options merge order in each_association
Custom association serializers were getting clobbered when using an each serializer.
2015-03-20 00:22:46 -04:00
Mateo Murphy
ca985e1afd Use association value for determining serializer used
Ensures overridden association value works when orignal association does not return a result.
2015-03-19 23:28:00 -04:00
Robbie Pitts
ad5677c4ec Make json api adapter 'include' option accept an array, accommodate comma delimited string for legacy reasons 2015-03-11 10:01:44 -04:00
Mateo Murphy
232e367560 Add serialization_scope_name_test 2015-02-12 17:48:43 -05:00
Joao Moura
8a432ad2b3 Adding cache support to version 0.10.0
It's a new implementation of cache based on ActiveSupport::Cache.
The implementation abstracts the cache in Adapter class on a
private method called cached_object, this method is intended
to be used on Adapters inside serializable_hash method in order
to cache each instance of the object that will be returned by
the serializer.

Some of its features are:
- A different syntax. (no longer need the cache_key method).
- An options argument that have the same arguments of ActiveSupport::Cache::Store, plus a key option that will be the prefix of the object cache on a pattern "#{key}-#{object.id}".
- It cache the objects individually and not the whole Serializer return, re-using it in different requests (as a show and a index method for example.)
2015-02-02 14:53:34 -02:00
Alexandre de Oliveira
e47231cdc8 Support overriding association methods
You can override associations to define custom scope on them.
2015-01-29 16:52:18 -02:00
Nicolás Hock Isaza
1d7d9fd6aa Fix nested include attributes
When the requests asked for a nested attribute in the `include` and it's
missing or empty, don't break because the type of the object can't be
determined.

If the request is for a collection and none of the elements has the
attribute, it will not be added to the `linked` key, similar to what
happens with simple includes.
2015-01-13 14:49:58 -05:00
Alexandre de Oliveira
bd27da1b76 Adds support for meta attribute
Currently, 0.10.0.pre doesn't support `meta` option in `render`. This
way, there's no way to support features such as pagination. `0.9` had
this feature in place.

This adds support for it, as well as fixes small things in README.md.

This won't support `meta` in array responses because arrays don't have
keys, obviously. Also, the response should have a `root` key, otherwise
no `meta` will be included.

In some cases, for example using JsonApi, ArraySerializer will result in
a response with a `root`. In that case, `meta` will be included.
2015-01-05 02:56:33 -02:00
Gary Gordon
5560b49098 Allow overriding the adapter with render option
Make it easy to use multiple adapters in an app.

use "adapter: false" to not use ams

make a test override config.adapter
2014-11-13 10:23:19 -05:00
Alexandre de Oliveira
4af02021ac Merge pull request #703 from ggordon/specify_serializer_from_controller
Support serializer and each_serializer options in renderer
2014-11-12 20:09:51 -02:00
Alexandre de Oliveira
91b3fba509 Includes links inside of linked resources
According to
http://jsonapi.org/format/#document-structure-resource-objects,

> Resource objects have the same internal structure, regardless of
> whether they represent primary or linked resources.

And then
http://jsonapi.org/format/#document-structure-resource-object-attributes,

> There are four reserved keys in resource objects:
>
> "id"
> "type"
> "href"
> "links"

This commits includes `links` inside of linked resources.
2014-11-12 15:44:54 -02:00
Alexandre de Oliveira
971f501e55 Bugfix: include nested has_many association
Currently, doing `include: author.bio` would work correctly, but not for
has_many associations such as `include: author.roles`. This fixes it.

The problem was basically that we were not handling arrays for has_many linked,
as happens for ArraySerializers.
2014-11-11 14:35:00 -02:00
Gary Gordon
d97b2f5005 Fix infinite recursion
The method for testing whether to include an association was causing
an infinite loop when two models referenced each other.
2014-11-07 10:03:31 -05:00
Mike A. Owens
58b6c4a6b7 Allow for the implicit use of ArraySerializer when :each_serializer is specified. 2014-11-07 09:39:36 -05:00
Gary Gordon
c84430cdad Support serializer and each_serializer in render json 2014-11-05 15:10:14 -05:00
Gary Gordon
d5bae0c2f0 Include 'linked' member for json-api collections
The options passed to the render are partitioned into adapter options
and serializer options. 'include' and 'root' are sent to the adapter,
not sure what options would go directly to serializer, but leaving this
in until I understand that better.
2014-11-03 17:13:55 -05:00
Guillermo Iguaran
5f198667be Fix support for custom root in JSON-API adapter 2014-10-22 04:01:12 -03:00
Guillermo Iguaran
557b56a50e Refactor adapters to implement support for array serialization 2014-10-15 17:35:50 -05:00
Guillermo Iguaran
b91a86b401 Add test for serialization for adapters with root by default 2014-10-14 12:42:21 -05:00
NullVoxPopuli
7338b62b02 add support for root keys
remove debugging gem

fix white space
2014-10-13 13:27:09 -04:00
Steve Klabnik
970b542549 Implement basic rendering
Woo actioncontroller
2014-07-09 17:52:31 -04:00