Commit Graph

681 Commits

Author SHA1 Message Date
Lucas Hosseini
adaf5b87df Merge pull request #1248 from beauby/jsonapi-parse
JSON API deserialization.
2016-01-13 06:18:52 +01:00
Lucas Hosseini
20a58d7f5c Add support for JSON API deserialization (experimental). 2016-01-13 05:48:06 +01:00
Lucas Hosseini
2e87c8effe Fix comment. 2016-01-13 05:41:32 +01:00
Ben Woosley
8ac1f107f4 Remove unnecessary dup in ActiveModel::Serializer::Associations#associate
The `_reflections` are duped on `inherited` - no need to `dup` them
with each addition.
2016-01-12 14:42:07 -08: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
Lucas Hosseini
f6fe0c8aa3 Extract links and type-related methods to their own module. 2016-01-12 15:05:39 +01:00
Ben Woosley
0a6c133d25 Tidy up the tests
* 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
2016-01-07 11:21:19 -08:00
lcp
43312fa083 support read_multi 2016-01-05 20:55:34 +08:00
Benjamin Fleischer
7d4f0c5c8a Merge branch 'bf4-consider_association_blocks' 2016-01-03 23:16:49 -06:00
Lucas Hosseini
77095f2a84 Add ActiveSupport::Autoload extension to Attribute. 2015-12-30 17:44:19 +01:00
Lucas Hosseini
a586a45863 Remove key from Attribute class. 2015-12-30 16:47:10 +01:00
Lucas Hosseini
7d24cbfd3d Extract latent Attribute object. 2015-12-30 16:47:10 +01:00
Lucas Hosseini
1d4b27f60f Improve attribute value computation. 2015-12-30 16:47:10 +01:00
Lucas Hosseini
ee0283cb57 Simplify attributes handling. 2015-12-30 16:47:10 +01:00
Benjamin Fleischer
d7de53ce30 Consider evaluating association in serializer context
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
```
2015-12-29 22:16:42 -06:00
Lucas Hosseini
fd06a8ad0d Extract caching into its own module. 2015-12-28 02:53:49 +01:00
Mauro George
5fb7cceafb Remove ActiveModelSerializers.silence_warnings dead code
The ActiveModelSerializers.silence_warnings was used to avoid warnings on the
Ruby interpreter when define a private attr_acessor. This method is not used in
any part of the code and the recommend way to handle this case is to use
protected instead the silence_warnings [1].

This patch remove the method from the project, this way we avoid people using
this by mistake.

[1]: https://bugs.ruby-lang.org/issues/10967
2015-12-23 19:25:48 -02:00
Benjamin Fleischer
b6a4ad1c26 Merge pull request #1383 from beauby/simplify-associations
Simplify reflections handling.
2015-12-22 10:37:28 -06:00
Benjamin Fleischer
fce856fc83 Merge pull request #1371 from bf4/documentation_updates
[DOCS] Refactor, update, create documentation
2015-12-20 22:30:44 -06:00
Benjamin Fleischer
30fd9d9eb7 Merge pull request #1353 from bf4/disable_serializer_lookup
Allow users to globally opt out of automatic serializer lookup
2015-12-20 20:04:16 -06:00
Lucas Hosseini
762f298c03 Simplify reflections handling. 2015-12-20 16:19:10 +01:00
Benjamin Fleischer
dff607dc41 Merge pull request #1336 from johnhamelink/master
Grape formatter feature requested in #1258 - Rebased and Repushed (#1273)
2015-12-17 20:31:38 -06:00
Benjamin Fleischer
ce17a1b305 [DOCS] Refactor, update, create documentation [ci skip] 2015-12-14 14:38:29 -06:00
Benjamin Fleischer
bf8270b8b4 Document Serializer settings and private api [ci skip] 2015-12-10 15:08:22 -06:00
Julian Paas
d85a17bb33 Grape formatter feature requested in #1258
- 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.
2015-12-10 10:06:40 +00:00
Benjamin Fleischer
386a567dfc Evaluate association blocks as scopes on the association 2015-12-04 13:58:22 -06:00
Benjamin Fleischer
3e8290a923 Serializer instance methods don't change; track at class level
Per groyoh
https://github.com/rails-api/active_model_serializers/pull/1356#discussion_r46713503
2015-12-04 13:31:34 -06:00
Benjamin Fleischer
c4feccfd10 Refactor Association/Reflection block value reading 2015-12-03 10:53:43 -06:00
Benjamin Fleischer
cd736e0adf Memoize attributes 2015-12-02 17:47:24 -06:00
Benjamin Fleischer
036604b149 Extract Serializer Attributes into its own file 2015-12-02 17:45:42 -06:00
Benjamin Fleischer
eceb2d5598 Refactor serializer attribute objects 2015-12-02 17:33:57 -06:00
Benjamin Fleischer
8804d758ef Remove dynamically defined instance methods 2015-12-02 17:20:22 -06:00
Benjamin Fleischer
0bf45ec2a7 Small refactor to Serializer::_attribute_mappings 2015-12-02 17:20:22 -06:00
Noah Silas
7bde7bf752 Handle conflicts between key names and serializer methods
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.
2015-12-02 17:20:22 -06:00
Benjamin Fleischer
e2903643c5 Encapsulate serialized_associations; test inline associations 2015-12-02 17:20:22 -06:00
Lucas Hosseini
7cbef1b3b5 Add inline syntax for defining associations
Adapted from
https://github.com/rails-api/active_model_serializers/pull/1262
2015-12-02 17:20:22 -06:00
Benjamin Fleischer
6020450fe4 Allow specifying attributes with a block
Adapted from https://github.com/rails-api/active_model_serializers/pull/1262
2015-12-02 17:20:22 -06:00
Benjamin Fleischer
87d18e9c32 Map attributes to Attribute values when defined in serializer 2015-12-02 17:20:22 -06:00
Benjamin Fleischer
b3b9a46eeb Merge pull request #1358 from rwstauner/dirs-with-spaces
Match file paths with spaces in caller regexp
2015-12-02 10:23:40 -06:00
Randy Stauner
90fa377040 Match file paths with spaces in caller regexp 2015-12-02 06:17:34 -07:00
yuuji.yaginuma
d8998a4877 fix description of USAGE 2015-12-01 08:54:27 +09:00
Benjamin Fleischer
28394340d8 Add config.serializer_lookup_enabled that defaults true 2015-11-30 00:20:58 -06:00
Benjamin Fleischer
47a14b6581 Improve ActionController::Serialization readability 2015-11-30 00:15:02 -06:00
Trek Glowacki
170db3ba88 Allow users to globally opt out of automatic lookup 2015-11-30 00:15:02 -06:00
Benjamin Fleischer
a9ce4fb766 Move caching initialization to Railtie
Also
- Add reference to config from ActiveModelSerializers.config
- correctly call super in FragmentCacheTest#setup
- rename test rails app from Foo to ActiveModelSerializers::RailsApplication
2015-11-25 21:56:01 -06:00
Peter Wagenet
39ef9bff10 Fix typo 2015-11-24 07:22:59 -08:00
Benjamin Fleischer
aa4384839d Merge pull request #1322 from bf4/maurogeorge-patch-10
Instrumenting rendering of resources
2015-11-19 13:01:38 -06:00
Benjamin Fleischer
733f5bca7e Rename event from AMS to active_model_serializers
per NullVoxPopuli comment
2015-11-19 12:44:33 -06:00
Benjamin Fleischer
d1c44c719d Update for review per maurogeorge feedback 2015-11-19 12:41:51 -06:00
Benjamin Fleischer
b36cc42f03 Separate out callbacks per ActiveJob pattern 2015-11-10 03:09:24 -06:00
Benjamin Fleischer
e8efc4eff4 Refactor callbacks in ams::logging 2015-11-10 03:09:24 -06:00
Benjamin Fleischer
360ecc88fe Clean up notification code with some meta-prog 2015-11-10 03:09:24 -06:00
Benjamin Fleischer
21bb306d38 Keep Logging in the ActiveModelSerializers namespace 2015-11-10 03:09:24 -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
e5a109865c Test ArraySerializer less rigorously on Minitest 4 2015-11-08 23:12:56 -06:00
tchak
31172b1be5 rename context to serialization_context 2015-11-07 20:32:27 +01:00
Paul Chobert
6407dbeadd Add test and bugfix to include an array of string 2015-11-06 17:23:25 +01:00
Lucas Hosseini
8ac2b9b01f Minor cleanup. 2015-11-03 23:59:12 +01:00
Lucas Hosseini
3804dcc238 Add support for resource-level JSON API links. 2015-11-03 23:50:16 +01:00
Lucas Hosseini
0948c4199a Compute only requested attributes. 2015-10-31 19:20:53 +01:00
L. Preston Sego III
17be650538 Revert "rename context to serialization_context and add url helpers" 2015-10-29 08:16:47 -04:00
L. Preston Sego III
a063cbea21 Merge pull request #1289 from tchak/serialization_context
rename context to serialization_context and add url helpers
2015-10-29 07:56:47 -04:00
L. Preston Sego III
48b041ecae Merge pull request #1294 from beauby/fix-serializer-lookup-non-ams
Only use subclasses of ActiveModel::Serializer during lookup.
2015-10-25 23:01:00 -04:00
Lucas Hosseini
b5aecfd114 Initialize fieldset. 2015-10-25 19:56:43 +01:00
Lucas Hosseini
13ef8fed1b Fix fields option to restrict relationships as well. 2015-10-24 21:28:13 +02:00
Lucas Hosseini
1a42345d84 Only use subclasses of ActiveModel::Serializer during lookup. 2015-10-23 17:17:03 +02:00
tchak
d08ee5994f rename context to serialization_context and add url helpers 2015-10-22 22:28:54 +02:00
Benjamin Fleischer
274cb66d34 Edits per beauby [ci skip] 2015-10-22 10:31:21 -05:00
Lucas Hosseini
63317699f3 Correct minor typos 2015-10-22 10:31:21 -05:00
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
737784c9b7 Merge pull request #1272 from bf4/add_public_poro_base
Add PORO serializable base class: ActiveModelSerializers::Model
2015-10-20 12:50:05 -05:00
Benjamin Fleischer
124faaa829 Add PORO serializable base class: ActiveModelSerializers::Model 2015-10-14 21:33:02 -05:00
Benjamin Fleischer
8529ea43c9 Handle no serializer source file to digest.
output warning

Closes #1176
2015-10-14 20:56:29 -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
f52ade25aa Merge pull request #1255 from bf4/serialize_inheritable_attributes
Make more class attributes inheritable
2015-10-09 03:10:28 -05: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
L. Preston Sego III
d5e6e3148a Merge pull request #1254 from bf4/remove_useless_access_modifier
Remove unnecessarily silenced warnings
2015-10-07 07:22:15 -04:00
Benjamin Fleischer
0f50847c7d Make more class attributes inheritable 2015-10-07 06:07:22 -05:00
Benjamin Fleischer
dc44b136aa Remove unnecessarily silenced warnings 2015-10-07 05:54:57 -05:00
Rodrigo Ra
5706e7d7fe serializer inherits cache configuration 2015-10-07 00:34:21 -03: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
L. Preston Sego III
3fe343f240 Merge pull request #1232 from beauby/move-root-to-adapter
Extract attributes filtering from serializer into adapter.
2015-10-05 12:56:54 -04:00
Lucas Hosseini
658810e6a0 Extract attributes filtering from serializer into adapter. 2015-10-05 08:21:26 +02:00
Lucas Hosseini
7847d05ecb Remove root_name class method from Serializer, as it is used nowhere. 2015-10-05 07:27:13 +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
8e8cfd45ff Fix bundle console no longer tries to load I18n::Rails 2015-10-01 23:08:34 -05: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
Benjamin Fleischer
a819da6c13 Merge pull request #1213 from AutoCloud/type_setting
Add Serializer 'type' directive to control type field, for
 use by the JsonApi adapter
2015-10-01 22:59:17 -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
Mauro George
908f13fe6c Remove empty rubocop.rake 2015-10-01 19:05:28 -03:00
Lucas Hosseini
e552e4329b Fix. 2015-09-23 18:59:32 +02: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
24a5f3843e Merge pull request #1171 from shicholas/require_statements
add require statements to top of file
2015-09-18 02:27:23 -04: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
L. Preston Sego III
e0a0ad3e46 Merge pull request #1167 from rails-api/simplify_serializer_attributes
Delegate Serializer.attributes to Serializer.attribute
2015-09-18 01:24:45 -04:00
Nicholas Shook
7f17ec8afa bring back autoload - fix test 2015-09-17 22:03:22 -07: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
484426ce17 Delegate Serializer.attributes to Serializer.attribute 2015-09-17 22:11:57 -05:00
Nicholas Shook
444b4cd1d8 all require 2015-09-17 14:06:55 -07:00
Nicholas Shook
faa56482d1 full require 2015-09-17 14:06:55 -07:00
Nicholas Shook
6bdb4a13e2 removed autoload statements 2015-09-17 14:06:54 -07:00
Nicholas Shook
a9e3143c19 add require statements to top of file
Based on
https://github.com/rails-api/active_model_serializers/issues/1170#issuecomment-141184047
2015-09-17 14:06:54 -07:00
Benjamin Fleischer
0091be89f8 Consistently refer to the 'JSON API' and the 'JsonApi' adapter 2015-09-17 15:22:28 -05:00
Benjamin Fleischer
c9ae868bfb Comment private accessor warnings 2015-09-17 14:32:22 -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
610775a95f Merge pull request #1129 from bf4/remove_serializable_resource_serialize
Remove SerializableResource.serialize in favor of `.new`
2015-09-15 17:32:44 -04: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
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
7eddbe418d Remove SerializableResource.serialize in favor of .new
Per discussion in https://github.com/rails-api/active_model_serializers/issues/1098
2015-09-08 23:28:10 -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
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
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
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
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
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
Benjamin Fleischer
005f71e2c2 Add ActiveModelSerializers.logger with default null device 2015-08-30 23:26:31 -05:00
Lucas Hosseini
343f8b96bd Fix bug preventing id overriding. 2015-08-31 06:25:20 +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
f95f7369f0 Refactor add_resource_relationship. 2015-08-31 03:24:03 +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
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
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
João Moura
fc7b9c3c14 Merge pull request #1063 from bf4/poro_lint
Lead by example: lint PORO model
2015-08-20 03:53:11 -03:00
Bruno Bacarini
3c3578a9b8 improvements on how to get self link on pagination class 2015-08-19 11:16:53 -03:00
Bruno Bacarini
01eab3bdb4 send whole request context to model serializer 2015-08-19 10:48:48 -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
59ae84baba exchange to a faster regex to get origina_url 2015-08-18 19:04:03 -03:00
Bruno Bacarini
77a8f66ad8 fix message on raise of pagination links class 2015-08-18 19:04:03 -03:00
Bruno Bacarini
e0d050d2af remove resource and options attr_reader from array_serialize 2015-08-18 19:04:03 -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
1fe8b06986 exchange pagination class to inside json_api scope 2015-08-18 19:04:02 -03:00
Bruno Bacarini
b864302695 remove 'page object' on paginations links' 2015-08-18 19:04:02 -03:00
Bruno Bacarini
f7c77c1256 add feature to include pagination links in response 2015-08-18 19:04:02 -03:00
Benjamin Fleischer
215fb85c7f Test caller line parsing and digesting 2015-08-18 17:32:29 -04:00
João Moura
e384b65f5d Merge pull request #1048 from bf4/cleanup_meta_in_adapter
Let FlattenJson adapter decide it doesn't include meta
2015-08-18 16:23:03 -03:00
Benjamin Fleischer
98d009a000 Let FlattenJson adapter decide it doesn't include meta 2015-08-18 15:13:21 -04:00
Benjamin Fleischer
dca286b0ec Lead by example: lint PORO model 2015-08-18 12:59:12 -04:00
Eric Guo
f93a7e8b41 need lookahead match for windows file path contain 'c:/git/'
So we will got full file path instead of only c if caller.first is: c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'

CALLER_FILE = /
  /A # start of string
  \S+ # one or more non-spaces
  (?= # stop previous match when
    :\d+:in # a colon is followed by one or more digits
               # followed by a colon followed by in
   )
/x

credit from https://gist.github.com/mikezter/540132 and @bf4
2015-08-18 09:52:34 +08:00
Aaron Lerch
35c8f0d835 Update fragment cache to support namespaced objects 2015-08-17 17:12:30 -04:00
Mikhail Topolskiy
e8e4bdefd2 Use underscored json_root 2015-08-13 20:31:48 +03:00
João Moura
b4949fbc2a Merge pull request #1049 from bf4/fix_json_api_adapter_serialable_hash
Fix incorrect s/options = {}/options ||= {}
2015-08-12 11:08:39 -03:00
Benjamin Fleischer
43e09c03de Fix incorrect s/options = {}/options ||= {}
Introduced in #965, surfaced in #1041
2015-08-11 17:08:09 -05:00
Josh Lane
033ce8e88d allow for a type attribute
* "namespace" json_api specific type method
2015-08-11 08:33:05 -07:00
Josh Lane
4af98852b8 fix warning
* don't overshadow serializer variable
2015-08-11 08:32:20 -07:00
Josh Lane
f6e3d4e1f9 allow id attribute to be overriden 2015-08-04 09:22:05 -07:00
João Moura
c4af610ed2 Merge pull request #1031 from bolshakov/feature/disaplow_to_define_multiple_associations_at_once
Disallow to define multiple associations at once
2015-08-01 18:22:00 -03:00
elliotlarson
b99a6350cc only require railtie if Rails is present 2015-07-31 12:51:57 -07:00
Артём Большаков
424a053ee5 Disallow to define multiple associations at once 2015-07-31 19:15:56 +03:00
João Moura
728b8f980e Merge pull request #1026 from jfelchner/patch-1
Bump Version Number to 0.10.0.rc2
2015-07-31 11:34:42 -03:00
João Moura
c703d0f35c Merge pull request #985 from bolshakov/feature/each_association
Associations implementation refactoring
2015-07-31 10:57:03 -03:00
Jeff Felchner
e468030cbf Bump Version Number to 0.10.0.rc2
Due to the fact that users need to switch from the released version to `master` occasionally to pull in upstream bugfixes, it's important that this version number stays in sync with the released version.
2015-07-31 04:51:55 -05:00
Артём Большаков
2952a332e0 Associations refactoring
* Move all associations related code from Serializer class to Associations module
* Introduce Reflection class hierarchy
* Introduce Association class
* Rid off Serializer#each_association
* Introduce Serializer#associations enumerator
2015-07-30 11:20:29 +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
João Moura
418721302b defining json_key(root) as model class name 2015-07-23 02:11:34 -04:00
João Moura
6266b6a002 Merge pull request #1006 from bf4/inflector-testing
Fix adapter inflection bug for api -> API
2015-07-20 23:20:00 -04:00
Benjamin Fleischer
ed23a37de9 require rails/railtie before subclassing Rails::Railtie 2015-07-17 14:16:30 -05:00
Benjamin Fleischer
4359026c0e Handle inflecting api to s/API/Api without side-effects 2015-07-17 14:09:13 -05:00
vyrak bunleang
1b09d0ec42 array serializer uses root option for json_key if available 2015-07-16 15:34:19 -06:00
Marek Pietrucha
e5ccb8e4dd root option is working (fixed #986) 2015-07-16 15:32:09 -06:00
Jiajia Wang
7faa5e8e2e Bug fix for ArraySerializer json_key
When the resource is a zero result query,
i.e. post_comments = PostComment.where("1=0")
the json_key will become 'postcomments' rather than 'post_comments'.
Using 'underscore' instead of 'downcase' fixes the error.
2015-07-15 10:11:06 +10:00
João Moura
775737619a Merge pull request #996 from bf4/serializer_lint
Add linter for serializable resource
2015-07-13 19:37:39 -04:00
Benjamin Fleischer
28174e297d Add linter for serializable resource 2015-07-09 11:20:19 -05:00
Rodrigo Ra
df63b59512 Add key option to serializer associations 2015-07-05 19:47:58 -03:00
João Moura
952d8adcdc Merge pull request #978 from regonn/fix-generators-template
fix generators template bug
2015-07-02 00:31:55 -03:00
regonn
5f300a0d42 fix generators template bug 2015-06-30 18:16:27 +09:00
Hugo Almeida
f25071ca70 Fixes virtual value not being used 2015-06-29 10:48:12 +09:00
João Moura
d3649d5b4e Renaming Error to NoSerializerError 2015-06-26 02:16:35 -03:00
Benjamin Fleischer
e5d1e40dbd Handle special-case of Array serializer with unserializable elements 2015-06-26 02:16:34 -03:00
Benjamin Fleischer
cf77786da2 Fix #955 2015-06-26 02:16:34 -03:00
João Moura
189b79523c fixing array rendering when elements doesn't have a serializer 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
c0a82648d5 Merge pull request #971 from bf4/readd_has_one_to_generator
Restore has_one to generator
2015-06-25 18:37:52 -03:00
Benjamin Fleischer
81935c8114 Restore has_one to generator
per #822 since it was readded in #725
2015-06-25 14:00:27 -05:00
Benjamin Fleischer
2d24dded14 serializable_hash and as_json should take options = nil
per ActiveModel::Serialization#serializable_hash
96bb004fc6/activemodel/lib/active_model/serialization.rb
    def serializable_hash(options = nil)
          options ||= {}

Otherwise, passing in nil to `as_json` or `serializable_hash`
makes things blow up when passing nil into attributes
2015-06-24 11:46:29 -05:00
João Moura
e321cb366d Getting root key from AR::Relation 2015-06-16 11:36:17 -03:00
João Moura
65e0d79195 pluralising root key when using arraySerializer 2015-06-15 15:59:01 -03:00
João Moura
329691276a disabling custom root option 2015-06-15 15:58:39 -03:00
João Moura
d061b2e9f4 enabling flatten json as default adapter 2015-06-15 13:49:24 -03:00
João Moura
1ea5608e78 updating tests to match new adapters structure 2015-06-15 13:39:36 -03:00
João Moura
2e46507971 avoiding nil cases when dynamically creating a class 2015-06-15 02:50:06 -03:00
João Moura
8e1214b4c5 force to use flattenJson when dealing with ArraySerializer 2015-06-15 02:48:39 -03:00
João Moura
b2f1947d4a removing unseless root method 2015-06-14 17:24:48 -03:00
João Moura
2bf91a0c0e updating adapters to follow new root logic 2015-06-14 03:25:20 -03:00
João Moura
a0753cb0bc autoloading new flatten son adapter 2015-06-14 03:25:00 -03:00
João Moura
1c3a180a20 disable root as flag option 2015-06-13 15:48:33 -03:00
João Moura
c8fcb60a5d addung fragment_cache method to Adapter::Json 2015-06-13 15:48:33 -03:00
João Moura
5932da64ef creating flatten_json adapter 2015-06-13 15:48:33 -03:00
João Moura
37114e9d5b removing unnecessary root parameter on fragment cache 2015-06-13 15:48:33 -03:00
Lachlan Sylvester
97e82c3eb7 use model name to determine the type 2015-06-13 19:45:46 +10:00
João Moura
de23501995 Merge pull request #949 from edwardloveall/el-870-fix
Don't pass serializer option to associated serializers
2015-06-13 03:01:25 -03:00
João Moura
6251b90d25 Merge pull request #902 from cristianbica/serializer_file_digest
Added serializer file digest to the cache_key
2015-06-12 18:56:43 -03:00
Santiago Pastorino
4752e6723a Make resource generator invoke serializer generator 2015-06-10 20:45:33 -03: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
João Moura
12adb2e6d4 Merge pull request #924 from navinpeiris/fragment-cache
Avoid unecessary calls to attribute methods when fragment caching
2015-05-27 14:55:31 -03:00
Navin Peiris
e0947fcbd4 Fixing issue where fragment cache calls attribute methods multiple times, even when they are supposed to be cached 2015-05-22 00:40:22 +05:30
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
Guillermo Iguaran
f7fb4dbb98 Merge pull request #909 from joaomdmoura/json-api-default
Defining Json-API Adapter as Default
2015-05-20 20:50:19 -05:00
Ryan Schlesinger
9355416ad0 Add rescue_from handler to clear state
Fixes #917
2015-05-19 17:23:29 -07:00
groyoh
5393e5d235 Prevent possible duplicated attributes
Calling ActiveModel::Serializer.attributes or ActiveModel::Serializer.attribute
methods multiple times won't create duplicated attributes anymore.
2015-05-18 22:42:10 +02:00
João Moura
d981ee5106 Merge pull request #880 from groyoh/serializer-inheritance
Inabling subclasses serializers to inherit attributes
2015-05-18 11:34:10 -03:00
groyoh
a794a06fa5 Fixed #911 2015-05-17 22:47:44 +02:00
João Moura
2c9c36e21f adding json_api as default adapter 2015-05-10 03:58:18 -03:00
João Moura
46ae776175 Merge pull request #897 from imanel/patch-1
Allow to define custom serializer for given class
2015-05-10 03:28:14 -03:00
Cristian Bica
7a62d31777 Added serializer file digest to the cache_key
Fixes #901
2015-05-06 08:37:18 +03:00
Bernard Potocki
c91b649504 Allow to define custom serializer for given class by defining #serializer_class method in serialized object's class. Resolves #515. 2015-05-03 17:47:52 +02:00
groyoh
5dcdfaaef3 Fixed a bug that appeared when json adapter serialize a nil association 2015-04-28 22:20:21 +02:00
Yohan Robert
02ffff599f Serializers now inherit attributes 2015-04-23 13:25:36 +02:00
Alexandre de Oliveira
1577969cb7 Bumps to 0.10.0.rc1 2015-04-22 03:06:06 -03:00
Alexandre de Oliveira
168c36fc7d Merge pull request #810 from joaomdmoura/fragment-cache
Adding Fragment Cache to AMS
2015-04-21 17:57:55 -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
Yohan Robert
6a0564a241 Fixed a bug that appears when a nil association is included 2015-04-03 12:50:05 +02:00
Mateo Murphy
9aebc6cb11 Fix bugs with included resources
Make sure they're cached along with the including resource and remove duplicates
2015-03-24 20:07:25 -04:00
Mateo Murphy
90c7005c79 Don't store the root as we don't need it elsewhere 2015-03-23 12:38:15 -04:00
Mateo Murphy
294d06624f Remove unused embed option 2015-03-22 20:43:56 -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
83c2854094 Rename add_linked to add_included
Better reflect generated output
2015-03-22 20:38:30 -04:00
Mateo Murphy
da86747a3e Use symbol for root in jsonapi, fix tests 2015-03-22 20:38:29 -04:00
Mateo Murphy
3ba4386bda Root is always "data" for jsonapi 2015-03-22 20:37:25 -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
Alexandre de Oliveira
0446a9714d Merge pull request #833 from lsylvester/cache-serializers-for-class
Cache serializers for class
2015-03-13 19:55:49 -03:00
Alexandre de Oliveira
af81a403e3 Passes serializer options down into associations 2015-03-11 16:37:14 -03:00
Alexandre de Oliveira
bcd3844e58 Stores passed in options in array serializers
This is supported in single serializers. This adds support for passing
options from array serializers to each serializer in it.
2015-03-11 16:14:09 -03:00
Guillermo Iguaran
73aeba4177 Merge pull request #836 from rails-api/stores-passed-in-options
Makes passed in options accessible inside serializers
2015-03-11 13:21:36 -05:00
Alexandre de Oliveira
48650ecf7e Makes passed in options accessible inside serializers
In some cases, we want to pass arguments from the controller and we want
to serializer a resource according to that. This allows serializers to
use the `options` method to retrieve whatever was passed in via
arguments.
2015-03-11 14:53:57 -03: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
Lachlan Sylvester
980d1ced81 add explicit thread_safe dependency 2015-03-11 11:15:17 +11:00
lsylvester
2b0c5ee084 clear the cache between requests 2015-03-11 09:53:47 +11:00
lsylvester
3fb560908e cache the serializers for a class 2015-03-11 09:53:47 +11:00
Guillermo Iguaran
d55f3b33b4 Merge pull request #811 from mateomurphy/scope
Reimplement serialization scope and scope_name
2015-03-10 10:16:06 -05:00
Alexandre de Oliveira
3389218fd3 Merge pull request #725 from ggordon/has_one_support
Support has_one to be compatible with 0.8.x
2015-03-05 15:08:25 -08:00
Alexandre de Oliveira
32343d4575 Merge pull request #822 from bf4/fix_has_one
Replace has_one with attribute in template
2015-03-03 11:13:29 -08:00
Gary Gordon
651b99f22e Support has_one to be compatible with 0.8.x
Update README and CHANGELOG
2015-03-01 13:47:34 -05:00
Benjamin Fleischer
79653ac733 Replace has_one with attribute in template 2015-02-25 14:20:18 -06:00
Will Jordan
f2ee544a88 Fix explicit serializer for associations 2015-02-24 16:01:43 -08:00
Mateo Murphy
2962f3f64e Reimplement serialization scope and scope_name 2015-02-09 11:24:41 -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
Robbie Pitts
90023b1af7 Make linked resource type names consistent with root names 2015-01-11 14:24:01 -05:00
Alexandre de Oliveira
6eb75af96b Merge pull request #696 from ggordon/explicit_serializer
Explicitly set serializer for associations
2015-01-06 09:57:07 -02:00
Alexandre de Oliveira
87f817943a Merge pull request #700 from arenoir/sparse_fieldsets
sparse fieldsets
2015-01-06 09:38:51 -02: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
Joakim Ekström
9768da7a70 Missing a word 2014-12-04 17:05:10 +01:00
Aaron Renoir
2ed52f96a6 merge upstream update fieldset 2014-11-13 17:45:47 -08:00
Alexandre de Oliveira
2f5626d616 Fixes nested has_many links in JSONAPI
When linked resource had has_many links, all of them would use the
association from the first resource, causing all of the items to build
`links` with the same associations.

This fixes it by iterating over the serializers, not just the
attributes array.
2014-11-13 16:38:47 -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
Gary Gordon
9f9715801a Explicitly set serializer for associations
Document specifying serializer for assocaition
2014-11-13 10:17:59 -05:00
Gary Gordon
08716d20c9 Rename attribute with :key (0.8.x compatibility) 2014-11-13 08:03:22 -05:00
Alexandre de Oliveira
ac37570bff Merge pull request #728 from rails-api/linked-resources-use-type-as-key
Use type as key for linked resources
2014-11-13 10:53:59 -02: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
7333f20285 Use type as key for linked resources
If type is `author` but the association is called `writer`, the linked
resource key should be called `authors`, e.g

    {
      ...
      linked: {
        authors: [{
          ...
        }]
      }
      ...
    }
2014-11-12 20:03:25 -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
Aaron Renoir
fc1562c04a add fields to adapter initialize function, pull in master, add tests using includes with fields 2014-11-05 18:10:37 -08:00
Aaron Renoir
c9c58e31e5 merge upstream/master 2014-11-05 16:47:15 -08:00
Gary Gordon
c84430cdad Support serializer and each_serializer in render json 2014-11-05 15:10:14 -05:00
Alexandre de Oliveira
95d122046d Merge pull request #692 from ggordon/linked_for_jsonapi_collection
Include 'linked' member for json-api collections
2014-11-04 21:59:15 -02:00
Guillermo Iguaran
f5411f045f Define as_json instead of to_json 2014-11-03 17:38:58 -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
3bba334cf8 JSON-API: Don't include linked section if associations are empty 2014-10-31 14:30:59 -05:00
Guillermo Iguaran
2d21a8e83f Add type when association name is different than objects type 2014-10-31 01:54:13 -05:00
Guillermo Iguaran
19ac139880 Handle correctly null associations
null belongs_to associations are now serialized as nil instead
of raise an error during serialization.
2014-10-30 09:35:05 -05:00
Alexandre de Oliveira
baf3db1365 Merge pull request #691 from jacob-s-son/fix-embed-option-for-associations
Fix embed option for associations
2014-10-29 18:41:59 -02:00
Aaron Renoir
be54e0bc4f remove serializer dependency from fieldset 2014-10-27 15:24:19 -07:00
Aaron Renoir
39bee48ae6 implement sparse fieldsets http://jsonapi.org/format/#fetching-sparse-fieldsets 2014-10-26 13:04:14 -07:00
Edgars Jekabsons
f9b7c74235 Renamed embed test to have "test" suffix
Fixed reference to association options
2014-10-22 15:30:44 +03:00
Guillermo Iguaran
5f198667be Fix support for custom root in JSON-API adapter 2014-10-22 04:01:12 -03:00
Guillermo Iguaran
7c030314cb Serialize ids as strings in JSON-API adapter 2014-10-15 18:56:49 -05:00
Guillermo Iguaran
557b56a50e Refactor adapters to implement support for array serialization 2014-10-15 17:35:50 -05:00
Guillermo Iguaran
3fa1116035 Include root by default in JSON-API serializers 2014-10-14 11:33:06 -05:00
Steve Klabnik
099f773a65 Merge pull request #625 from JordanFaust/feature/url-dsl
Add DSL for urls
2014-10-14 11:47:14 -04:00
Steve Klabnik
410eacc30e Merge pull request #677 from ride/embed-ids-option
Add support for embed: :ids option for in associations
2014-10-14 11:39:09 -04:00
Steve Klabnik
341cca7d8a Merge pull request #681 from quainjn/inherited-serializer
Check superclasses for Serializers
2014-10-14 11:36:02 -04:00
Jake Quain
c0166f3026 Check superclasses for Serializers 2014-10-13 17:39:54 -06:00
NullVoxPopuli
7338b62b02 add support for root keys
remove debugging gem

fix white space
2014-10-13 13:27:09 -04:00
Guillermo Iguaran
188336522f Add support for embed: :ids option for in associations 2014-10-10 16:48:55 -05:00
Ryunosuke SATO
4fe9790bfa Support Rails 4.2.0
This patch is the same fixes as #655 and #663 .
2014-10-10 00:52:59 +09:00
Jordan Faust
ad0859e262 Add DSL for urls 2014-09-01 13:44:22 -05:00
Tema Bolshakov and Dmitry Myaskovskiy
71a43a432a Pass options to associations 2014-08-29 20:16:11 +04:00
Tema Bolshakov and Dmitry Myaskovskiy
258b5953e2 clean up code 2014-08-29 19:47:46 +04:00
Tema Bolshakov and Dmitry Myaskovskiy
45a47a1c4d safe_constantize instead of rescue 2014-08-29 19:47:29 +04:00
Tema Bolshakov and Dmitry Myaskovskiy
ff37b6260d test for json adapter 2014-08-29 19:40:01 +04:00