Commit Graph

513 Commits

Author SHA1 Message Date
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
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
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
Yohan Robert
fb62fb39b2 Fix caching issue happening with memory_store
It seems that fecthing from memory_store returns a reference to the
object and not a copy. Since the Attributes adapter applies #merge! on
the Hash that is returned from the memory_store, the value in the cache
is also modified.
2016-03-30 09:53:21 -05:00
Benjamin Fleischer
cdab6f2b8a The cache store needs to be the actually store, not e.g. :memory_store
Status quo in test app:
In Rails
    ActionController::Base.cache_store = :memory_store
and then AMS railtie does:
  ActiveModelSerializers.config.cache_store = config.action_controller.cache_store
then, in the Railtie
1. ActiveSupport.on_load(:action_controller) fires
  - ActiveModelSerializers.config.cache_store #=> nil
  - ActionController::Base.cache_store        #=> #<ActiveSupport::Cache::FileStore:0x007fe319256760...]
2. After set_configs fires
  - ActiveModelSerializers.config.cache_store #+> #<ActiveSupport::Cache::FileStore:0x007fe319256760 ,
3. Tests pass, but notice that we're using the FileStore, not memory store

When we change the config to the test app:
  ActionController::Base.cache_store = :memory_store
  config = Rails.configuration
  config.action_controller.cache_store = :memory_store
then, in the Railtie:
1. ActiveSupport.on_load(:action_controller) fires
  - ActiveModelSerializers.config.cache_store #=> nil
  - ActionController::Base.cache_store        #=> #ActiveSupport::Cache::MemoryStore entries=0, size=0, options={}>]
2. After set_configs fires
  - ActiveModelSerializers.config.cache_store #=> :memory_store
3. And we get a lot of failures:
  NoMethodError: undefined method `fetch' for :memory_store:Symbol

So, we see that when we set the ActionController::Base.cache_store
directly in our test app, we could set
ActiveModelSerializers.config.cache_store in the :action_controller load
hook, but that would never use the Rails config.

To fix the Rails config, we change the config to the test app:
  config = Rails.configuration
  config.action_controller.cache_store = :memory_store
and then AMS railtie does:
  ActiveModelSerializers.config.cache_store = ActiveSupport::Cache.lookup_store(config.action_controller.cache_store
  ActiveSupport.on_load(:action_controller) do
    ::ActiveModelSerializers.config.cache_store = cache_store
  end
then
1. After set_configs fires
  - ActiveModelSerializers.config.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207113611740
2. ActiveSupport.on_load(:action_controller) fires
  - ActionController::Base.cache_store        #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207106279660
  - ActiveModelSerializers.config.cache_store #=> <#ActiveSupport::Cache::MemoryStore, object_id 70207106279660
    (notice the object_id changed)
3. And we get a failure:

  1) Failure:
  ActiveModelSerializers::CacheTest#test_associations_cache_when_updated
  [active_model_serializers/test/cache_test.rb:141]:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -{:id=>"post", :title=>"New Post", :body=>"Body"}
  +{:id=>"post", :title=>"New Post", :body=>"Body", :comments=>[{:id=>2, :body=>"ZOMG A NEW COMMENT"}], :blog=>{:id=>999, :name=>"Custom blog"}, :author=>{:id=>"author", :name=>"Joao M. D. Moura"}}

If we take out the on_load(:action_controller) hook, we get a ton of
failures.  So clearly, our code expects the controller cache to be the
same as the serializer cache.

So, we make sure we use an on_load(:action_controller) hook that runs
after set_configs

And look at the test and see it is filled with direct calls to ActionController::Base.cache_store

    assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
    assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))

But that's not a problem in this case, since they're the same object.

For now, let's remove the :memory_store setting and use the default FileStore
2016-03-30 09:52:29 -05:00
Andre Schweighofer
a1ae7dc0d9 Update deprecation warning with correct namespace 2016-03-30 15:51:13 +02:00
Yohan Robert
21cb896802 Move SerializableResource to ActiveModelSerializers namespace
Ref. https://github.com/rails-api/active_model_serializers/pull/1310
2016-03-30 11:33:04 +02:00
Benjamin Fleischer
355e0f6a37 Merge branch 'lserman-master'
Followup needed:
- Update code comments https://github.com/rails-api/active_model_serializers/pull/1622#discussion_r57750471
- Move test class into test scope https://github.com/rails-api/active_model_serializers/pull/1622#discussion_r57659150
2016-03-29 21:50:27 -05:00
Moritz Lawitschka
afe786d19a Properly deserialize dasherized keys
The JSON API adapater dasherizes every key, but the deserializer left the keys
unaltered. Thus, the client had to send underscored keys in the request body in
order for Rails to properly match sent values to model attributes.

This commit adds automatic key transformation on deserialization. Per default the
deserializer transforms the keys to underscore, but this behaviour can also be
changed by including `key_transform` in the deserializer options.
2016-03-29 22:46:01 +02:00
Benjamin Fleischer
c7b2916f37 Merge pull request #1547 from bf4/jsonapi_renderer
Basic Jsonapi Renderer registration
2016-03-28 22:23:40 -05:00
Benjamin Fleischer
d364c4f188 Spike Jsonapi Renderer registration 2016-03-28 22:04:16 -05:00