Commit Graph

105 Commits

Author SHA1 Message Date
Godfrey Chan
6581f8ce41 Fix default_serializer_options, closes #112 & #113
Allow options such as :serializer, :scope, :root etc to be set via
default_serializer_options and allow the inline options to override such
defaults.
2012-12-03 03:09:58 -08:00
Francesco Rodriguez
6be6599bb7 Fix failing tests on Rails master 2012-11-25 14:56:58 -05:00
Jean Boussier
06e8218193 Throw a descriptive error if attempting to serialize an array with Serializer not ArraySerializer #143 2012-11-22 23:27:18 -05:00
Jo Liss
2fc083a1fe Merge pull request #117 from joliss/computed-attributes
Make schema not crash on computed attributes & associations
2012-11-22 08:31:18 -08:00
Tee Parham
2cb7a41780 move ArraySerializer tests into separate file 2012-11-20 10:58:16 -07:00
Tee Parham
f47a10cadc move test fakes to separate file 2012-11-20 10:44:23 -07:00
Samnang Chhun
96ce310595 Add alias_method ActiveModel::Serializer.root= to be consistency with ActiveModel::ArraySerializer 2012-11-20 00:29:00 +07:00
Jo Liss
538b0c0916 Make schema not crash on computed associations
We pick nil here as well.
2012-10-31 20:07:08 +01:00
Jo Liss
6281a9149e Make schema not crash on computed attributes
We do not know the type for computed attributes, so we pick nil.

Perhaps at some point we might add a :type option for attributes (or
not), but in any case it's important to not crash when there are
computed attributes.
2012-10-31 20:07:08 +01:00
Jo Liss
ee3cec3d0c When objects are sideloaded multiple times, serialize them only once
To achieve this, we make the following change when sideloading: Instead
of serializing associations and discarding duplicate *hashes*, we
memorize the *objects* (records) that we have already serialized, and
only serialize those that are new.

This change is mostly transparent, and brings down serialization time
from 3.1 seconds to 1.0 seconds on my set of sample data.

There is one change in the behavior: If you sideload the same object
multiple times, and it yields different hashes, like so:

    embed :ids, include: true
    has_many :comments
    has_many :recent_comments, root: comments, serializer: CommentShortSerializer

