Commit Graph

216 Commits

Author SHA1 Message Date
Benjamin Fleischer
80e75202ea Document Serializer and FragmentCache 2015-10-22 10:31:21 -05:00
Benjamin Fleischer
2c8b9b796d Rename ArraySerializer to CollectionSerializer for clarity 2015-10-21 16:53:26 -05:00
Benjamin Fleischer
124faaa829 Add PORO serializable base class: ActiveModelSerializers::Model 2015-10-14 21:33:02 -05:00
João Moura
da7e6dc795 Merge pull request #1247 from beauby/jsonapi-toplevel-links
Add support for toplevel JSON API links.
2015-10-10 17:52:08 -03:00
L. Preston Sego III
6018ef16c4 Merge pull request #1225 from beauby/nested-serializer-lookup
Add support for nested serializers
2015-10-09 10:12:08 -04:00
Benjamin Fleischer
49eb531539 Merge pull request #1239 from beauby/fix-duplicate-jsonapi
Fix duplicate resources inside included in compound document.
2015-10-08 14:19:02 -05:00
Lucas Hosseini
9147469842 Extend serializer lookup to the parent serializer. 2015-10-08 18:23:54 +02:00
Benjamin Fleischer
dc44b136aa Remove unnecessarily silenced warnings 2015-10-07 05:54:57 -05:00
Lucas Hosseini
54303b6290 Add support for toplevel JSON API links. 2015-10-06 20:01:46 +02:00
Lucas Hosseini
f4bb4c81b0 Fix duplicate resources between data and included. 2015-10-06 17:25:27 +02:00
Lucas Hosseini
503bfe9598 Move meta/meta_key handling inside adapter. 2015-10-05 19:06:50 +02:00
Lucas Hosseini
658810e6a0 Extract attributes filtering from serializer into adapter. 2015-10-05 08:21:26 +02:00
Lucas Hosseini
34d65716cb Merge pull request #1214 from NullVoxPopuli/issue/1211-failing-test
Fix #1211, include_tree is null when using the key: options
2015-10-02 15:29:04 +02:00
Liam Bowen
f8323fc9e5 Fixes #1211 - retrieve the key from the reflection options when building associations 2015-10-02 08:52:21 -04:00
Benjamin Fleischer
a2bfe190e3 Merge pull request #1050 from bf4/json_api_member
Add top-level jsonapi member to JSON API adapter
2015-10-01 23:03:11 -05:00
Ivan Yurov
2dd569ae51 Add Serializer 'type' directive to control type field, for use by the JsonApi adapter 2015-10-01 22:48:57 -04:00
Benjamin Fleischer
9d7da8afc4 Add support for top level jsonapi member. 2015-10-01 21:22:49 -05:00
Lucas Hosseini
35473cf983 Merge pull request #1186 from bf4/empty_else
Enforce case requires else; allow else nil
2015-09-21 17:48:27 +02: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
Benjamin Fleischer
1d8a3d039a Enforce case requires else; allow else nil 2015-09-21 09:50:53 -05: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
L. Preston Sego III
8f2dd66710 Merge pull request #1183 from rails-api/block_style
Update Rubocop todo
2015-09-21 01:25:50 -05:00
Lucas Hosseini
4976837c31 Fix options passing in Json and Attributes adapters. 2015-09-21 07:13:17 +02:00
Benjamin Fleischer
140b4f2735 Update Rubocop todo 2015-09-21 00:11:08 -05: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
19de5f7722 Introduce Adapter::Base
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
2015-09-20 12:26:04 -05:00
Benjamin Fleischer
ad2ca3b45c Remove Adapter autoloads in favor of require
Adapters must be eager loaded to ensure they are defined
before they are used as namespacing.

