Commit Graph

480 Commits

Author SHA1 Message Date
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
João Moura
1388ae82f2 Merge pull request #1120 from Eric-Guo/master
Add windows platform to loading sqlite3
2015-09-07 13:10:03 -03:00
João Moura
f4b17166eb Merge pull request #1123 from bacarini/master
Remove url options
2015-09-07 13:05:04 -03:00
Bruno Bacarini
8634503849 Remove url options
Removing url options because It does not works at all.
Thus, there are others PR at the moment to include url(links) as well.
2015-09-07 12:13:19 -03:00
João Moura
f149e5084b Merge pull request #1093 from beauby/improve-tests
Factor `with_adapter` + force cache clear before each test.
2015-09-07 10:52:15 -03:00
Lucas Hosseini
890003b305 Minor style improvements. 2015-09-07 09:06:17 +02:00
Lucas Hosseini
ebb05959d3 Merge remote-tracking branch 'upstream/master' into improve-tests 2015-09-07 09:03:17 +02:00
Eric Guo
70702f0af3 Add windows platform to loading sqlite3 to make testing suite pass, which caused by #1105 2015-09-07 14:19:57 +08:00
João Moura
c03427dc5c Merge pull request #1095 from beauby/config-docs
Add documentation about configuration options.
2015-09-07 02:13:11 -03:00
João Moura
8e084377d8 Adding code climate badges 2015-09-06 18:33:13 -03:00
João Moura
83975fc5be Updating appveyor badge 2015-09-06 18:03:03 -03:00
João Moura
6799453f09 Merge pull request #1069 from bf4/coverage
Add test coverage; account for no artifacts on CI
2015-09-06 17:58:54 -03:00
João Moura
2375420809 Merge pull request #1103 from beauby/fix-jsonapi-ri
Move `id` and `json_api_type` methods from `Serializer` to `JsonApi`.
2015-09-06 17:55:45 -03:00
Lucas Hosseini
f27f13ccc1 Fix style. 2015-09-06 19:29:51 +02:00
Lucas Hosseini
070a2e63bd Merge remote-tracking branch 'upstream/master' into fix-jsonapi-ri 2015-09-06 17:21:43 +02:00
Benjamin Fleischer
c401722c10 Add codeclimate test reporter (CI only)
requires repo admin to add
CODECLIMATE_REPO_TOKEN
to the CI env
2015-09-06 09:19:07 -05:00
Benjamin Fleischer
94469be1ca Add test coverage; account for no artifacts on CI
Drop coverage a bit for JRuby and Rubinius because they
don't generate the same Coverage as CRuby
2015-09-06 09:19:07 -05:00
João Moura
f2279946b6 Merge pull request #1106 from bf4/style_checking
Add Style enforcer (via Rubocop)
2015-09-05 15:54:35 -03:00
Benjamin Fleischer
c39c20d4a4 Add no-op rubocop for rbx 2015-09-04 02:32:16 -05:00
Benjamin Fleischer
1d15de4f8f Skip checking style in tmp 2015-09-03 22:56:04 -05:00
Benjamin Fleischer
8e8f6aba7e Remove space in {} 2015-09-03 20:55:40 -05: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
João Moura
6784866a2d Merge pull request #1079 from bf4/all_serializer_have_object
Add ArraySerializer#object like Serializer
2015-09-03 01:43:14 -03:00
Lucas Hosseini
3793f3ff4b Rename resource_objects_for to primary_data_for. 2015-09-02 19:27:40 +02:00
Lucas Hosseini
f8c553a0ed Cleanup. 2015-09-02 15:58:31 +02:00
Lucas Hosseini
b6b8dff8c9 Merge remote-tracking branch 'upstream/master' into improve-tests 2015-09-01 21:25:28 +02:00
Lucas Hosseini
a8a0566d29 Refactor relationships_for. 2015-09-01 21:16:00 +02:00
Lucas Hosseini
c593adbcb2 Further cleanup add_included. 2015-09-01 20:46:18 +02:00
Lucas Hosseini
bae4951e05 Further cleanup included_for. 2015-09-01 20:30:38 +02:00
Lucas Hosseini
91c5cbe0b9 Cleanup add_included. 2015-09-01 17:57:30 +02:00
Lucas Hosseini
f7612f2542 Further refactor/streamline method names. 2015-09-01 16:52:43 +02:00
Lucas Hosseini
04012052a6 Fix 'id' -> :id. 2015-09-01 15:39:29 +02:00
João Moura
e0b74d8731 Merge pull request #1096 from beauby/fix-attribute
Fix definition of serializer attributes with multiple calls to `attri…
2015-09-01 09:42:59 -03:00
Lucas Hosseini
c4faafdebc Refactor resource_identifier. 2015-09-01 10:15:50 +02:00
Lucas Hosseini
d9c680599a Refactor. 2015-09-01 01:41:29 +02:00
João Moura
8d3a89e106 Merge pull request #1105 from beauby/add-activerecord-fixtures
Add ActiveRecord-backed fixtures.
2015-08-31 02:37:57 -03:00
João Moura
5f03454037 Merge pull request #1108 from bf4/better_lint
Better lint
2015-08-31 02:36:35 -03:00
Benjamin Fleischer
9673b6471c Better lint
Extracted from
https://github.com/rails-api/active_model_serializers/pull/1004/files#diff-56455571c4ba7a2b4c640b9e8168f522R40

Correct cache_key lint for ActiveRecord 4.1+

https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/integration.rb
def cache_key

https://github.com/rails/rails/blob/4-1-stable/activerecord/lib/active_record/integration.rb
def cache_key(*timestamp_names)
2015-08-31 00:22:17 -05:00
João Moura
b20f1f5f9d Merge pull request #1102 from beauby/remove-embed
Remove remains of `embed` option.
2015-08-31 01:33:30 -03:00
Lucas Hosseini
343f8b96bd Fix bug preventing id overriding. 2015-08-31 06:25:20 +02:00
Lucas Hosseini
83f11acd66 Add Gemfile dependencies to ActiveRecord and sqlite3. 2015-08-31 06:19:16 +02:00
Lucas Hosseini
b175b04408 Merge remote-tracking branch 'upstream/master' into add-activerecord-fixtures 2015-08-31 05:40:02 +02:00
João Moura
9ff35dabda Merge pull request #1090 from bf4/clarify_ams_dependencies
Clarify AMS dependencies
2015-08-31 00:39:24 -03:00
Benjamin Fleischer
e3d3d92201 Clarify AMS dependencies 2015-08-30 22:26:25 -05:00
Lucas Hosseini
d0d00d02a0 Add ActiveRecord-backed fixtures. 2015-08-31 05:11:32 +02:00
Lucas Hosseini
f95f7369f0 Refactor add_resource_relationship. 2015-08-31 03:24:03 +02:00