active_model_serializers_custom 0.10.90
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +35 -0
- data/.rubocop.yml +109 -0
- data/.simplecov +110 -0
- data/.travis.yml +63 -0
- data/CHANGELOG.md +727 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +74 -0
- data/MIT-LICENSE +22 -0
- data/README.md +305 -0
- data/Rakefile +76 -0
- data/active_model_serializers.gemspec +64 -0
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +76 -0
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer.rb +418 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +91 -0
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +90 -0
- data/lib/active_model/serializer/concerns/caching.rb +304 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- 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 +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +7 -0
- data/lib/active_model_serializers.rb +63 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/adapter/attributes.rb +15 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +90 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +52 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +12 -0
- data/lib/generators/rails/serializer_generator.rb +38 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +64 -0
- data/test/action_controller/explicit_serializer_test.rb +137 -0
- data/test/action_controller/json/include_test.rb +248 -0
- data/test/action_controller/json_api/deserialization_test.rb +114 -0
- data/test/action_controller/json_api/errors_test.rb +42 -0
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +204 -0
- data/test/action_controller/json_api/pagination_test.rb +126 -0
- data/test/action_controller/json_api/transform_test.rb +191 -0
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +239 -0
- data/test/action_controller/serialization_scope_name_test.rb +237 -0
- data/test/action_controller/serialization_test.rb +480 -0
- data/test/active_model_serializers/adapter_for_test.rb +210 -0
- data/test/active_model_serializers/json_pointer_test.rb +24 -0
- data/test/active_model_serializers/logging_test.rb +79 -0
- data/test/active_model_serializers/model_test.rb +144 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +70 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +73 -0
- data/test/active_model_serializers/test/schema_test.rb +133 -0
- data/test/active_model_serializers/test/serializer_test.rb +64 -0
- data/test/active_record_test.rb +11 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +102 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +106 -0
- data/test/adapter/json/has_many_test.rb +55 -0
- data/test/adapter/json/transform_test.rb +95 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +98 -0
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +98 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +175 -0
- data/test/adapter/json_api/has_one_test.rb +82 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +35 -0
- data/test/adapter/json_api/linked_test.rb +415 -0
- data/test/adapter/json_api/links_test.rb +112 -0
- data/test/adapter/json_api/pagination_links_test.rb +208 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/relationship_test.rb +399 -0
- data/test/adapter/json_api/resource_meta_test.rb +102 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_api/transform_test.rb +514 -0
- data/test/adapter/json_api/type_test.rb +195 -0
- data/test/adapter/json_test.rb +48 -0
- data/test/adapter/null_test.rb +24 -0
- data/test/adapter/polymorphic_test.rb +220 -0
- data/test/adapter_test.rb +69 -0
- data/test/array_serializer_test.rb +24 -0
- data/test/benchmark/app.rb +67 -0
- data/test/benchmark/benchmarking_support.rb +69 -0
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +121 -0
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +47 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +85 -0
- data/test/benchmark/fixtures.rb +221 -0
- data/test/cache_test.rb +717 -0
- data/test/collection_serializer_test.rb +129 -0
- data/test/fixtures/active_record.rb +115 -0
- data/test/fixtures/poro.rb +227 -0
- data/test/generators/scaffold_controller_generator_test.rb +26 -0
- data/test/generators/serializer_generator_test.rb +77 -0
- data/test/grape_test.rb +198 -0
- data/test/lint_test.rb +51 -0
- data/test/logger_test.rb +22 -0
- data/test/poro_test.rb +11 -0
- data/test/serializable_resource_test.rb +81 -0
- data/test/serializers/association_macros_test.rb +39 -0
- data/test/serializers/associations_test.rb +520 -0
- data/test/serializers/attribute_test.rb +155 -0
- data/test/serializers/attributes_test.rb +54 -0
- data/test/serializers/caching_configuration_test_isolated.rb +172 -0
- data/test/serializers/configuration_test.rb +34 -0
- data/test/serializers/fieldset_test.rb +16 -0
- data/test/serializers/meta_test.rb +204 -0
- data/test/serializers/options_test.rb +34 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +81 -0
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serialization_test.rb +57 -0
- data/test/serializers/serializer_for_test.rb +138 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +86 -0
- data/test/support/rails5_shims.rb +55 -0
- data/test/support/rails_app.rb +40 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +81 -0
- data/test/test_helper.rb +72 -0
- metadata +622 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
class Serializer
|
7
|
+
class CollectionSerializerTest < ActiveSupport::TestCase
|
8
|
+
class SingularModel < ::Model; end
|
9
|
+
class SingularModelSerializer < ActiveModel::Serializer
|
10
|
+
end
|
11
|
+
class HasManyModel < ::Model
|
12
|
+
associations :singular_models
|
13
|
+
end
|
14
|
+
class HasManyModelSerializer < ActiveModel::Serializer
|
15
|
+
has_many :singular_models
|
16
|
+
|
17
|
+
def custom_options
|
18
|
+
instance_options
|
19
|
+
end
|
20
|
+
end
|
21
|
+
class MessagesSerializer < ActiveModel::Serializer
|
22
|
+
type 'messages'
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup
|
26
|
+
@singular_model = SingularModel.new
|
27
|
+
@has_many_model = HasManyModel.new
|
28
|
+
@resource = build_named_collection @singular_model, @has_many_model
|
29
|
+
@serializer = collection_serializer.new(@resource, some: :options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def collection_serializer
|
33
|
+
CollectionSerializer
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_named_collection(*resource)
|
37
|
+
resource.define_singleton_method(:name) { 'MeResource' }
|
38
|
+
resource
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_has_object_reader_serializer_interface
|
42
|
+
assert_equal @serializer.object, @resource
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_respond_to_each
|
46
|
+
assert_respond_to @serializer, :each
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_each_object_should_be_serialized_with_appropriate_serializer
|
50
|
+
serializers = @serializer.to_a
|
51
|
+
|
52
|
+
assert_kind_of SingularModelSerializer, serializers.first
|
53
|
+
assert_kind_of SingularModel, serializers.first.object
|
54
|
+
|
55
|
+
assert_kind_of HasManyModelSerializer, serializers.last
|
56
|
+
assert_kind_of HasManyModel, serializers.last.object
|
57
|
+
|
58
|
+
assert_equal :options, serializers.last.custom_options[:some]
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_serializer_option_not_passed_to_each_serializer
|
62
|
+
serializers = collection_serializer.new([@has_many_model], serializer: HasManyModelSerializer).to_a
|
63
|
+
|
64
|
+
refute serializers.first.custom_options.key?(:serializer)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_root_default
|
68
|
+
@serializer = collection_serializer.new([@singular_model, @has_many_model])
|
69
|
+
assert_nil @serializer.root
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_root
|
73
|
+
expected = 'custom_root'
|
74
|
+
@serializer = collection_serializer.new([@singular_model, @has_many_model], root: expected)
|
75
|
+
assert_equal expected, @serializer.root
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_root_with_no_serializers
|
79
|
+
expected = 'custom_root'
|
80
|
+
@serializer = collection_serializer.new([], root: expected)
|
81
|
+
assert_equal expected, @serializer.root
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_json_key_with_resource_with_serializer
|
85
|
+
singular_key = @serializer.send(:serializers).first.json_key
|
86
|
+
assert_equal singular_key.pluralize, @serializer.json_key
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_json_key_with_resource_with_name_and_no_serializers
|
90
|
+
serializer = collection_serializer.new(build_named_collection)
|
91
|
+
assert_equal 'me_resources', serializer.json_key
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_json_key_with_resource_with_nil_name_and_no_serializers
|
95
|
+
resource = []
|
96
|
+
resource.define_singleton_method(:name) { nil }
|
97
|
+
serializer = collection_serializer.new(resource)
|
98
|
+
assert_raise ArgumentError do
|
99
|
+
serializer.json_key
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_json_key_with_resource_without_name_and_no_serializers
|
104
|
+
serializer = collection_serializer.new([])
|
105
|
+
assert_raise ArgumentError do
|
106
|
+
serializer.json_key
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_json_key_with_empty_resources_with_serializer
|
111
|
+
resource = []
|
112
|
+
serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
|
113
|
+
assert_equal 'messages', serializer.json_key
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_json_key_with_root
|
117
|
+
expected = 'custom_root'
|
118
|
+
serializer = collection_serializer.new(@resource, root: expected)
|
119
|
+
assert_equal expected, serializer.json_key
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_json_key_with_root_and_no_serializers
|
123
|
+
expected = 'custom_root'
|
124
|
+
serializer = collection_serializer.new(build_named_collection, root: expected)
|
125
|
+
assert_equal expected, serializer.json_key
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
6
|
+
ActiveRecord::Schema.define do
|
7
|
+
self.verbose = false
|
8
|
+
create_table :posts, force: true do |t|
|
9
|
+
t.string :title
|
10
|
+
t.text :body
|
11
|
+
t.references :author
|
12
|
+
t.timestamps null: false
|
13
|
+
end
|
14
|
+
create_table :authors, force: true do |t|
|
15
|
+
t.string :name
|
16
|
+
t.timestamps null: false
|
17
|
+
end
|
18
|
+
create_table :comments, force: true do |t|
|
19
|
+
t.text :contents
|
20
|
+
t.references :author
|
21
|
+
t.references :post
|
22
|
+
t.timestamps null: false
|
23
|
+
end
|
24
|
+
create_table :employees, force: true do |t|
|
25
|
+
t.string :name
|
26
|
+
t.string :email
|
27
|
+
t.timestamps null: false
|
28
|
+
end
|
29
|
+
create_table :object_tags, force: true do |t|
|
30
|
+
t.string :poly_tag_id
|
31
|
+
t.string :taggable_type
|
32
|
+
t.string :taggable_id
|
33
|
+
t.timestamps null: false
|
34
|
+
end
|
35
|
+
create_table :poly_tags, force: true do |t|
|
36
|
+
t.string :phrase
|
37
|
+
t.timestamps null: false
|
38
|
+
end
|
39
|
+
create_table :pictures, force: true do |t|
|
40
|
+
t.string :title
|
41
|
+
t.string :imageable_type
|
42
|
+
t.string :imageable_id
|
43
|
+
t.timestamps null: false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module ARModels
|
48
|
+
class Post < ActiveRecord::Base
|
49
|
+
has_many :comments
|
50
|
+
belongs_to :author
|
51
|
+
end
|
52
|
+
class PostSerializer < ActiveModel::Serializer
|
53
|
+
attributes :id, :title, :body
|
54
|
+
|
55
|
+
has_many :comments
|
56
|
+
belongs_to :author
|
57
|
+
end
|
58
|
+
|
59
|
+
class Comment < ActiveRecord::Base
|
60
|
+
belongs_to :post
|
61
|
+
belongs_to :author
|
62
|
+
end
|
63
|
+
class CommentSerializer < ActiveModel::Serializer
|
64
|
+
attributes :id, :contents
|
65
|
+
|
66
|
+
belongs_to :author
|
67
|
+
end
|
68
|
+
|
69
|
+
class Author < ActiveRecord::Base
|
70
|
+
has_many :posts
|
71
|
+
end
|
72
|
+
class AuthorSerializer < ActiveModel::Serializer
|
73
|
+
attributes :id, :name
|
74
|
+
|
75
|
+
has_many :posts
|
76
|
+
end
|
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
|
@@ -0,0 +1,227 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Model < ActiveModelSerializers::Model
|
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)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
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)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# see
|
32
|
+
# https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb
|
33
|
+
# The below allows you to do:
|
34
|
+
#
|
35
|
+
# model = ModelWithErrors.new
|
36
|
+
# model.validate! # => ["cannot be nil"]
|
37
|
+
# model.errors.full_messages # => ["name cannot be nil"]
|
38
|
+
class ModelWithErrors < Model
|
39
|
+
attributes :name
|
40
|
+
end
|
41
|
+
|
42
|
+
class Profile < Model
|
43
|
+
attributes :name, :description
|
44
|
+
associations :comments
|
45
|
+
end
|
46
|
+
class ProfileSerializer < ActiveModel::Serializer
|
47
|
+
attributes :name, :description
|
48
|
+
end
|
49
|
+
class ProfilePreviewSerializer < ActiveModel::Serializer
|
50
|
+
attributes :name
|
51
|
+
end
|
52
|
+
|
53
|
+
class Author < Model
|
54
|
+
attributes :name
|
55
|
+
associations :posts, :bio, :roles, :comments
|
56
|
+
end
|
57
|
+
class AuthorSerializer < ActiveModel::Serializer
|
58
|
+
cache key: 'writer', skip_digest: true
|
59
|
+
attribute :id
|
60
|
+
attribute :name
|
61
|
+
|
62
|
+
has_many :posts
|
63
|
+
has_many :roles
|
64
|
+
has_one :bio
|
65
|
+
end
|
66
|
+
class AuthorPreviewSerializer < ActiveModel::Serializer
|
67
|
+
attributes :id
|
68
|
+
has_many :posts
|
69
|
+
end
|
70
|
+
|
71
|
+
class Comment < Model
|
72
|
+
attributes :body, :date
|
73
|
+
associations :post, :author, :likes
|
74
|
+
end
|
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
|
80
|
+
end
|
81
|
+
class CommentPreviewSerializer < ActiveModel::Serializer
|
82
|
+
attributes :id
|
83
|
+
|
84
|
+
belongs_to :post
|
85
|
+
end
|
86
|
+
|
87
|
+
class Post < Model
|
88
|
+
attributes :title, :body
|
89
|
+
associations :author, :comments, :blog, :tags, :related
|
90
|
+
end
|
91
|
+
class PostSerializer < ActiveModel::Serializer
|
92
|
+
cache key: 'post', expires_in: 0.1, skip_digest: true
|
93
|
+
attributes :id, :title, :body
|
94
|
+
|
95
|
+
has_many :comments
|
96
|
+
belongs_to :blog
|
97
|
+
belongs_to :author
|
98
|
+
|
99
|
+
def blog
|
100
|
+
Blog.new(id: 999, name: 'Custom blog')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
class SpammyPostSerializer < ActiveModel::Serializer
|
104
|
+
attributes :id
|
105
|
+
has_many :related
|
106
|
+
end
|
107
|
+
class PostPreviewSerializer < ActiveModel::Serializer
|
108
|
+
attributes :title, :body, :id
|
109
|
+
|
110
|
+
has_many :comments, serializer: ::CommentPreviewSerializer
|
111
|
+
belongs_to :author, serializer: ::AuthorPreviewSerializer
|
112
|
+
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
|
118
|
+
end
|
119
|
+
|
120
|
+
class Bio < Model
|
121
|
+
attributes :content, :rating
|
122
|
+
associations :author
|
123
|
+
end
|
124
|
+
class BioSerializer < ActiveModel::Serializer
|
125
|
+
cache except: [:content], skip_digest: true
|
126
|
+
attributes :id, :content, :rating
|
127
|
+
|
128
|
+
belongs_to :author
|
129
|
+
end
|
130
|
+
|
131
|
+
class Blog < Model
|
132
|
+
attributes :name, :type, :special_attribute
|
133
|
+
associations :writer, :articles
|
134
|
+
end
|
135
|
+
class BlogSerializer < ActiveModel::Serializer
|
136
|
+
cache key: 'blog'
|
137
|
+
attributes :id, :name
|
138
|
+
|
139
|
+
belongs_to :writer
|
140
|
+
has_many :articles
|
141
|
+
end
|
142
|
+
class AlternateBlogSerializer < ActiveModel::Serializer
|
143
|
+
attribute :id
|
144
|
+
attribute :name, key: :title
|
145
|
+
end
|
146
|
+
class CustomBlogSerializer < ActiveModel::Serializer
|
147
|
+
attribute :id
|
148
|
+
attribute :special_attribute
|
149
|
+
has_many :articles
|
150
|
+
end
|
151
|
+
|
152
|
+
class Role < Model
|
153
|
+
attributes :name, :description, :special_attribute
|
154
|
+
associations :author
|
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
|
161
|
+
|
162
|
+
def friendly_id
|
163
|
+
"#{object.name}-#{object.id}"
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
class Location < Model
|
168
|
+
attributes :lat, :lng
|
169
|
+
associations :place
|
170
|
+
end
|
171
|
+
class LocationSerializer < ActiveModel::Serializer
|
172
|
+
cache only: [:address], skip_digest: true
|
173
|
+
attributes :id, :lat, :lng
|
174
|
+
|
175
|
+
belongs_to :place, key: :address
|
176
|
+
|
177
|
+
def place
|
178
|
+
'Nowhere'
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
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
|
189
|
+
end
|
190
|
+
|
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
|
199
|
+
|
200
|
+
module Spam
|
201
|
+
class UnrelatedLink < Model
|
202
|
+
end
|
203
|
+
class UnrelatedLinkSerializer < ActiveModel::Serializer
|
204
|
+
cache only: [:id]
|
205
|
+
attributes :id
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
class VirtualValue < Model; end
|
210
|
+
class VirtualValueSerializer < ActiveModel::Serializer
|
211
|
+
attributes :id
|
212
|
+
has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
|
213
|
+
{ type: 'reviews', id: '2' }]
|
214
|
+
has_one :maker, virtual_value: { type: 'makers', id: '1' }
|
215
|
+
|
216
|
+
def reviews
|
217
|
+
end
|
218
|
+
|
219
|
+
def maker
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
|
224
|
+
def json_key
|
225
|
+
'paginated'
|
226
|
+
end
|
227
|
+
end
|