then previously, it would be included multiple times, whereas now, the
first hash wins. (I haven't actually tested this.) I don't know that
either option is preferable. It's not covered by the test suite, and I
think it's an edge case that is OK to ignore entirely.
2012-10-29 23:15:32 +01:00
Jo Liss
28ee88ca9a In the test, use the same :hash across serializers
Otherwise, `include!` will not remember the unique_values of
already-sideloaded hashes across serializer calls.
2012-10-29 22:48:51 +01:00
José Valim
be70c5c846 Revert "clean up lazy loading of serialization support for
ActiveRecord::Base and ActionController::Base"

The whole idea of having a lazy hook is that it can be executed
in the context of different targets. The moment you hardcode the class,
the hooks can no longer run in the proper context.

This reverts commit 506e2ac9ad.
2012-10-17 19:28:02 +02:00
Jo Liss
3c5e58bf13 Merge pull request #129 from dgeb/active-record-init
clean up lazy loading of serialization support
2012-09-21 11:34:30 -07:00
Jo Liss
0a5d3eb953 Fix assertion for Ruby 1.8.7 2012-09-21 16:57:51 +02:00
Jo Liss
646d57c438 Make test suite compatible with Rails 4
Rails 4 changes the JSONP content_type to text/javascript
2012-09-21 16:36:42 +02:00
Dan Gebhardt
281b45ad2a match method should not be used for routes; instead, a specific HTTP method should be used 2012-09-20 21:17:07 -04:00
Dan Gebhardt
506e2ac9ad clean up lazy loading of serialization support for ActiveRecord::Base and ActionController::Base 2012-09-20 14:23:59 -04:00
Dan Gebhardt
42221a6140 define include_XXX? methods, which can be overridden to conditionally include attributes and associations 2012-08-29 09:26:41 -04:00
Dan Gebhardt
68dc57eb73 simplified the API for include_associations!() to make conditional includes cleaner 2012-08-29 07:42:23 -04:00
twinturbo
38859d1f3d Easier to work with polymorphic interface 2012-08-25 17:39:39 +02:00
Alex Kwiatkowski & Will Bagby
754aa295ba Return empty hash when url_options not provided 2012-08-07 12:56:58 -04:00
Ray Cohen
424dacb457 scope option to render takes precedence over serialization_scope 2012-07-28 21:06:15 -04:00
Ray Cohen
1e7c69c729 Test for having scope option to render override the controller's serialization scope 2012-07-28 19:05:45 -04:00
José Valim
b813646076 assert_include is not available before 1.9 2012-07-21 10:01:27 +02:00
José Valim
e886f597c7 Sets are not ordered, do not depend on the order 2012-07-21 09:48:12 +02:00
José Valim
ef7d475912 Merge pull request #94 from teeparham/array-serializer-root
Add root option to ArraySerializer
2012-07-21 00:38:18 -07:00
José Valim
62484e758c Merge pull request #95 from twinturbo/set-serializer
Close #90
2012-07-21 00:37:31 -07:00
twinturbo
6f3b250dc9 Don't include empty polymoprhic associations
Take this serializer:

class TodoSerializer < ActiveModel::Serializer
  root :todo, :include => true
  has_one :reference, :polymorphic => true
end

A nil reference would generate this JSON:

{
  "todo": { "reference": null },
  "nil_classes": []
}

This commit prevents the `nil_classes` key from being added when
serializing and including nil polymoprhic associations.
2012-07-16 15:08:01 +02:00
twinturbo
486d282922 Raise error when associations cannot be included
include! only works when the source serializer has a root set. The
as_json method sets up some state for the include! method. If a child
association has associations with `:include => true` or `root foo,
:include => true` would cause an undefined method error for `NilClass`.
This is entirely unhelpful for the end user.

This commit raise an error when this situation occurs. It makes it clear
that it's not a problem with AMS but the serialization graph.
2012-07-16 14:18:58 +02:00
twinturbo
5111615ac1 Actually test set serialization 2012-07-15 13:42:29 +02:00
Bradley Priest
f85c624d02 Fix travis 2012-07-15 18:50:47 +08:00
twinturbo
7072e79787 Close #90 2012-07-15 12:18:41 +02:00
twinturbo
cbd7d7d385 Add test for nested included polymoprhic associations 2012-07-15 11:49:36 +02:00
twinturbo
c4e5cd547b Add test for complex polymorphic associations 2012-07-15 11:49:36 +02:00
twinturbo
9f20fe8b36 Test passes 2012-07-15 11:49:36 +02:00
twinturbo
3ca1621011 Add failing test for polymorphic with include 2012-07-15 11:49:36 +02:00
twinturbo
7e96856b87 Support serialize polymorphic id 2012-07-15 11:49:36 +02:00
twinturbo
32f8779114 Basic rooted polymorphism 2012-07-15 11:49:36 +02:00
Tee Parham
84c7cfa988 fix test for custom serializer, add test for :each_serializer 2012-07-14 22:03:14 -06:00
Tee Parham
0832e42917 add class attribute :root to ArraySerializer
You can now set the default behavior for Array serialization in a
single place
2012-07-14 21:44:23 -06:00
José Valim
56824f055b Update master 2012-07-14 18:17:36 +03:00
twinturbo
7936e3efba Add "scope" method 2012-07-14 14:54:23 +02:00
José Valim
3e87c6414d Merge pull request #92 from twinturbo/query-attributes
Close #86
2012-07-14 04:16:07 -07:00
twinturbo
2b9cd97436 Close #86 2012-07-14 13:12:26 +02:00
José Valim
d6a68ed14f Merge pull request #85 from pushcx/master
Allow setting :each_serializer in 'render :json' calls
2012-07-14 04:05:40 -07:00
Bradley Priest
7eb2b90b7c Automatically include ArraySerializer in ActiveRecord::Relation
fixes #81
2012-07-10 10:17:54 +08:00
Peter Harkins
1c820a9ba7 Allow setting the serializer for ArraySerializer. 2012-06-21 11:12:13 -05:00
Tee Parham
c898e3daf9 remove unncessary is_a?(Hash) check 2012-06-12 10:38:32 -07:00
Andy Lindeman
47850677e9 Allows serialization_scope to be disabled with serialization_scope nil 2012-06-05 12:37:09 -04:00
Yehuda Katz
7afac9cd5c Fix the tests 2012-06-05 10:18:05 +02:00