Commit Graph

603 Commits

Author SHA1 Message Date
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
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
Benjamin Fleischer
ec5dc497b0 Handle render.ams with nil serializer or adapter 2016-03-28 21:33:23 -05:00
Benjamin Fleischer
84197e4dad SerializableResource handles no serializer like controller 2016-03-28 20:04:45 -05:00
Logan Serman
d0389ca765 Fix fragment caching inherited serializers to use distinct per-serializer caches. 2016-03-28 16:26:51 -05:00
Yohan Robert
5af7d96294 Merge pull request #1618 from bf4/RomanKapitonov-master
Empty collection root key from explicit serializer option
2016-03-27 18:38:14 +02:00
Benjamin Fleischer
a74d174420 Include Serializer._type in collection serializer json_key cascade 2016-03-27 10:55:31 -05:00
Roman Kapitonov
2dd0c33461 [FIX] Fetch json key from item serializer if empty collection is passed to collection serializer and each_searializer is specified. 2016-03-27 10:52:25 -05:00
Benjamin Fleischer
2627740806 Silence @_routes warnings 2016-03-27 10:45:57 -05:00
Benjamin Fleischer
fb06a462bb Fix warnings 2016-03-25 10:28:13 -05:00
Benjamin Fleischer
dd60a371ae Simplify caching of value of config.perform_caching 2016-03-24 22:22:19 -05:00
Benjamin Fleischer
c3c69a607a Separate enabling of caching and setting the cache store 2016-03-24 22:20:42 -05:00
Benjamin Fleischer
9953d7abe0 Trigger callback to set serializer#_cache when controller loaded 2016-03-24 22:20:42 -05:00
Ben Mills
c533d1a7fe Provide key case translation 2016-03-15 12:21:10 -06:00
Ben Mills
e8286b6138 Omit meta when blank 2016-03-14 08:36:09 -06:00
Benjamin Fleischer
eda8ff1737 Move serializer caching from adapter 2016-03-13 19:57:59 -05:00
Benjamin Fleischer
fcd394ab09 Merge pull request #1482 from bf4/jsonapify
Document JSON API implementation defs and progress in class
2016-03-13 01:18:31 -06:00
Benjamin Fleischer
bdb997b1d9 Document JSON API implementation defs and progress in class 2016-03-13 00:30:06 -06:00
Benjamin Fleischer
b169ed387b Make serializers serializable, step 1. 2016-03-13 00:26:15 -06:00
Benjamin Fleischer
85658c0230 Add better serialization_scope tests; uncover bug 2016-03-12 19:59:43 -06:00
Ben Morrall
b5dd90c8f9 Fixed pagination issue with last page size 2016-03-10 09:30:29 +11:00
Yohan Robert
efb09051ea Refactor fragment cache methods
Removed extra calls to constantize and DRY'd the code.
2016-03-08 23:51:10 +01:00
Benjamin Fleischer
68d5233b31 Address rubocop warnings 2016-03-08 22:32:34 +01:00
Benjamin Fleischer
a26d3e4425 Rubocop autocorrect 2016-03-08 22:32:34 +01:00
Ben Mills
cc10928472 Provide Rails url_helpers via SerializationContext 2016-03-07 10:40:34 -07:00
Yohan Robert
8dfbc4818d Simplify adapter deprecation and delegation
The Adapter module was refactored a bit to use Active Support delegation
and remove duplicated code.
The CHANGELOG was also added.
2016-03-07 15:09:09 +01:00
Benjamin Fleischer
b50195fde7 Add a deprecation DSL 2016-03-07 01:07:06 -06:00
Benjamin Fleischer
6b4c8df6fb Clean up test deprecation warnings 2016-03-06 23:35:22 -06:00
Benjamin Fleischer
5d7a1a4889 Remove the last of ApiObjects 2016-03-06 23:24:53 -06:00
Yohan Robert
dd94fe2163 Follow up to #1535
- The removed classes and modules were added back with deprecation
  warning and deprecation test were added for them.
- One test was renamed because it contained `__`.
- Some tests were refactored.
- The ActiveModelSerializers::Deserialization module is now called
  Adapter instead of ActiveModelSerializers::Adapter.
