Commit Graph

621 Commits

Author SHA1 Message Date
Lee Richmond
2145540795 Add include_data :if_sideloaded (#1931)
For JSONAPI, `include_data` currently means, "should we populate the
'data'" key for this relationship. Current options are true/false.

This adds the `:if_sideloaded` option. This means "only
populate the 'data' key when we are sideloading this relationship." This
is because 'data' is often only relevant to sideloading, and causes a
database hit.

Addresses https://github.com/rails-api/active_model_serializers/issues/1555
2016-09-25 12:57:19 -04:00
Lee Richmond
a77dfdaa85 Ensure valid jsonapi when blank relationship (#1930)
If you specify include_data false, and do not have any links for this
relationship, we would output something like:

`{ relationships: { comments: {} } }`

This is not valid jsonapi. We will now render

`{ relationships: { comments: { meta: {} } } }`

Instead.

Relevant jsonapi spec: http://jsonapi.org/format/#document-resource-object-relationships
2016-09-19 14:10:50 -04:00
Lee Richmond
11bd778f17 Merge pull request #1912 from richmolj/generator
Ensure generator picks up ApplicationSerializer
2016-09-06 12:42:37 -07:00
Lee Richmond
19b5abf66e Disable pagination links via config 2016-09-06 15:06:54 -04:00
Lee Richmond
3f16b75a68 Ensure generator picks up ApplicationSerializer
ApplicationSerializer could exist, but not be loaded. So, we check
existence by looking at the filesystem instead of defined?.

Fixes https://github.com/rails-api/active_model_serializers/issues/1890
2016-09-05 19:00:25 -04:00
Benjamin Fleischer
d34069b6ae Merge pull request #1799 from ScottKbka/patch-1
Update rendering.md
2016-08-31 16:18:47 -05:00
Benjamin Fleischer
20e394d512 Refactor Association into Field like everything else (#1897)
* Make assocations asserts easier to understand

* Refactor Association into Field like everything else

* Make assocation serializer/links/meta lazier

* Push association deeper into relationship

* Simplify association usage in relationships

* Better naming of reflection parent serializer

* Easier to read association method
2016-08-31 09:35:41 -04:00
L. Preston Sego III
7d2997b3ff Organize Serializer concerns into concerns/ (#1898) 2016-08-31 06:53:51 -04:00
Benjamin Fleischer
010243484e Merge pull request #1887 from johnnymo87/correct-comment
Correct comment
2016-08-31 01:04:36 -05:00
Benjamin Fleischer
7c4ce7bcb0 Organize Serializer concerns into concerns/ 2016-08-31 00:59:48 -05:00
Ian C. Anderson
2423ca4999 Support key transformation for Attributes adapter (#1889)
The `:attributes` adapter is the default one, but it did not support
key transformation. This was very surprising behavior, since the
"Configuration Options" page in the guides didn't mention that this
behavior was not supported by the attributes adapter.

This commit adds key transform support to the attributes adapter, and
adds documentation about the default transform for the attributes
adapter (which is `:unaltered`).

This commit also handles arrays when transforming keys, which was needed
in the case where you're serializing a collection with the Attributes
adapter. With the JSON adapter, it was always guaranteed to pass a hash
to the KeyTransform functions because of the top-level key. Since there
is no top-level key for the Attributes adapter, the return value could
be an array.
2016-08-25 15:21:27 -04:00
johnnymo87
f96d97d3e9 Correct comment 2016-08-22 13:10:29 +00:00
Scott Kobewka
7178b9de7b Documenting Adapter. Fixing typo in association.rb documentation. 2016-08-17 20:24:36 -04:00
Benjamin Fleischer
455068d455 Merge pull request #1884 from bf4/simplify_relationship
* Simplify Relationship
2016-08-17 16:18:14 -05:00
Benjamin Fleischer
abb15b9622 Simplify Relationship 2016-08-17 15:58:11 -05:00
Benjamin Fleischer
d66e272994 Correct typos 2016-08-16 18:18:58 -05:00
Yevhen Shemet
1896e5a525 ActiveModelSerializers::Model successor initialized with string keys fix (#1881) 2016-08-16 06:49:10 -04:00
Mark Abramov
5f3bdcc87c Use ActiveSupport::Cache.expand_cache_key for cache key expansions (#1878)
* Use ActiveSupport::Cache.expand_cache_key for cache key expansions
2016-08-13 16:59:36 -05:00
L. Preston Sego III
6de3f31b6e Namespace separator setting for json-api and tests (#1874)
Adds jsonapi_namespace_separator configuration

Also:

* Enable getting type from record class without serializer

Needs Followup:

- https://github.com/rails-api/active_model_serializers/pull/1874#discussion_r74607042 
- https://github.com/rails-api/active_model_serializers/pull/1874#discussion_r74607734
2016-08-12 12:54:42 -05:00
Avon
3ad2457aaf Bugfix/redefine associations on inherited serializers (#1848)
* replace reflection collection type with hash to prevent duplicated associations in some cases

* include tests

* Fix robucup offenses

* Improve test

* Remove usless requirement

* improve tests

* remove custom_options option from Post and InheritedPost serializer

* Improve tests

* update changelog

* update changelog
2016-07-17 16:25:43 -04:00
Yohan Robert
9a206a1f5d Remove nil relationships links (#1833) 2016-07-12 14:02:34 +02:00
Benjamin Fleischer
d8f3fa4bca Bump to 0.10.2 2016-07-05 13:20:24 -05:00
Benjamin Fleischer
f4e00dc65d Merge pull request #1815 from dubadub/making-rubocop-happy
Fix code-styling issues from .rubocop_todo.yml

* re: RuboCop: Bulk minor style corrections

* re: RuboCop - hash indention corrections

* re: RuboCop - replace rocket style hashes

* re: RuboCop - get rid of redundant curly braces around a hash parameter

* re: RuboCop - Align the elements of a hash literal if they span more than one line.

* re: RuboCop - Use nested module/class definition instead of compact style.

* re: RuboCop - Suppress of handling LoadError for optional dependencies

* re: RuboCop - use include_ prefix instead of has_

* re: RuboCop - Disable Style/PredicateName rule for public API methods

* re: RuboCop - Remove empty .rubocop_todo.yml

* re: RuboCop - replace rocket style hashes
2016-06-26 22:22:55 -05:00
zaaroth
bcf3358524 Ensuring read_multi works with fragment cache. (#1814)
* Ensuring read_multi works with fragment cache.
2016-06-22 22:33:02 -05:00
Alexey Dubovskoy
af959b0273 re: RuboCop - Disable Style/PredicateName rule for public API methods 2016-06-20 22:17:19 +01:00
Alexey Dubovskoy
85f417f8d2 re: RuboCop - Use nested module/class definition instead of compact style. 2016-06-20 22:15:20 +01:00
Alexey Dubovskoy
024b2d51d3 re: RuboCop - replace rocket style hashes 2016-06-20 22:14:12 +01:00
Alexey Dubovskoy
f15f6850de re: RuboCop: Bulk minor style corrections 2016-06-20 22:12:16 +01:00
Benjamin Fleischer
32a3b53892 Bump to 0.10.1 2016-06-16 09:41:10 -05:00
Onome
580492282f Fix #1759, Grape integration, adds serialization_context (#4)
* Fix #1759, Grape integration, adds serialization_context

- `serialization_context` is added in grape formatter so grape continues to render models without an explicit call to the `render` helper method
- Made it straightforward for subclasses to add other serializer options (such as `serialization_scope`).

* Updated Grape tests to include:
- paginated collections
- implicit Grape serializer (i.e. without explicit invocation of `render` helper method)

* Update Changelog with fixes.
2016-06-14 09:46:22 -05:00
Benjamin Fleischer
a7296e8a92 Fix #1759, Grape integration, adds serialization_context
- improves improves serialization_context to take options and not depend
on a `request` object.
- adds descriptive error on missing serialization_context.
- Document overriding `CollectionSerializer#paginated?`.
2016-06-14 09:45:30 -05:00
Filippos Vasilakis
be01fc8ee7 Pass fields down from constructor 2016-06-09 03:26:52 -05:00
Benjamin Fleischer
cd9689683c Merge branch 'ouvrages-exclude_nil_links' 2016-06-09 02:55:32 -05:00
Benjamin Fleischer
b599360ae3 Provide convenience serializer_class for all the self.class calls
per groyoh
https://github.com/rails-api/active_model_serializers/pull/1781#discussion_r66021340
2016-06-07 20:28:50 -05:00
Benjamin Fleischer
5375e009e2 Test caching with fragmented key
- on association, fix up assocation logic
- on attribute
2016-06-07 20:26:38 -05:00
Benjamin Fleischer
b8924157d7 Remove remaining fragmented cache class 2016-06-07 03:42:03 -05:00
Benjamin Fleischer
253205bb49 Improve Coverage 2016-06-07 01:50:03 -05:00
Benjamin Fleischer
35a7c81034 Fix up caching, especially fragment_cache 2016-06-07 00:52:05 -05:00
Benjamin Fleischer
7254d34c90 Move Serializer#serialize into Serializer#serializable_hash 2016-06-05 23:33:37 -05:00
Benjamin Fleischer
913f396bb1 Move adapter cache properties to class level (where they belong). 2016-06-04 15:00:51 -05:00
Benjamin Fleischer
516e7da8ff Move serialization logic into Serializer and CollectionSerializer 2016-06-04 14:59:51 -05:00
Benjamin Fleischer
41575e36f7 Moving Attributes#serializable_hash_for_single_resource to Serializer 2016-06-04 14:59:51 -05:00
Benjamin Fleischer
f28e486d4a Remove Attributes adapter recursion 2016-06-04 14:59:51 -05:00
Ben Mills
d900b09f8d
Upgrade to rubocop ~> 0.40.0 2016-06-01 09:21:29 -06:00
Benjamin Fleischer
385abb4ba0 Simplify Serializer#cached_attributes to take a fields argument 2016-06-01 01:02:17 -05:00
Benjamin Fleischer
ba23de686d Complete extracting to Serializer#cached_attributes 2016-06-01 01:02:13 -05:00
Benjamin Fleischer
96750b2f9a Remove unnecessary Serializer#cached_fields 2016-06-01 00:47:10 -05:00
Benjamin Fleischer
eb86663393 Remove unnecessary Adapter::Base#resource_object_for 2016-06-01 00:43:24 -05:00
Benjamin Fleischer
fcfbc0d529 Merge pull request #1764 from bf4/serializer_cleanup_2
Remove unnecessary Adapter::Base#cache_attributes
2016-06-01 00:42:54 -05:00
Benjamin Fleischer
ee518e1856 Remove unnecessary Adapter::Base#cache_attributes 2016-06-01 00:35:11 -05:00
Benjamin Fleischer
0cc87fde40 Remove unnecessary Adapter::Base#cache_check 2016-06-01 00:15:29 -05:00
Benjamin Fleischer
5a4eef6cea Remove IncludeTree; missing from #1685 2016-05-30 23:08:23 -05:00
Lucas Hosseini
f48fd2a327 Extract IncludeTree. (#1685) 2016-05-28 10:07:11 -04:00
L. Preston Sego III
7d7329bbcf Merge pull request #1426 from brigade/default-include
Add a default_include_tree config variable to ActiveModel::Serializer
2016-05-26 13:31:53 -04:00
Ben Mills
9cffc10208
Add Rails >= 5.0.beta3 JSON API params parsing (#1751)
This reverts commit 6288203277.
2016-05-26 11:19:23 -06:00
Ben Mills
6288203277 Revert "Add Rails >= 5.0.beta3 JSON API params parsing" (#1751) 2016-05-26 11:17:32 -06:00
Noah Silas
94db09b3f6 Fix RuboCop 0.40 linter errors (#1722)
These errors are breaking the build, which seems to use RuboCop 0.40 [1]
despite the Gemfile.lock pinning rubocop to 0.38.

New lints that I am updating the code style to reflect:

- Style/EmptyCaseCondition: Do not use empty case condition, instead use
  an if expression.

- Style/MultilineArrayBraceLayout: Closing array brace must be on the
  same line as the last array element when opening brace is on the same
  line as the first array element.

- Style/MultilineHashBraceLayout: Closing hash brace must be on the same
  line as the last hash element when opening brace is on the same line
  as the first hash element.

- Style/MultilineMethodCallBraceLayout: Closing method call brace must
  be on the line after the last argument when opening brace is on a
  separate line from the first argument.

[1] https://github.com/bbatsov/rubocop/releases/tag/v0.40.0
2016-05-26 12:58:05 -04:00
Benjamin Fleischer
8a3196d920 Improve jsonapi mime type registration for Rails 5 (#1747) 2016-05-26 10:55:12 -06:00
Ben Woosley
8c18d18cdb Add default_includes configuration
This is useful to set application-wide default behavior - e.g. in
previous versions of AMS the default behavior was to serialize the
full object graph by default - equivalent to the '**' include tree.

Currently just the global setting, but I think this could also work
on a per-serializer basis, with more attention.
2016-05-26 00:16:14 +00:00
Yohan Robert
a701777bd5 Prevent loading association when include_data is set to false (#1710)
This should fix #1707.
2016-05-24 20:46:22 -04:00
Ben Mills
b6d4ec03de
Bump to v0.10.0 2016-05-17 12:49:37 -06:00
cgmckeever
bbed12864d adds polymorphic option to association definition which includes association type in serializer
regen gemlock

regen gemlock

better variable naming

rubocop fixes

adds to changelog

adds empty relationship and has_many polymorph tests

indent

test cleaning

-rubocop

rubocop

rubocop

rubocop

changelog

remove silly .DS

fix roque failure

fix
2016-05-17 12:30:59 -05:00
Benjamin Fleischer
6c321cd862 Assert Schema (#1677)
* Assert Schema

* Fix regression from #1695 where JSONAPI renders empty meta

* Add changelog
2016-05-17 13:22:38 -04:00
Benjamin Fleischer
9f59398f2b Merge pull request #1732 from remear/jsonapi-renderer
Include ControllerSupport on ActiveSupport on_load action_controller
2016-05-17 12:10:17 -05:00
Ben Mills
7356071ea0
Include ControllerSupport on ActiveSupport on_load action_controller 2016-05-17 09:39:30 -06:00
Benjamin Fleischer
93cad825b7 Include actual exception message with custom exceptions 2016-05-17 10:32:53 -05:00
Benjamin Fleischer
d0d7af470c Test::Schema exceptions should be Minitest::Assertions 2016-05-17 10:31:35 -05:00
Benjamin Fleischer
05fd59644d Setting the content_type sets the response Content-Type
Otherwise we have two headers, 'Content-Type' and 'CONTENT_TYPE'.
I don't know when Rails decides to use one or the other.
2016-05-01 23:30:52 -05:00
Fumiaki MATSUSHIMA
aa087a22b5 String/Lambda support for conditional attributes/associations 2016-04-26 21:37:25 +09:00
Nader Akhnoukh
37ca0c1f6c Support pagination link for Kaminari when no data is returned 2016-04-22 11:04:40 -06:00
Lucas Hosseini
e804d37924 Meta no longer handled in Base adapter. 2016-04-21 18:25:17 +02:00
Benjamin Fleischer
7485c8487e Merge pull request #1687 from bf4/lazyify_calculating_caller_digest
Lazify calculating caller file digest until used
2016-04-18 16:09:24 -05:00
Benjamin Fleischer
1d24c9708a Lazify calculating caller file digest until used 2016-04-18 12:04:31 -05:00
Benjamin Fleischer
b7e2bc06ed Memoize resource relationships
bin/bench_regression
  "version": "0.10.0.rc5",
  "rails_version": "4.2.6",
  "benchmark_run[environment]": "2.2.2p95",

perf/only_calc_associations_once
"commit_hash": "1e7c428",
caching on: caching serializers: gc off 741.7702402782281/ips; 1355 objects
caching on: non-caching serializers: gc off 712.3752615532874/ips; 1257 objects
caching off: caching serializers: gc off 706.0789199312495/ips; 1355 objects
caching off: non-caching serializers: gc off 751.5310710635379/ips; 1257 objects

master
"commit_hash": "1033b711c7d7c231bb5b832e7dfe7f99389f22c4"
caching on: caching serializers: gc off 567.7959835633892/ips; 1803 objects
caching on: non-caching serializers: gc off 776.4929551133658/ips; 1257 objects
caching off: caching serializers: gc off 538.046851190591/ips; 1803 objects
caching off: non-caching serializers: gc off 738.5596630209004/ips; 1257 objects
2016-04-18 11:11:13 -05:00
Benjamin Fleischer
cc80eba9c9 Refactor fragment cache logic some more 2016-04-13 01:08:10 -05:00
Benjamin Fleischer
06636b25b2 Begin simplifying fragment cache 2016-04-13 01:08:09 -05:00
Benjamin Fleischer
1e10c20ac0 Consolidate and simplify caching code 2016-04-13 01:08:09 -05:00
Benjamin Fleischer
929a5d0a51 Restrict serializable_hash to accepted options, only for tests 2016-04-13 00:40:57 -05:00
Benjamin Fleischer
aad7779a3f Restrict serializable_hash to accepted options (#1647)
Restrict tests/impl from passing AMS options into serializable_hash
2016-04-11 13:10:18 -05:00
Michael Witrant
87bffef945 Exclude links without any data 2016-04-07 19:07:59 +02:00
Benjamin Fleischer
01edebd37c Remove untested lint conditions 2016-04-04 20:15:30 -05:00
L. Preston Sego III
74d172b4f6 Merge pull request #1662 from remear/drop-old-versions
Drop support for Rails 4.0 and Ruby 2.0.0
2016-04-04 18:10:01 -04:00
Benjamin Fleischer
e580487de3 Bump to v0.10.0.rc5 🎂 2016-04-04 16:50:31 -05:00
Benjamin Fleischer
b58f73ced1 Merge branch 'andreaseger-fix_missing_require_of_active_support_json' 2016-04-04 13:54:12 -05:00
Ben Mills
89e0a39fbb Drop support for Rails 4.0 and Ruby 2.0.0 2016-04-04 11:41:35 -06:00
Benjamin Fleischer
6370e5c72a Fix read_attribute_for_serialization not seeing parent serializer methods
Fixes #1653, #1658, #1660

Define "scope_name" on instance singleton, not all instances
2016-04-04 12:29:26 -05:00
Ben Mills
3498647d1a Apply key transforms to keys referenced in values 2016-04-04 10:16:04 -06:00
Andreas Eger
af2b38c43b add require for active_support/json to fix #1656 2016-04-04 09:26:48 +02:00
Benjamin Fleischer
22f88ef379 Merge pull request #1651 from NullVoxPopuli/deserialization-error-with-no-attributes
Fix for Deserialization erroring when a relationship is null in the json api document.
2016-04-03 14:32:24 -05:00
NullVoxPopuli
5be33afbfb Fix deserialization of nil relationships
failing test

use try for when the assoc_data is possibly nil

rubocop test/action_controller/json_api/deserialization_test.rb -a

attempt to work on rails-master

account for rails/master having  instead of nil for assoc_data

added changelog
2016-04-01 16:06:35 -04:00
Benjamin Fleischer
7acbb765bd Merge pull request #1650 from bf4/fix_serialization_scope
[FIX] serialization scope options
2016-04-01 14:17:19 -05:00
Benjamin Fleischer
a065bc28d1 Fix serialization scope options 2016-04-01 05:43:46 -05:00
Benjamin Fleischer
21b2eff2ab Improvements from Rails plugin template 2016-04-01 05:39:03 -05:00
kevintyll
16a3f93ce9 Include adapter in cache key
Confirm caching attributes with different key json_api vs. attributes adapter

Adapted from @kevintyll's original test
https://github.com/rails-api/active_model_serializers/pull/1644#issuecomment-204147094
2016-04-01 00:52:28 -05:00
Benjamin Fleischer
4ba4c298ec Prefer object.cache_key when available. 2016-03-31 22:29:13 -05:00
kevintyll
ab6bd600e3 When caching, return the object's cache_key up front if it's defined.
This will prevent objects PORO objects that don't have updated_at defined, from throwing an error.

Not as big a deal now that PORO objects can inherit ActiveModelSerializers::Model, but still necessary if it's not inherited for whatever reason.

Add the Adapter type to the cache key.

This prevents incorrect results when the same object is serialized with different adapters.

BF:

Cherry-pick of
040a97b9e9
which was a squash of
f89ed71058

from pr 1346
2016-03-31 22:29:13 -05:00
Ben Mills
b73b780b79 Merge pull request #1629 from lawitschka/key-transform-on-deserialization
Properly deserialize dasherized keys
2016-03-31 13:30:02 -06:00
Nahuel Cuesta Luengo
32b85a0aae Make cache_store reference explicit
This avoids an issue when the base class for application's controllers inherit from `ActionController::API` instead of `ActionController::Base`.
2016-03-30 21:33:09 -03:00
Benjamin Fleischer
fa7b3afbfd Prefer explicitly yielding the serializer, per groyoh 2016-03-30 14:01:28 -05:00
Benjamin Fleischer
ae6805eacd Add serializer to association block context 2016-03-30 11:03:38 -05:00