active_model_serializers 0.10.0 → 0.10.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +10 -5
- data/.travis.yml +41 -21
- data/CHANGELOG.md +200 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +25 -4
- data/README.md +166 -28
- data/Rakefile +5 -32
- data/active_model_serializers.gemspec +23 -25
- data/appveyor.yml +10 -6
- data/bin/rubocop +38 -0
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +35 -11
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +86 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +1 -1
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +63 -25
- data/docs/general/serializers.md +137 -14
- data/docs/howto/add_pagination_links.md +16 -17
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +11 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +67 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +15 -3
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +4 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +10 -5
- data/lib/active_model/serializer/association.rb +64 -10
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
- data/lib/active_model/serializer/collection_serializer.rb +39 -13
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +87 -116
- data/lib/active_model/serializer/error_serializer.rb +13 -7
- data/lib/active_model/serializer/errors_serializer.rb +27 -20
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +2 -0
- data/lib/active_model/serializer/has_many_reflection.rb +5 -3
- data/lib/active_model/serializer/has_one_reflection.rb +3 -4
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +136 -130
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +132 -67
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +308 -82
- data/lib/active_model_serializers/adapter/attributes.rb +5 -66
- data/lib/active_model_serializers/adapter/base.rb +41 -39
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
- data/lib/active_model_serializers/adapter/json_api.rb +84 -65
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +9 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +3 -2
- data/lib/active_model_serializers/deserialization.rb +4 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +111 -28
- data/lib/active_model_serializers/railtie.rb +7 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
- data/lib/active_model_serializers/serializable_resource.rb +10 -7
- data/lib/active_model_serializers/serialization_context.rb +12 -3
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +35 -10
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +6 -4
- data/lib/grape/active_model_serializers.rb +9 -5
- data/lib/grape/formatters/active_model_serializers.rb +21 -2
- data/lib/grape/helpers/active_model_serializers.rb +3 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +16 -5
- data/test/action_controller/explicit_serializer_test.rb +7 -4
- data/test/action_controller/json/include_test.rb +108 -27
- data/test/action_controller/json_api/deserialization_test.rb +3 -1
- data/test/action_controller/json_api/errors_test.rb +10 -9
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +31 -24
- data/test/action_controller/json_api/pagination_test.rb +33 -23
- data/test/action_controller/json_api/transform_test.rb +13 -3
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +234 -0
- data/test/action_controller/serialization_scope_name_test.rb +14 -6
- data/test/action_controller/serialization_test.rb +23 -12
- data/test/active_model_serializers/adapter_for_test.rb +2 -0
- data/test/active_model_serializers/json_pointer_test.rb +17 -13
- data/test/active_model_serializers/logging_test.rb +2 -0
- data/test/active_model_serializers/model_test.rb +139 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +14 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +25 -10
- data/test/active_model_serializers/test/schema_test.rb +5 -2
- data/test/active_model_serializers/test/serializer_test.rb +2 -0
- data/test/active_record_test.rb +2 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +2 -0
- data/test/adapter/json/belongs_to_test.rb +2 -0
- data/test/adapter/json/collection_test.rb +16 -0
- data/test/adapter/json/has_many_test.rb +12 -2
- data/test/adapter/json/transform_test.rb +17 -15
- data/test/adapter/json_api/belongs_to_test.rb +2 -0
- data/test/adapter/json_api/collection_test.rb +6 -3
- data/test/adapter/json_api/errors_test.rb +19 -19
- data/test/adapter/json_api/fields_test.rb +14 -3
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
- data/test/adapter/json_api/has_many_test.rb +51 -20
- data/test/adapter/json_api/has_one_test.rb +2 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +7 -7
- data/test/adapter/json_api/linked_test.rb +35 -12
- data/test/adapter/json_api/links_test.rb +22 -3
- data/test/adapter/json_api/pagination_links_test.rb +55 -13
- data/test/adapter/json_api/parse_test.rb +3 -1
- data/test/adapter/json_api/relationship_test.rb +311 -73
- data/test/adapter/json_api/resource_meta_test.rb +5 -3
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
- data/test/adapter/json_api/transform_test.rb +265 -253
- data/test/adapter/json_api/type_test.rb +170 -36
- data/test/adapter/json_test.rb +10 -7
- data/test/adapter/null_test.rb +3 -2
- data/test/adapter/polymorphic_test.rb +54 -5
- data/test/adapter_test.rb +3 -1
- data/test/array_serializer_test.rb +2 -0
- data/test/benchmark/app.rb +3 -1
- data/test/benchmark/benchmarking_support.rb +3 -1
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +18 -16
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +23 -10
- data/test/benchmark/controllers.rb +18 -17
- data/test/benchmark/fixtures.rb +74 -72
- data/test/cache_test.rb +301 -69
- data/test/collection_serializer_test.rb +33 -14
- data/test/fixtures/active_record.rb +47 -10
- data/test/fixtures/poro.rb +128 -183
- data/test/generators/scaffold_controller_generator_test.rb +2 -0
- data/test/generators/serializer_generator_test.rb +25 -5
- data/test/grape_test.rb +172 -56
- data/test/lint_test.rb +3 -1
- data/test/logger_test.rb +15 -11
- data/test/poro_test.rb +2 -0
- data/test/serializable_resource_test.rb +20 -22
- data/test/serializers/association_macros_test.rb +5 -2
- data/test/serializers/associations_test.rb +274 -49
- data/test/serializers/attribute_test.rb +7 -3
- data/test/serializers/attributes_test.rb +3 -1
- data/test/serializers/caching_configuration_test_isolated.rb +8 -6
- data/test/serializers/configuration_test.rb +2 -0
- data/test/serializers/fieldset_test.rb +3 -1
- data/test/serializers/meta_test.rb +14 -6
- data/test/serializers/options_test.rb +19 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +5 -3
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +3 -1
- data/test/serializers/serialization_test.rb +4 -2
- data/test/serializers/serializer_for_test.rb +14 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/isolated_unit.rb +11 -4
- data/test/support/rails5_shims.rb +10 -2
- data/test/support/rails_app.rb +4 -9
- data/test/support/serialization_testing.rb +33 -5
- data/test/test_helper.rb +15 -0
- metadata +126 -46
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/adapter/json_api/resource_identifier_test.rb +0 -85
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_record'
|
2
4
|
|
3
5
|
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
@@ -47,16 +49,6 @@ module ARModels
|
|
47
49
|
has_many :comments
|
48
50
|
belongs_to :author
|
49
51
|
end
|
50
|
-
|
51
|
-
class Comment < ActiveRecord::Base
|
52
|
-
belongs_to :post
|
53
|
-
belongs_to :author
|
54
|
-
end
|
55
|
-
|
56
|
-
class Author < ActiveRecord::Base
|
57
|
-
has_many :posts
|
58
|
-
end
|
59
|
-
|
60
52
|
class PostSerializer < ActiveModel::Serializer
|
61
53
|
attributes :id, :title, :body
|
62
54
|
|
@@ -64,15 +56,60 @@ module ARModels
|
|
64
56
|
belongs_to :author
|
65
57
|
end
|
66
58
|
|
59
|
+
class Comment < ActiveRecord::Base
|
60
|
+
belongs_to :post
|
61
|
+
belongs_to :author
|
62
|
+
end
|
67
63
|
class CommentSerializer < ActiveModel::Serializer
|
68
64
|
attributes :id, :contents
|
69
65
|
|
70
66
|
belongs_to :author
|
71
67
|
end
|
72
68
|
|
69
|
+
class Author < ActiveRecord::Base
|
70
|
+
has_many :posts
|
71
|
+
end
|
73
72
|
class AuthorSerializer < ActiveModel::Serializer
|
74
73
|
attributes :id, :name
|
75
74
|
|
76
75
|
has_many :posts
|
77
76
|
end
|
78
77
|
end
|
78
|
+
|
79
|
+
class Employee < ActiveRecord::Base
|
80
|
+
has_many :pictures, as: :imageable
|
81
|
+
has_many :object_tags, as: :taggable
|
82
|
+
end
|
83
|
+
|
84
|
+
class PolymorphicSimpleSerializer < ActiveModel::Serializer
|
85
|
+
attributes :id
|
86
|
+
end
|
87
|
+
|
88
|
+
class ObjectTag < ActiveRecord::Base
|
89
|
+
belongs_to :poly_tag
|
90
|
+
belongs_to :taggable, polymorphic: true
|
91
|
+
end
|
92
|
+
class PolymorphicObjectTagSerializer < ActiveModel::Serializer
|
93
|
+
attributes :id
|
94
|
+
belongs_to :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
|
95
|
+
end
|
96
|
+
|
97
|
+
class PolyTag < ActiveRecord::Base
|
98
|
+
has_many :object_tags
|
99
|
+
end
|
100
|
+
class PolymorphicTagSerializer < ActiveModel::Serializer
|
101
|
+
attributes :id, :phrase
|
102
|
+
has_many :object_tags, serializer: PolymorphicObjectTagSerializer
|
103
|
+
end
|
104
|
+
|
105
|
+
class Picture < ActiveRecord::Base
|
106
|
+
belongs_to :imageable, polymorphic: true
|
107
|
+
has_many :object_tags, as: :taggable
|
108
|
+
end
|
109
|
+
class PolymorphicHasManySerializer < ActiveModel::Serializer
|
110
|
+
attributes :id, :name
|
111
|
+
end
|
112
|
+
class PolymorphicBelongsToSerializer < ActiveModel::Serializer
|
113
|
+
attributes :id, :title
|
114
|
+
belongs_to :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
|
115
|
+
end
|
data/test/fixtures/poro.rb
CHANGED
@@ -1,25 +1,30 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
3
|
class Model < ActiveModelSerializers::Model
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4
|
+
rand(2).zero? && derive_attributes_from_names_and_fix_accessors
|
5
|
+
|
6
|
+
attr_writer :id
|
7
|
+
|
8
|
+
# At this time, just for organization of intent
|
9
|
+
class_attribute :association_names
|
10
|
+
self.association_names = []
|
11
|
+
|
12
|
+
def self.associations(*names)
|
13
|
+
self.association_names |= names.map(&:to_sym)
|
14
|
+
# Silence redefinition of methods warnings
|
15
|
+
ActiveModelSerializers.silence_warnings do
|
16
|
+
attr_accessor(*names)
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def associations
|
21
|
+
association_names.each_with_object({}) do |association_name, result|
|
22
|
+
result[association_name] = public_send(association_name).freeze
|
23
|
+
end.with_indifferent_access.freeze
|
24
|
+
end
|
25
|
+
|
26
|
+
def attributes
|
27
|
+
super.except(*association_names)
|
23
28
|
end
|
24
29
|
end
|
25
30
|
|
@@ -30,67 +35,60 @@ end
|
|
30
35
|
# model = ModelWithErrors.new
|
31
36
|
# model.validate! # => ["cannot be nil"]
|
32
37
|
# model.errors.full_messages # => ["name cannot be nil"]
|
33
|
-
class ModelWithErrors <
|
34
|
-
|
38
|
+
class ModelWithErrors < Model
|
39
|
+
attributes :name
|
35
40
|
end
|
36
41
|
|
37
42
|
class Profile < Model
|
43
|
+
attributes :name, :description
|
44
|
+
associations :comments
|
38
45
|
end
|
39
|
-
|
40
46
|
class ProfileSerializer < ActiveModel::Serializer
|
41
47
|
attributes :name, :description
|
42
|
-
|
43
|
-
# TODO: is this used anywhere?
|
44
|
-
def arguments_passed_in?
|
45
|
-
instance_options[:my_options] == :accessible
|
46
|
-
end
|
47
48
|
end
|
48
|
-
|
49
49
|
class ProfilePreviewSerializer < ActiveModel::Serializer
|
50
50
|
attributes :name
|
51
51
|
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
Bio = Class.new(Model)
|
57
|
-
Blog = Class.new(Model)
|
58
|
-
Role = Class.new(Model)
|
59
|
-
User = Class.new(Model)
|
60
|
-
Location = Class.new(Model)
|
61
|
-
Place = Class.new(Model)
|
62
|
-
Tag = Class.new(Model)
|
63
|
-
VirtualValue = Class.new(Model)
|
64
|
-
Comment = Class.new(Model) do
|
65
|
-
# Uses a custom non-time-based cache key
|
66
|
-
def cache_key
|
67
|
-
"#{self.class.name.downcase}/#{self.id}"
|
68
|
-
end
|
53
|
+
class Author < Model
|
54
|
+
attributes :name
|
55
|
+
associations :posts, :bio, :roles, :comments
|
69
56
|
end
|
57
|
+
class AuthorSerializer < ActiveModel::Serializer
|
58
|
+
cache key: 'writer', skip_digest: true
|
59
|
+
attribute :id
|
60
|
+
attribute :name
|
70
61
|
|
71
|
-
|
72
|
-
has_many :
|
73
|
-
|
62
|
+
has_many :posts
|
63
|
+
has_many :roles
|
64
|
+
has_one :bio
|
74
65
|
end
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
belongs_to :taggable, polymorphic: true
|
66
|
+
class AuthorPreviewSerializer < ActiveModel::Serializer
|
67
|
+
attributes :id
|
68
|
+
has_many :posts
|
79
69
|
end
|
80
70
|
|
81
|
-
class
|
82
|
-
|
83
|
-
|
71
|
+
class Comment < Model
|
72
|
+
attributes :body, :date
|
73
|
+
associations :post, :author, :likes
|
84
74
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
75
|
+
class CommentSerializer < ActiveModel::Serializer
|
76
|
+
cache expires_in: 1.day, skip_digest: true
|
77
|
+
attributes :id, :body
|
78
|
+
belongs_to :post
|
79
|
+
belongs_to :author
|
88
80
|
end
|
81
|
+
class CommentPreviewSerializer < ActiveModel::Serializer
|
82
|
+
attributes :id
|
89
83
|
|
90
|
-
|
91
|
-
|
84
|
+
belongs_to :post
|
85
|
+
end
|
92
86
|
|
93
|
-
|
87
|
+
class Post < Model
|
88
|
+
attributes :title, :body
|
89
|
+
associations :author, :comments, :blog, :tags, :related
|
90
|
+
end
|
91
|
+
class PostSerializer < ActiveModel::Serializer
|
94
92
|
cache key: 'post', expires_in: 0.1, skip_digest: true
|
95
93
|
attributes :id, :title, :body
|
96
94
|
|
@@ -101,143 +99,116 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
|
|
101
99
|
def blog
|
102
100
|
Blog.new(id: 999, name: 'Custom blog')
|
103
101
|
end
|
104
|
-
|
105
|
-
# TODO: is this used anywhere?
|
106
|
-
def custom_options
|
107
|
-
instance_options
|
108
|
-
end
|
109
102
|
end
|
110
|
-
|
111
|
-
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
|
103
|
+
class SpammyPostSerializer < ActiveModel::Serializer
|
112
104
|
attributes :id
|
113
105
|
has_many :related
|
114
106
|
end
|
107
|
+
class PostPreviewSerializer < ActiveModel::Serializer
|
108
|
+
attributes :title, :body, :id
|
115
109
|
|
116
|
-
|
117
|
-
|
118
|
-
attributes :id, :body
|
119
|
-
|
120
|
-
belongs_to :post
|
121
|
-
belongs_to :author
|
122
|
-
|
123
|
-
def custom_options
|
124
|
-
instance_options
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
129
|
-
cache key: 'writer', skip_digest: true
|
130
|
-
attribute :id
|
131
|
-
attribute :name
|
132
|
-
|
133
|
-
has_many :posts
|
134
|
-
has_many :roles
|
135
|
-
has_one :bio
|
136
|
-
end
|
137
|
-
|
138
|
-
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
139
|
-
cache only: [:name], skip_digest: true
|
140
|
-
attributes :id, :name, :description, :slug
|
141
|
-
|
142
|
-
def slug
|
143
|
-
"#{object.name}-#{object.id}"
|
144
|
-
end
|
145
|
-
|
146
|
-
belongs_to :author
|
147
|
-
end
|
148
|
-
|
149
|
-
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
150
|
-
attributes :id, :time
|
151
|
-
|
152
|
-
belongs_to :likeable
|
110
|
+
has_many :comments, serializer: ::CommentPreviewSerializer
|
111
|
+
belongs_to :author, serializer: ::AuthorPreviewSerializer
|
153
112
|
end
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
belongs_to :place
|
160
|
-
|
161
|
-
def place
|
162
|
-
'Nowhere'
|
163
|
-
end
|
113
|
+
class PostWithCustomKeysSerializer < ActiveModel::Serializer
|
114
|
+
attributes :id
|
115
|
+
has_many :comments, key: :reviews
|
116
|
+
belongs_to :author, key: :writer
|
117
|
+
has_one :blog, key: :site
|
164
118
|
end
|
165
119
|
|
166
|
-
|
167
|
-
attributes :
|
168
|
-
|
169
|
-
has_many :locations
|
120
|
+
class Bio < Model
|
121
|
+
attributes :content, :rating
|
122
|
+
associations :author
|
170
123
|
end
|
171
|
-
|
172
|
-
BioSerializer = Class.new(ActiveModel::Serializer) do
|
124
|
+
class BioSerializer < ActiveModel::Serializer
|
173
125
|
cache except: [:content], skip_digest: true
|
174
126
|
attributes :id, :content, :rating
|
175
127
|
|
176
128
|
belongs_to :author
|
177
129
|
end
|
178
130
|
|
179
|
-
|
131
|
+
class Blog < Model
|
132
|
+
attributes :name, :type, :special_attribute
|
133
|
+
associations :writer, :articles
|
134
|
+
end
|
135
|
+
class BlogSerializer < ActiveModel::Serializer
|
180
136
|
cache key: 'blog'
|
181
137
|
attributes :id, :name
|
182
138
|
|
183
139
|
belongs_to :writer
|
184
140
|
has_many :articles
|
185
141
|
end
|
186
|
-
|
187
|
-
PaginatedSerializer = Class.new(ActiveModel::Serializer::CollectionSerializer) do
|
188
|
-
def json_key
|
189
|
-
'paginated'
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
142
|
+
class AlternateBlogSerializer < ActiveModel::Serializer
|
194
143
|
attribute :id
|
195
144
|
attribute :name, key: :title
|
196
145
|
end
|
197
|
-
|
198
|
-
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
|
146
|
+
class CustomBlogSerializer < ActiveModel::Serializer
|
199
147
|
attribute :id
|
200
148
|
attribute :special_attribute
|
201
|
-
|
202
149
|
has_many :articles
|
203
150
|
end
|
204
151
|
|
205
|
-
|
206
|
-
attributes :
|
207
|
-
|
208
|
-
belongs_to :post
|
152
|
+
class Role < Model
|
153
|
+
attributes :name, :description, :special_attribute
|
154
|
+
associations :author
|
209
155
|
end
|
156
|
+
class RoleSerializer < ActiveModel::Serializer
|
157
|
+
cache only: [:name, :slug], skip_digest: true
|
158
|
+
attributes :id, :name, :description
|
159
|
+
attribute :friendly_id, key: :slug
|
160
|
+
belongs_to :author
|
210
161
|
|
211
|
-
|
212
|
-
|
162
|
+
def friendly_id
|
163
|
+
"#{object.name}-#{object.id}"
|
164
|
+
end
|
165
|
+
end
|
213
166
|
|
214
|
-
|
167
|
+
class Location < Model
|
168
|
+
attributes :lat, :lng
|
169
|
+
associations :place
|
215
170
|
end
|
171
|
+
class LocationSerializer < ActiveModel::Serializer
|
172
|
+
cache only: [:address], skip_digest: true
|
173
|
+
attributes :id, :lat, :lng
|
216
174
|
|
217
|
-
|
218
|
-
attributes :title, :body, :id
|
175
|
+
belongs_to :place, key: :address
|
219
176
|
|
220
|
-
|
221
|
-
|
177
|
+
def place
|
178
|
+
'Nowhere'
|
179
|
+
end
|
222
180
|
end
|
223
181
|
|
224
|
-
|
225
|
-
attributes :
|
226
|
-
|
227
|
-
|
182
|
+
class Place < Model
|
183
|
+
attributes :name
|
184
|
+
associations :locations
|
185
|
+
end
|
186
|
+
class PlaceSerializer < ActiveModel::Serializer
|
187
|
+
attributes :id, :name
|
188
|
+
has_many :locations
|
228
189
|
end
|
229
190
|
|
230
|
-
|
231
|
-
attributes :
|
191
|
+
class Like < Model
|
192
|
+
attributes :time
|
193
|
+
associations :likeable
|
194
|
+
end
|
195
|
+
class LikeSerializer < ActiveModel::Serializer
|
196
|
+
attributes :id, :time
|
197
|
+
belongs_to :likeable
|
198
|
+
end
|
232
199
|
|
233
|
-
|
234
|
-
|
235
|
-
|
200
|
+
module Spam
|
201
|
+
class UnrelatedLink < Model
|
202
|
+
end
|
203
|
+
class UnrelatedLinkSerializer < ActiveModel::Serializer
|
204
|
+
cache only: [:id]
|
205
|
+
attributes :id
|
206
|
+
end
|
236
207
|
end
|
237
208
|
|
238
|
-
|
209
|
+
class VirtualValue < Model; end
|
210
|
+
class VirtualValueSerializer < ActiveModel::Serializer
|
239
211
|
attributes :id
|
240
|
-
|
241
212
|
has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
|
242
213
|
{ type: 'reviews', id: '2' }]
|
243
214
|
has_one :maker, virtual_value: { type: 'makers', id: '1' }
|
@@ -249,34 +220,8 @@ VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
|
|
249
220
|
end
|
250
221
|
end
|
251
222
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
PolymorphicBelongsToSerializer = Class.new(ActiveModel::Serializer) do
|
257
|
-
attributes :id, :title
|
258
|
-
|
259
|
-
has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
|
260
|
-
end
|
261
|
-
|
262
|
-
PolymorphicSimpleSerializer = Class.new(ActiveModel::Serializer) do
|
263
|
-
attributes :id
|
264
|
-
end
|
265
|
-
|
266
|
-
PolymorphicObjectTagSerializer = Class.new(ActiveModel::Serializer) do
|
267
|
-
attributes :id
|
268
|
-
|
269
|
-
has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
|
270
|
-
end
|
271
|
-
|
272
|
-
PolymorphicTagSerializer = Class.new(ActiveModel::Serializer) do
|
273
|
-
attributes :id, :phrase
|
274
|
-
|
275
|
-
has_many :object_tags, serializer: PolymorphicObjectTagSerializer
|
276
|
-
end
|
277
|
-
|
278
|
-
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
279
|
-
cache only: [:id]
|
280
|
-
attributes :id
|
223
|
+
class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
|
224
|
+
def json_key
|
225
|
+
'paginated'
|
226
|
+
end
|
281
227
|
end
|
282
|
-
$VERBOSE = verbose
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
require 'generators/rails/resource_override'
|
3
5
|
require 'generators/rails/serializer_generator'
|
@@ -20,11 +22,10 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def test_uses_application_serializer_if_one_exists
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
Object.send :remove_const, :ApplicationSerializer
|
25
|
+
stub_safe_constantize(expected: 'ApplicationSerializer') do
|
26
|
+
run_generator
|
27
|
+
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
|
28
|
+
end
|
28
29
|
end
|
29
30
|
|
30
31
|
def test_uses_given_parent
|
@@ -54,4 +55,23 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
|
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def stub_safe_constantize(expected:)
|
62
|
+
String.class_eval do
|
63
|
+
alias_method :old, :safe_constantize
|
64
|
+
end
|
65
|
+
String.send(:define_method, :safe_constantize) do
|
66
|
+
Class if self == expected
|
67
|
+
end
|
68
|
+
|
69
|
+
yield
|
70
|
+
ensure
|
71
|
+
String.class_eval do
|
72
|
+
undef_method :safe_constantize
|
73
|
+
alias_method :safe_constantize, :old
|
74
|
+
undef_method :old
|
75
|
+
end
|
76
|
+
end
|
57
77
|
end
|