- The changelog was added for #1535
2016-03-06 23:15:39 -06:00
NullVoxPopuli
d03db81070 add an extra format token to the primary data string so that JRuby doesn't break 2016-03-06 20:05:25 -06:00
Benjamin Fleischer
e6ae34b84c Update documentation with Yard links 2016-03-06 20:05:25 -06:00
Benjamin Fleischer
3d986377b6 Collapse JSON API success/failure documents in one adapter
Idea per remear (Ben Mills) in the slack:
https://amserializers.slack.com/archives/general/p1455140474000171

remear:

    just so i understand, the adapter in `render json: resource, status: 422, adapter: 'json_api/error',
    serializer: ActiveModel::Serializer::ErrorSerializer` is a different one than, say what i’ve
    specified in a base serializer with `ActiveModel::Serializer.config.adapter = :json_api`. correct?

    and a followup question of, why not same adapter but different serializer?

me:

   With the way the code is written now, it might be possible to not require a special jsonapi adapter.
   However, the behavior is pretty different from the jsonapi adapter.

   this first draft of the PR had it automatically set the adapter if there were errors.  since that
   requires more discussion, I took a step back and made it explicit for this PR

   If I were to re-use the json api adapter and remove the error one, it think the serializable hash
   method would look like

   ```
   def serializable_hash(options = nil)
     return { errors: JsonApi::Error.collection_errors } if serializer.is_a?(ErrorsSerializer)
     return { errors: JsonApi::Error.resource_errors(serializer) } if serializer.is_a?(ErrorSerializer)
     options ||= {}
   ```

   I suppose it could be something more duckish like

   ```
   def serializable_hash(options = nil)
     if serializer.errors? # object.errors.any? || object.any? {|o| o.errors.any? }
       JsonApi::Error.new(serializer).serializable_hash
     else
       # etc
   ```
2016-03-06 12:03:17 -06:00
Benjamin Fleischer
96107c56aa Require explicit adapter/serializer to render JSON API errors
- Separate collection errors from resource errors in adapter
- Refactor to ErrorsSerializer; first-class json error methods
- DOCS
- Rails 4.0 requires assert exact exception class, boo
2016-03-06 12:03:17 -06:00
Benjamin Fleischer
dfe162638c Generalize detection of serializable resource with errors 2016-03-06 12:03:17 -06:00
Benjamin Fleischer
0ba944dabf RFC: Json Api Errors (WIP)
- ActiveModelSerializers::JsonPointer
- ActiveModel::Serializer::Adapter::JsonApi::Error
- ActiveModel::Serializer::Adapter::JsonApi::Error.attributes
- Fix rubocop config
2016-03-06 12:03:14 -06:00
Benjamin Fleischer
e35390623d Improve adapter test coverage per groyoh 2016-02-25 23:08:20 -06:00
Benjamin Fleischer
25c9df0b97 Merge branch 'master' into domitian-move-namespace-of-adapter-to-active-model-serializers
Conflicts:
	CHANGELOG.md
	lib/active_model/serializer/adapter/attributes.rb
	lib/active_model/serializer/adapter/cached_serializer.rb
	lib/active_model/serializer/adapter/fragment_cache.rb
	lib/active_model/serializer/adapter/json_api.rb
	lib/active_model/serializer/adapter/json_api/link.rb
	test/adapter/fragment_cache_test.rb
	test/adapter/json_api/links_test.rb
	test/adapter/json_api/resource_type_config_test.rb
2016-02-23 23:21:49 -06:00
bobba surendranath chowdary
252f9c4ae9 Moved the adapter and adapter folder to active_model_serializers folder and changed the module namespace
Changed the namespace in adapters and folder to active_model_serializers from active_model::serializer

Changed namespace of adapters in serializers and other folders

Moved adapter_for_test file to active_model_serializers folder and changed namespace of adapter inside the test file

Require ActiveSupport's string/inflections

We depend on string/inflections to define String#underscore.

Refactor JsonApi adapter to avoid redundant computations.

Update readme.md to link to v0.10.0.rc4

changed namespace of adapter folder testcases

Changed all namespaces of adapter under active_moder_serializers

Namespaced IncludeTree which is from serializer module, so needed to namespace it properly

Fixed wrong namsepacing of fieldset

namespace change in deserializer json_api

