Commit Graph

435 Commits

Author SHA1 Message Date
Chris Nixon
7fbf7e536d Use condition_type in case statement for included?. 2016-01-13 06:20:05 +01:00
Lucas Hosseini
40ed7b57bd Factor out ancestor class Field of Attribute and Reflection. 2016-01-13 06:20:04 +01:00
Lucas Hosseini
6860318133 Add support for if/unless on associations. 2016-01-13 06:19:47 +01:00
Lucas Hosseini
a502b5d38b Add support for if/unless on attributes. 2016-01-13 06:19:28 +01:00
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