Commit Graph

246 Commits

Author SHA1 Message Date
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
9d65f0adc5 Distinguish options ivar from local; Extract latent Adapter::CachedSerializer 2015-09-17 10:45:09 -05: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
L. Preston Sego III
a34cb998c7 rubocop-fixes 2015-09-15 14:58:04 -04: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
L. Preston Sego III
a453de112b Merge pull request #1109 from bf4/fix_minitest_after_run
Make better use of Minitest's lifecycle
2015-09-15 14:18:07 -04:00
Lucas Hosseini
7e2f3d122b Merge pull request #1110 from beauby/add-ar-models-tests
Add lint tests for AR models
2015-09-14 02:05:54 +02:00
Lucas Hosseini
ce7a839f3d Extended format for JSONAPI include option. 2015-09-13 22:45:47 +02: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
Lucas Hosseini
ec6260870d Add lint tests for AR models. 2015-09-07 18:34:42 +02: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
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
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
Lucas Hosseini
070a2e63bd Merge remote-tracking branch 'upstream/master' into fix-jsonapi-ri 2015-09-06 17:21:43 +02: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
Benjamin Fleischer
b18671fd03 Make better use of Minitest's lifecycle
http://blog.arkency.com/2013/06/are-we-abusing-at-exit/
2015-09-06 07:34:42 -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
b6b8dff8c9 Merge remote-tracking branch 'upstream/master' into improve-tests 2015-09-01 21:25:28 +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
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
Benjamin Fleischer
005f71e2c2 Add ActiveModelSerializers.logger with default null device 2015-08-30 23:26:31 -05:00
Lucas Hosseini
83f11acd66 Add Gemfile dependencies to ActiveRecord and sqlite3. 2015-08-31 06:19:16 +02:00
Lucas Hosseini
d0d00d02a0 Add ActiveRecord-backed fixtures. 2015-08-31 05:11:32 +02:00
Lucas Hosseini
8482abfac7 Move id and json_api_type methods from Serializer to JsonApi. 2015-08-31 02:32:38 +02:00
Lucas Hosseini
c5446d759f Remove traces of embed option. 2015-08-31 01:26:22 +02:00
Lucas Hosseini
995bbcc18d Fix definition of serializer attributes with multiple calls to attribute instead of one single call to attributes. 2015-08-28 21:06:10 +02:00
Lucas Hosseini
4bba16bf4e Factor with_adapter + force cache clear before each test. 2015-08-28 17:00:24 +02:00
João Moura
64168cbecd Merge pull request #1081 from beauby/jsonapi-singular-plural-config
Add configuration option to set resource type to singular/plural
2015-08-28 07:47:55 -03:00
Benjamin Fleischer
d315151e8a Fix warnings
JRuby-specific: fix 'warning: (...) interpreted as grouped expression'
2015-08-26 09:22:02 -05:00
Benjamin Fleischer
9aad8b4d29 Cleanup CaptureWarnings after_run 2015-08-26 09:19:36 -05:00
Benjamin Fleischer
a8e9bb1c14 Remove uniq on warnings that confused output 2015-08-26 09:19:36 -05:00
Benjamin Fleischer
530a1bdfd7 Compartmentalize test helper support 2015-08-26 09:19:36 -05:00
João Moura
af280abbb7 Merge pull request #1066 from rails-api/appveyor
Adding appveyor to the project
2015-08-26 04:46:16 -03:00
Lucas Hosseini
91235ba7bc Add configuration option to set resource type to singular/plural with jsonapi. 2015-08-24 22:55:22 +02:00
Benjamin Fleischer
dc4ee94fea Add ArraySerializer#object like Serializer 2015-08-24 13:51:46 -05:00
João Moura
87c47f8fdc Merge pull request #1041 from bacarini/master
Adding pagination links
2015-08-22 13:38:32 -03:00
Eric Guo
30463f88fa Make testing suite running and pass in Windows
thanks @bf4 give many help and suggestion, original PR https://github.com/rails-api/active_model_serializers/pull/1014
2015-08-21 21:07:52 +08:00
Bruno Bacarini
d50262edbe test pagination links the way the controller does 2015-08-20 11:32:03 -03:00
Bruno Bacarini
01eab3bdb4 send whole request context to model serializer 2015-08-19 10:48:48 -03: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