Fixed the namespace for collection serializer when used inside adapter, changed namespace for adapter to new namespace which I had forgotten previously

Modified logging test and adapter test cases to make the testcases pass

Changed the yardoc links,as old links are not taking to documentation pages,proper links for 0.10,0.9 and 0.8 in rubydoc

Rubocop errors are fixed by underscore naming unused variables

Moved the require of adapter to serializable resource

Remoeved adapter dependency inside serializer and added warning to Serializer::adapter method

Fixed frament cache test which is calling Serializer.adapter

Changed the name of lookup_adapter_from_config to configured_adapter

Changed the docs which will show the new namespace of adapters

Rubocop fix
2016-02-23 21:49:58 -06:00
Yohan Robert
727d7631ae Add symbol support for ActiveModel::Serializer.type method
The ActiveModel::Serializer.type method now accepts symbol as paremeter:
class AuthorSerializer < ActiveModel::Serializer
  type :profile
end
The test file for the type was also refactored.
2016-02-19 17:05:13 +01:00
Yohan Robert
3cbc0541c1 Fix bug displaying nil for relationship link href
When using the relationship link with a block, calling "meta" without "href", e.g.
has_one :bio do
  link :related do
    meta id: 1
  end
end

results in in a nil "href", e.g.
{ links: { posts: { related: { href: nil, meta: { id: 1 } }  } } }.
According to JSONAPI, we should be able to use meta without href
(http://jsonapi.org/format/#document-links).
2016-02-15 09:26:53 +01:00
Benjamin Fleischer
2b673634d9 Merge branch 'groyoh-association-blocks' 2016-02-12 14:52:21 -06:00
Benjamin Fleischer
54aa6aa499 Merge branch 'LcpMarvel-support_read_multi'
Followup:
- Changelog
2016-02-11 00:48:48 -06:00
Yohan Robert
b1fd43303c Fix relationship behavior when using block
When using the relationships DSL with a block e.g.
has_one :bio do
  link :self, "some_link"
end
the "data" field would be rendered with a nil value even though the bio
is not nil. This happened because the block return value was set to nil
but used as a value for the "data" field.
2016-02-10 21:04:37 +01:00
Yohan Robert
2c4193851b Follow up to #1454
PR #1454 was merged with some missing fixes. All these fixes are
addressed by this commit:
- Rename ActiveModel::Serializer::Adapter::JsonApi::Association to
ActiveModel::Serializer::Adapter::JsonApi::Relationship
- Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to
ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module
- Add unit test for
ActiveModel::Serializer::Adapter::JsonApi::Relationship
- Add unit test for
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
2016-02-10 12:57:54 +01:00
Benjamin Fleischer
a40998273d Merge branch 'CheckMateIO-bugfix/fagment-caching-breaks-type-setting'
Squashed commits that were done in other PR

Follow up needs:
- Changelog
2016-02-09 22:35:45 -06:00
Brian McManus
d67f7da114 Preserve the serializer type when fragment caching
We were not previously cloning the type setting into the dynamically
generated cached/non-cached serializers for a given fragment-cached
serializer. This led to the type generated for JsonApi having the wrong
value when fragment caching is enabled by adding either :except or :only
options to cache.

This pulls the type setting from the fragment-cached serializer forward
onto the dynamic caching classes so it is preserved in the output.
2016-02-09 22:35:32 -06:00
Benjamin Fleischer
4974295ee0 Merge branch 'CheckMateIO-bugfix/fix-fragment-cached-check'
Remaining issues:
- Changelog
- Test
2016-02-09 22:01:01 -06:00
Benjamin Fleischer
50950d9533 Merge branch 'beauby-resource-level-meta'
Followup concerns:
- https://github.com/rails-api/active_model_serializers/pull/1340/files#r47451387
- https://github.com/rails-api/active_model_serializers/pull/1340/files#r48116963
- https://github.com/rails-api/active_model_serializers/pull/1340#discussion_r47451387
- https://github.com/rails-api/active_model_serializers/pull/1340#issuecomment-164306882
- https://github.com/rails-api/active_model_serializers/pull/1340#issuecomment-166202978
- https://github.com/rails-api/active_model_serializers/pull/1340#issuecomment-173028896
2016-02-08 18:00:51 -06:00
Benjamin Fleischer
dcbe4ef9e2 Rubocop autocorrect indentation 2016-02-08 17:39:45 -06:00
Benjamin Fleischer
e51597480a Merge branch 'beauby-association-blocks'
Has Rubocop failures to be fixed in next commit.
2016-02-08 17:37:31 -06:00
bobba surendranath chowdary
68f09e59c4 Fixed a documentation error regarding adapter key constant, added tests
for SerializableResource::use_adapter?
2016-02-08 15:27:56 +05:30
Benjamin Fleischer
72c2c9f0d7 Merge pull request #1447 from beauby/jsonapi-refactor-2016
[PERF] Refactor JsonApi adapter to avoid redundant computations.
2016-02-02 10:10:47 -06:00
Nate Sullivan
3c1fe0fd0f Require ActiveSupport's string/inflections
We depend on string/inflections to define String#underscore.
2016-01-31 13:45:58 -08:00
Brian McManus
3a092c9b4b Fixed fragment_cached? method to check if caching
I noticed that fragment caching does not actually check if caching is
enabled as it seemingly should.

The way CachedSerializer#fragment_cached? worked previously would return
true even in an environment where caching was disabled as defined by
`ActiveModelSerializers.config.perform_caching`.

Added check for `_cache` like in the `cached?` method before checking
whether `_cache_only` or `_cache_except` is set.

There were no existing tests for any of these methods but it's a pretty
trivial change.
2016-01-28 13:47:17 -08:00
João M. D. Moura
58ff7535b7 releaseing new version RC4 2016-01-27 10:03:01 -05:00
Lucas Hosseini
790efee9c3 Merge pull request #1467 from dgynn/pr_configure_generators
Ensure that generators get configured correctly
2016-01-26 18:16:13 +01:00
Alexandre de Oliveira
ef58efdf73 Merge pull request #1418 from brigade/collection-pluralize
Don't pluralize the CollectionSerializer#root for #json_key
2016-01-25 19:46:47 -02:00
Dave Gynn
b1b3e9fb6a ensure that generators get configured correctly 2016-01-25 09:01:25 -08:00
Lucas Hosseini
061f1c0f59 Add support for relationship-level links and meta. 2016-01-21 02:36:54 +01:00
Lucas Hosseini
701404f757 Clean up meta handling. 2016-01-20 01:00:14 +01:00
Lucas Hosseini
0bd5c6584f Add support for resource-level meta. 2016-01-20 00:49:13 +01:00
Lucas Hosseini
20ddc5e102 Refactor JsonApi adapter to avoid redundant computations. 2016-01-20 00:31:22 +01:00
Benjamin Fleischer
8981683b9f Merge pull request #1352 from bf4/railties
Fix generators (@dgynn); load Railtie only with Rails, ensures caching configured
2016-01-18 23:24:40 -05:00
Dave Gynn
6713864b64 combine config initializers and update comments
this also changes the action_controller load hook to not trigger loading of the ActionController::Base
2016-01-16 14:08:32 -08:00
Lucas Hosseini
30d8414cce Add support for dynamic string-links in JsonApi adapter. 2016-01-15 14:36:42 +01:00
Dave Gynn
ea8d463555 use action_controller configuration options in initializers
this uses the configuration settings rather than calling ActionController::Base to get the configured values.
after the "action_controller.set_configs" initializer has run, the configuration option holds the value Base will get when it loads.
2016-01-15 01:58:14 -06:00
Benjamin Fleischer
509221c1e0 Only call railtie when Rails is defined; assume controller loaded
Isolated Testing

- Rake test inspired by https://github.com/rails/rails/blob/v5.0.0.beta1/activejob/Rakefile
- Isolated unit inspired by
  - https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
  - https://github.com/rails/rails/blob/v5.0.0.beta1/activemodel/test/cases/railtie_test.rb

Misc

- Turns out `mattr_accessor(:logger) {
  ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }`
  was always nil until the Railtie was loaded, since mattr_accessor
  block defaults don't really work on modules, but on the classes that
  include them.
- Commented on important on Rails being required first for caching to
  work.
- In isolated tests, `active_support/core_ext/object/with_options` is required.
2016-01-15 01:54:07 -06:00
Benjamin Fleischer
fe015d17f2 Fix load-order issues 2016-01-15 01:54:07 -06:00
Dave Gynn
94db22c1e0 Only load generators when needed
- use hook_for to hook in the serializer and remove load_generators
- move generators so they can be found by rails
- move to_prepare block to railtie config

This commit improves the way the generators are loaded and how
they extend the resource generator.

* The initializer block has been changed to a `generator` block which is only executed when generators are needed.
* The call to `app.load_generators` has been removed. There is no need to load *all* generators.
* The `resource_override.rb` has been changed to use `hook_for` to extend the resource generator.
* The directory for the generators has been moved to match the way Rails looks to load generators.

With `hook_for` it would now be possible for a user to pass `--no-serializer` to skip that option.
The `--serialize` option also now shows up in the generator help with `rails g resource --help`.

These changes follow the way the Draper gem extends the `controller` generator.
2016-01-15 01:52:27 -06:00
Benjamin Fleischer
0c2153ac5e Collect more Rails initialization code in the Railtie 2016-01-15 01:52:27 -06:00
Benjamin Fleischer
87ca408ead Merge pull request #1425 from beauby/extract-serializer-modules
[CLEANUP] Extract links and type-related methods to their own module.
2016-01-15 00:56:40 -06:00
Mauro George
5058694f4a Create assert_response_schema test helper
It is a common pattern to use JSON Schema to validate a API response[1], [2]
and [3].

This patch creates the `assert_response_schema` test helper that helps people do
this kind of validation easily on the controller tests.

[1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher
[2]: https://github.com/sharethrough/json-schema-rspec
[3]: https://github.com/rails-api/active_model_serializers/issues/1011#issuecomment-127608121
2016-01-15 00:45:56 -06:00
Benjamin Fleischer
9779185d57 Restore test-local subscriber teardown
This commit revises 0ce4ad35a12ffd858f6a5d7bbeb48fc1e2cfac92
`Remove unused/unusable unsubscribe since we don't want to unsubscribe`

Looking at Rails implementation of assert_template
which was likely the inspiration for assert_serializer:

f756b33c13/lib/rails/controller/testing/template_assertions.rb

Ref:

- https://github.com/rails-api/active_model_serializers/pull/596
- https://github.com/rails-api/active_model_serializers/pull/620
- https://github.com/rails-api/active_model_serializers/issues/616
2016-01-13 21:47:08 -06:00
Benjamin Fleischer
28f314aef2 Surface logging event name for re-use 2016-01-13 21:47:08 -06:00
Benjamin Fleischer
f59431439d Remove unused/unusable unsubscribe since we don't want to unsubscribe
In 0.9 (which this implementation is based on), the instrumentation
was `!serialize.active_model_serializers`.

https://github.com/rails-api/active_model_serializers/pull/596/

The '!' in the event name meant the event wasn't meant for
production.

https://github.com/rails/rails/pull/10446/files#r4075679

Since we intend the event for production and have a log subscriber,
if we unsubscribe from `render.active_model_serializers`, we'll
break other tests that are relying on that event being subscribed.
2016-01-13 20:54:22 -06:00
Benjamin Fleischer
ef09c9043f Small perf, readability refactor to Test::Serializer 2016-01-13 20:54:22 -06:00
Benjamin Fleischer
37a6d2b245 Be consisent in usage of ActiveSupport::Autoload 2016-01-13 20:54:22 -06:00
Mauro George
c0b99c980c Bring back assert_serializer for controller testing
The `assert_serializer` test helper was added in 0.9.0.apha1[1],
and was not included in 0.10.

This patch brings back the `assert_serializer` test helper. This is the last
revision[2] that has the helper. The original helper was used as base.

[1]: https://github.com/rails-api/active_model_serializers/pull/596
[2]: 610aeb2e92

- Create the AssertSerializer

- Use the Test namespace

- Make the tests pass on the Rails master
  - Rails 5 does not include `assert_template` but we need this on the tests of
the helper.
  - This add the `rails-controller-testing` to keep support on `assert_template`.

- Only load test helpers in the test environment
2016-01-13 20:54:22 -06:00
Lucas Hosseini
2696557650 Replace Field#included? with Field#excluded?. 2016-01-13 06:20:05 +01:00
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