cf6a074a1c (diff-41f2b3509d33e1c65bb70ee0ec7a2eea)
2015-09-18 12:44:53 -05:00
Benjamin Fleischer
c6f8d0f5f2 Rename FlattenJson to Attributes (allow plural adapter names) 2015-09-18 10:17:02 -05:00
Benjamin Fleischer
ceef214f1e FlattenJson adapter no longer inherits Json adapter 2015-09-18 10:15:49 -05:00
L. Preston Sego III
701eb17a27 Merge pull request #1172 from bf4/better_adapter_registration
Better serializer registration, get more than just the first module
2015-09-18 02:12:54 -04:00
Benjamin Fleischer
eb1264ad99 Better serializer registration, get more than just the first module
But is potentially breaking anyone on rc3, but the fix is just
to manually register the adapter with the rc3-style name
2015-09-17 23:33:04 -05:00
Benjamin Fleischer
0091be89f8 Consistently refer to the 'JSON API' and the 'JsonApi' adapter 2015-09-17 15:22:28 -05:00
Benjamin Fleischer
9d65f0adc5 Distinguish options ivar from local; Extract latent Adapter::CachedSerializer 2015-09-17 10:45:09 -05:00
João M. D. Moura
1ca73e0c33 updating version to new release 2015-09-16 12:19:44 -03:00
L. Preston Sego III
1bff6173d1 Merge pull request #1089 from bf4/add_logger
Add ActiveModelSerializers.logger with default null device
2015-09-15 14:51:29 -04:00
João Moura
479146c02a Merge pull request #1121 from beauby/fix-jsonapi-links
Refactor `add_links` in JSONAPI adapter.
2015-09-15 05:00:39 -03:00
Lucas Hosseini
fb7ec88e2e Remove unnecessary parentheses accidentally reintroduced in #1017. 2015-09-15 00:25:37 +02:00
Lucas Hosseini
319a9071af Remove legacy method accidentally reintroduced in #1017. 2015-09-15 00:22:28 +02:00
Lucas Hosseini
285cdf841e Split serializable_hash into two methods. 2015-09-14 06:46:52 +02:00
Lucas Hosseini
572ff7db20 Refactor add_links in JSONAPI adapter. 2015-09-14 06:46:52 +02:00
Lucas Hosseini
2789a579e8 Merge pull request #1131 from beauby/jsonapi-include-tree
Extended format for JSONAPI `include` option
2015-09-13 23:10:05 +02:00
Lucas Hosseini
ce7a839f3d Extended format for JSONAPI include option. 2015-09-13 22:45:47 +02:00
Ville Lautanala
fada4dcb08 Fix typo in fieldset exception 2015-09-12 18:56:15 +03:00
L. Preston Sego III
b594d1487b Merge pull request #1132 from beauby/fix-flatten-json
Get rid of unnecessary instance variables, and implied dependencies.
2015-09-12 11:51:38 -04:00
Lucas Hosseini
a9d07cd68f Get rid of unnecessary instance variables, and implied dependencies. 2015-09-10 04:02:06 +02:00
Benjamin Fleischer
28345adef0 Use Adapter.const_get instead of safe_constantize
(Thanks to sandstrom for the reference to ActiveJob::QueueAdapters
a11571cec3/activejob/lib/active_job/queue_adapters.rb (L123-L133)
2015-09-09 08:55:20 -05:00
Benjamin Fleischer
363345b8dd Rename Adapter.get to Adapter.lookup
Per https://github.com/rails-api/active_model_serializers/pull/1017#discussion_r39003855
comment by sandstrom in discussion of the inherited hook

> I'm thinking that it would be better to register adapters manually, without using the hook, i.e.
> have people call ActiveModel::Serializer::Adapter.register directly (perhaps in an initializer).

> Possibly, some inspiration can be taken from how ActiveJob adapters are wired[1].

> [1] a11571cec3/activejob/lib/active_job/queue_adapter.rb (L52-L56)
2015-09-09 08:55:20 -05: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
Benjamin Fleischer
d9e76c29d5 Make Adapters registerable so they are not namespace-constrained
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
2015-09-08 22:59:36 -05:00