active_model_serializers 0.10.9 → 0.10.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -2
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer.rb +10 -2
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers/adapter/attributes.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
- metadata +12 -259
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -109
- data/.simplecov +0 -110
- data/.travis.yml +0 -63
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -74
- data/Rakefile +0 -76
- data/active_model_serializers.gemspec +0 -64
- data/appveyor.yml +0 -28
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -269
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -185
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -495
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -140
- data/docs/howto/add_root_key.md +0 -62
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -147
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema.md +0 -151
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/test/action_controller/adapter_selector_test.rb +0 -64
- data/test/action_controller/explicit_serializer_test.rb +0 -137
- data/test/action_controller/json/include_test.rb +0 -248
- data/test/action_controller/json_api/deserialization_test.rb +0 -114
- data/test/action_controller/json_api/errors_test.rb +0 -42
- data/test/action_controller/json_api/fields_test.rb +0 -68
- data/test/action_controller/json_api/linked_test.rb +0 -204
- data/test/action_controller/json_api/pagination_test.rb +0 -126
- data/test/action_controller/json_api/transform_test.rb +0 -191
- data/test/action_controller/lookup_proc_test.rb +0 -51
- data/test/action_controller/namespace_lookup_test.rb +0 -234
- data/test/action_controller/serialization_scope_name_test.rb +0 -237
- data/test/action_controller/serialization_test.rb +0 -480
- data/test/active_model_serializers/adapter_for_test.rb +0 -210
- data/test/active_model_serializers/json_pointer_test.rb +0 -24
- data/test/active_model_serializers/logging_test.rb +0 -79
- data/test/active_model_serializers/model_test.rb +0 -144
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
- data/test/active_model_serializers/test/schema_test.rb +0 -133
- data/test/active_model_serializers/test/serializer_test.rb +0 -64
- data/test/active_record_test.rb +0 -11
- data/test/adapter/attributes_test.rb +0 -42
- data/test/adapter/deprecation_test.rb +0 -102
- data/test/adapter/json/belongs_to_test.rb +0 -47
- data/test/adapter/json/collection_test.rb +0 -106
- data/test/adapter/json/has_many_test.rb +0 -55
- data/test/adapter/json/transform_test.rb +0 -95
- data/test/adapter/json_api/belongs_to_test.rb +0 -157
- data/test/adapter/json_api/collection_test.rb +0 -98
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -98
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
- data/test/adapter/json_api/has_many_test.rb +0 -175
- data/test/adapter/json_api/has_one_test.rb +0 -82
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -415
- data/test/adapter/json_api/links_test.rb +0 -112
- data/test/adapter/json_api/pagination_links_test.rb +0 -208
- data/test/adapter/json_api/parse_test.rb +0 -139
- data/test/adapter/json_api/relationship_test.rb +0 -399
- data/test/adapter/json_api/resource_meta_test.rb +0 -102
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
- data/test/adapter/json_api/transform_test.rb +0 -514
- data/test/adapter/json_api/type_test.rb +0 -195
- data/test/adapter/json_test.rb +0 -48
- data/test/adapter/null_test.rb +0 -24
- data/test/adapter/polymorphic_test.rb +0 -220
- data/test/adapter_test.rb +0 -69
- data/test/array_serializer_test.rb +0 -24
- data/test/benchmark/app.rb +0 -67
- data/test/benchmark/benchmarking_support.rb +0 -69
- data/test/benchmark/bm_active_record.rb +0 -83
- data/test/benchmark/bm_adapter.rb +0 -40
- data/test/benchmark/bm_caching.rb +0 -121
- data/test/benchmark/bm_lookup_chain.rb +0 -85
- data/test/benchmark/bm_transform.rb +0 -47
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -85
- data/test/benchmark/fixtures.rb +0 -221
- data/test/cache_test.rb +0 -717
- data/test/collection_serializer_test.rb +0 -129
- data/test/fixtures/active_record.rb +0 -115
- data/test/fixtures/poro.rb +0 -227
- data/test/generators/scaffold_controller_generator_test.rb +0 -26
- data/test/generators/serializer_generator_test.rb +0 -77
- data/test/grape_test.rb +0 -198
- data/test/lint_test.rb +0 -51
- data/test/logger_test.rb +0 -22
- data/test/poro_test.rb +0 -11
- data/test/serializable_resource_test.rb +0 -81
- data/test/serializers/association_macros_test.rb +0 -39
- data/test/serializers/associations_test.rb +0 -520
- data/test/serializers/attribute_test.rb +0 -155
- data/test/serializers/attributes_test.rb +0 -54
- data/test/serializers/caching_configuration_test_isolated.rb +0 -172
- data/test/serializers/configuration_test.rb +0 -34
- data/test/serializers/fieldset_test.rb +0 -16
- data/test/serializers/meta_test.rb +0 -204
- data/test/serializers/options_test.rb +0 -34
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
- data/test/serializers/reflection_test.rb +0 -481
- data/test/serializers/root_test.rb +0 -23
- data/test/serializers/serialization_test.rb +0 -57
- data/test/serializers/serializer_for_test.rb +0 -138
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -86
- data/test/support/rails5_shims.rb +0 -55
- data/test/support/rails_app.rb +0 -40
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -81
- data/test/test_helper.rb +0 -72
@@ -1,155 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
class AttributeTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@blog = Blog.new(id: 1, name: 'AMS Hints', type: 'stuff')
|
10
|
-
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_attributes_definition
|
14
|
-
assert_equal([:id, :title],
|
15
|
-
@blog_serializer.class._attributes)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_json_serializable_hash
|
19
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(@blog_serializer)
|
20
|
-
assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_attribute_inheritance_with_key
|
24
|
-
inherited_klass = Class.new(AlternateBlogSerializer)
|
25
|
-
blog_serializer = inherited_klass.new(@blog)
|
26
|
-
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
27
|
-
assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_multiple_calls_with_the_same_attribute
|
31
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
32
|
-
attribute :title
|
33
|
-
attribute :title
|
34
|
-
end
|
35
|
-
|
36
|
-
assert_equal([:title], serializer_class._attributes)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_id_attribute_override
|
40
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
41
|
-
attribute :name, key: :id
|
42
|
-
end
|
43
|
-
|
44
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
|
45
|
-
assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_object_attribute_override
|
49
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
50
|
-
attribute :name, key: :object
|
51
|
-
end
|
52
|
-
|
53
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
|
54
|
-
assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_type_attribute
|
58
|
-
attribute_serializer = Class.new(ActiveModel::Serializer) do
|
59
|
-
attribute :id, key: :type
|
60
|
-
end
|
61
|
-
attributes_serializer = Class.new(ActiveModel::Serializer) do
|
62
|
-
attributes :type
|
63
|
-
end
|
64
|
-
|
65
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(attribute_serializer.new(@blog))
|
66
|
-
assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
|
67
|
-
|
68
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(attributes_serializer.new(@blog))
|
69
|
-
assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_id_attribute_override_before
|
73
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
74
|
-
def id
|
75
|
-
'custom'
|
76
|
-
end
|
77
|
-
|
78
|
-
attribute :id
|
79
|
-
end
|
80
|
-
|
81
|
-
hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
|
82
|
-
|
83
|
-
assert_equal('custom', hash[:blog][:id])
|
84
|
-
end
|
85
|
-
|
86
|
-
class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
|
87
|
-
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
|
88
|
-
attribute :name do
|
89
|
-
"#{object.first_name} #{object.last_name}"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_virtual_attribute_block
|
94
|
-
post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
|
95
|
-
hash = serializable(post).serializable_hash
|
96
|
-
expected = { name: 'Lucas Hosseini' }
|
97
|
-
|
98
|
-
assert_equal(expected, hash)
|
99
|
-
end
|
100
|
-
|
101
|
-
# rubocop:disable Metrics/AbcSize
|
102
|
-
def test_conditional_associations
|
103
|
-
model = Class.new(::Model) do
|
104
|
-
attributes :true, :false, :attribute
|
105
|
-
end.new(true: true, false: false)
|
106
|
-
|
107
|
-
scenarios = [
|
108
|
-
{ options: { if: :true }, included: true },
|
109
|
-
{ options: { if: :false }, included: false },
|
110
|
-
{ options: { unless: :false }, included: true },
|
111
|
-
{ options: { unless: :true }, included: false },
|
112
|
-
{ options: { if: 'object.true' }, included: true },
|
113
|
-
{ options: { if: 'object.false' }, included: false },
|
114
|
-
{ options: { unless: 'object.false' }, included: true },
|
115
|
-
{ options: { unless: 'object.true' }, included: false },
|
116
|
-
{ options: { if: -> { object.true } }, included: true },
|
117
|
-
{ options: { if: -> { object.false } }, included: false },
|
118
|
-
{ options: { unless: -> { object.false } }, included: true },
|
119
|
-
{ options: { unless: -> { object.true } }, included: false },
|
120
|
-
{ options: { if: -> (s) { s.object.true } }, included: true },
|
121
|
-
{ options: { if: -> (s) { s.object.false } }, included: false },
|
122
|
-
{ options: { unless: -> (s) { s.object.false } }, included: true },
|
123
|
-
{ options: { unless: -> (s) { s.object.true } }, included: false }
|
124
|
-
]
|
125
|
-
|
126
|
-
scenarios.each do |s|
|
127
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
128
|
-
attribute :attribute, s[:options]
|
129
|
-
|
130
|
-
def true
|
131
|
-
true
|
132
|
-
end
|
133
|
-
|
134
|
-
def false
|
135
|
-
false
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
hash = serializable(model, serializer: serializer).serializable_hash
|
140
|
-
assert_equal(s[:included], hash.key?(:attribute), "Error with #{s[:options]}")
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_illegal_conditional_attributes
|
145
|
-
exception = assert_raises(TypeError) do
|
146
|
-
Class.new(ActiveModel::Serializer) do
|
147
|
-
attribute :x, if: nil
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
assert_match(/:if should be a Symbol, String or Proc/, exception.message)
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
class AttributesTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
10
|
-
@profile_serializer = ProfileSerializer.new(@profile)
|
11
|
-
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
12
|
-
@serializer_klass = Class.new(CommentSerializer)
|
13
|
-
@serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
|
14
|
-
attributes :date, :likes
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_attributes_definition
|
19
|
-
assert_equal([:name, :description],
|
20
|
-
@profile_serializer.class._attributes)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_attributes_inheritance_definition
|
24
|
-
assert_equal([:id, :body], @serializer_klass._attributes)
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_attributes_inheritance
|
28
|
-
serializer = @serializer_klass.new(@comment)
|
29
|
-
assert_equal({ id: 1, body: 'ZOMG!!' },
|
30
|
-
serializer.attributes)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_attribute_inheritance_with_new_attribute_definition
|
34
|
-
assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
|
35
|
-
assert_equal([:id, :body], CommentSerializer._attributes)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_attribute_inheritance_with_new_attribute
|
39
|
-
serializer = @serializer_klass_with_new_attributes.new(@comment)
|
40
|
-
assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
|
41
|
-
serializer.attributes)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_multiple_calls_with_the_same_attribute
|
45
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
46
|
-
attributes :id, :title
|
47
|
-
attributes :id, :title, :title, :body
|
48
|
-
end
|
49
|
-
|
50
|
-
assert_equal([:id, :title, :body], serializer_class._attributes)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,172 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Execute this test in isolation
|
4
|
-
require 'support/isolated_unit'
|
5
|
-
|
6
|
-
class CachingConfigurationTest < ActiveSupport::TestCase
|
7
|
-
include ActiveSupport::Testing::Isolation
|
8
|
-
|
9
|
-
setup do
|
10
|
-
require 'rails'
|
11
|
-
# AMS needs to be required before Rails.application is initialized for
|
12
|
-
# Railtie's to fire in Rails.application.initialize!
|
13
|
-
# (and make_basic_app initializes the app)
|
14
|
-
require 'active_model_serializers'
|
15
|
-
# Create serializers before Rails.application.initialize!
|
16
|
-
# To ensure we're testing that the cache settings depend on
|
17
|
-
# the Railtie firing, not on the ActionController being loaded.
|
18
|
-
create_serializers
|
19
|
-
end
|
20
|
-
|
21
|
-
def create_serializers
|
22
|
-
@cached_serializer = Class.new(ActiveModel::Serializer) do
|
23
|
-
cache skip_digest: true
|
24
|
-
attributes :id, :name, :title
|
25
|
-
end
|
26
|
-
@fragment_cached_serializer = Class.new(ActiveModel::Serializer) do
|
27
|
-
cache only: :id
|
28
|
-
attributes :id, :name, :title
|
29
|
-
end
|
30
|
-
@non_cached_serializer = Class.new(ActiveModel::Serializer) do
|
31
|
-
attributes :id, :name, :title
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class PerformCachingTrue < CachingConfigurationTest
|
36
|
-
setup do
|
37
|
-
# Let's make that Rails app and initialize it!
|
38
|
-
make_basic_app do |app|
|
39
|
-
app.config.action_controller.perform_caching = true
|
40
|
-
app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
|
41
|
-
end
|
42
|
-
controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
|
43
|
-
end
|
44
|
-
|
45
|
-
test 'it sets perform_caching to true on AMS.config and serializers' do
|
46
|
-
assert Rails.configuration.action_controller.perform_caching
|
47
|
-
assert ActiveModelSerializers.config.perform_caching
|
48
|
-
assert ActiveModel::Serializer.perform_caching?
|
49
|
-
assert @cached_serializer.perform_caching?
|
50
|
-
assert @non_cached_serializer.perform_caching?
|
51
|
-
assert @fragment_cached_serializer.perform_caching?
|
52
|
-
end
|
53
|
-
|
54
|
-
test 'it sets the AMS.config.cache_store to the controller cache_store' do
|
55
|
-
assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
|
56
|
-
assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
|
57
|
-
end
|
58
|
-
|
59
|
-
test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
|
60
|
-
assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
|
61
|
-
assert_equal controller_cache_store, @cached_serializer.cache_store.class
|
62
|
-
assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
|
63
|
-
end
|
64
|
-
|
65
|
-
test 'the cached serializer has cache_enabled?' do
|
66
|
-
assert @cached_serializer.cache_enabled?
|
67
|
-
end
|
68
|
-
|
69
|
-
test 'the cached serializer does not have fragment_cache_enabled?' do
|
70
|
-
refute @cached_serializer.fragment_cache_enabled?
|
71
|
-
end
|
72
|
-
|
73
|
-
test 'the non-cached serializer cache_store is nil' do
|
74
|
-
assert_nil @non_cached_serializer._cache
|
75
|
-
assert_nil @non_cached_serializer.cache_store
|
76
|
-
assert_nil @non_cached_serializer._cache
|
77
|
-
end
|
78
|
-
|
79
|
-
test 'the non-cached serializer does not have cache_enabled?' do
|
80
|
-
refute @non_cached_serializer.cache_enabled?
|
81
|
-
end
|
82
|
-
|
83
|
-
test 'the non-cached serializer does not have fragment_cache_enabled?' do
|
84
|
-
refute @non_cached_serializer.fragment_cache_enabled?
|
85
|
-
end
|
86
|
-
|
87
|
-
test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
|
88
|
-
assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
|
89
|
-
assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
|
90
|
-
assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
|
91
|
-
end
|
92
|
-
|
93
|
-
test 'the fragment cached serializer does not have cache_enabled?' do
|
94
|
-
refute @fragment_cached_serializer.cache_enabled?
|
95
|
-
end
|
96
|
-
|
97
|
-
test 'the fragment cached serializer has fragment_cache_enabled?' do
|
98
|
-
assert @fragment_cached_serializer.fragment_cache_enabled?
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
class PerformCachingFalse < CachingConfigurationTest
|
103
|
-
setup do
|
104
|
-
# Let's make that Rails app and initialize it!
|
105
|
-
make_basic_app do |app|
|
106
|
-
app.config.action_controller.perform_caching = false
|
107
|
-
app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
|
108
|
-
end
|
109
|
-
controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
|
110
|
-
end
|
111
|
-
|
112
|
-
test 'it sets perform_caching to false on AMS.config and serializers' do
|
113
|
-
refute Rails.configuration.action_controller.perform_caching
|
114
|
-
refute ActiveModelSerializers.config.perform_caching
|
115
|
-
refute ActiveModel::Serializer.perform_caching?
|
116
|
-
refute @cached_serializer.perform_caching?
|
117
|
-
refute @non_cached_serializer.perform_caching?
|
118
|
-
refute @fragment_cached_serializer.perform_caching?
|
119
|
-
end
|
120
|
-
|
121
|
-
test 'it sets the AMS.config.cache_store to the controller cache_store' do
|
122
|
-
assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
|
123
|
-
assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
|
124
|
-
end
|
125
|
-
|
126
|
-
test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
|
127
|
-
assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
|
128
|
-
assert_equal controller_cache_store, @cached_serializer.cache_store.class
|
129
|
-
assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
|
130
|
-
end
|
131
|
-
|
132
|
-
test 'the cached serializer does not have cache_enabled?' do
|
133
|
-
refute @cached_serializer.cache_enabled?
|
134
|
-
end
|
135
|
-
|
136
|
-
test 'the cached serializer does not have fragment_cache_enabled?' do
|
137
|
-
refute @cached_serializer.fragment_cache_enabled?
|
138
|
-
end
|
139
|
-
|
140
|
-
test 'the non-cached serializer cache_store is nil' do
|
141
|
-
assert_nil @non_cached_serializer._cache
|
142
|
-
assert_nil @non_cached_serializer.cache_store
|
143
|
-
assert_nil @non_cached_serializer._cache
|
144
|
-
end
|
145
|
-
|
146
|
-
test 'the non-cached serializer does not have cache_enabled?' do
|
147
|
-
refute @non_cached_serializer.cache_enabled?
|
148
|
-
end
|
149
|
-
|
150
|
-
test 'the non-cached serializer does not have fragment_cache_enabled?' do
|
151
|
-
refute @non_cached_serializer.fragment_cache_enabled?
|
152
|
-
end
|
153
|
-
|
154
|
-
test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
|
155
|
-
assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
|
156
|
-
assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
|
157
|
-
assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
|
158
|
-
end
|
159
|
-
|
160
|
-
test 'the fragment cached serializer does not have cache_enabled?' do
|
161
|
-
refute @fragment_cached_serializer.cache_enabled?
|
162
|
-
end
|
163
|
-
|
164
|
-
test 'the fragment cached serializer does not have fragment_cache_enabled?' do
|
165
|
-
refute @fragment_cached_serializer.fragment_cache_enabled?
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def controller_cache_store
|
170
|
-
ActionController::Base.cache_store.class
|
171
|
-
end
|
172
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
class ConfigurationTest < ActiveSupport::TestCase
|
8
|
-
def test_collection_serializer
|
9
|
-
assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.collection_serializer
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_array_serializer
|
13
|
-
assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.array_serializer
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_setting_array_serializer_sets_collection_serializer
|
17
|
-
config = ActiveModelSerializers.config
|
18
|
-
old_config = config.dup
|
19
|
-
begin
|
20
|
-
assert_equal ActiveModel::Serializer::CollectionSerializer, config.collection_serializer
|
21
|
-
config.array_serializer = :foo
|
22
|
-
assert_equal config.array_serializer, :foo
|
23
|
-
assert_equal config.collection_serializer, :foo
|
24
|
-
ensure
|
25
|
-
ActiveModelSerializers.config.replace(old_config)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_default_adapter
|
30
|
-
assert_equal :attributes, ActiveModelSerializers.config.adapter
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
class FieldsetTest < ActiveSupport::TestCase
|
8
|
-
def test_fieldset_with_hash
|
9
|
-
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
10
|
-
expected = { post: [:id, :title], comment: [:body] }
|
11
|
-
|
12
|
-
assert_equal(expected, fieldset.fields)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,204 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
class MetaTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@blog = Blog.new(id: 1,
|
10
|
-
name: 'AMS Hints',
|
11
|
-
writer: Author.new(id: 2, name: 'Steve'),
|
12
|
-
articles: [Post.new(id: 3, title: 'AMS')])
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_meta_is_present_with_root
|
16
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
17
|
-
@blog,
|
18
|
-
adapter: :json,
|
19
|
-
serializer: AlternateBlogSerializer,
|
20
|
-
meta: { total: 10 }
|
21
|
-
).as_json
|
22
|
-
expected = {
|
23
|
-
blog: {
|
24
|
-
id: 1,
|
25
|
-
title: 'AMS Hints'
|
26
|
-
},
|
27
|
-
'meta' => {
|
28
|
-
total: 10
|
29
|
-
}
|
30
|
-
}
|
31
|
-
assert_equal(expected, actual)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_meta_is_not_included_when_blank
|
35
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
36
|
-
@blog,
|
37
|
-
adapter: :json,
|
38
|
-
serializer: AlternateBlogSerializer,
|
39
|
-
meta: {}
|
40
|
-
).as_json
|
41
|
-
expected = {
|
42
|
-
blog: {
|
43
|
-
id: 1,
|
44
|
-
title: 'AMS Hints'
|
45
|
-
}
|
46
|
-
}
|
47
|
-
assert_equal(expected, actual)
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_meta_is_not_included_when_empty_string
|
51
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
52
|
-
@blog,
|
53
|
-
adapter: :json,
|
54
|
-
serializer: AlternateBlogSerializer,
|
55
|
-
meta: ''
|
56
|
-
).as_json
|
57
|
-
expected = {
|
58
|
-
blog: {
|
59
|
-
id: 1,
|
60
|
-
title: 'AMS Hints'
|
61
|
-
}
|
62
|
-
}
|
63
|
-
assert_equal(expected, actual)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_meta_is_not_included_when_root_is_missing
|
67
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
68
|
-
@blog,
|
69
|
-
adapter: :attributes,
|
70
|
-
serializer: AlternateBlogSerializer,
|
71
|
-
meta: { total: 10 }
|
72
|
-
).as_json
|
73
|
-
expected = {
|
74
|
-
id: 1,
|
75
|
-
title: 'AMS Hints'
|
76
|
-
}
|
77
|
-
assert_equal(expected, actual)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_meta_key_is_used
|
81
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
82
|
-
@blog,
|
83
|
-
adapter: :json,
|
84
|
-
serializer: AlternateBlogSerializer,
|
85
|
-
meta: { total: 10 },
|
86
|
-
meta_key: 'haha_meta'
|
87
|
-
).as_json
|
88
|
-
expected = {
|
89
|
-
blog: {
|
90
|
-
id: 1,
|
91
|
-
title: 'AMS Hints'
|
92
|
-
},
|
93
|
-
'haha_meta' => {
|
94
|
-
total: 10
|
95
|
-
}
|
96
|
-
}
|
97
|
-
assert_equal(expected, actual)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_meta_key_is_not_used_with_json_api
|
101
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
102
|
-
@blog,
|
103
|
-
adapter: :json_api,
|
104
|
-
serializer: AlternateBlogSerializer,
|
105
|
-
meta: { total: 10 },
|
106
|
-
meta_key: 'haha_meta'
|
107
|
-
).as_json
|
108
|
-
expected = {
|
109
|
-
data: {
|
110
|
-
id: '1',
|
111
|
-
type: 'blogs',
|
112
|
-
attributes: { title: 'AMS Hints' }
|
113
|
-
},
|
114
|
-
meta: { total: 10 }
|
115
|
-
}
|
116
|
-
assert_equal(expected, actual)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_meta_key_is_not_present_when_empty_hash_with_json_api
|
120
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
121
|
-
@blog,
|
122
|
-
adapter: :json_api,
|
123
|
-
serializer: AlternateBlogSerializer,
|
124
|
-
meta: {}
|
125
|
-
).as_json
|
126
|
-
expected = {
|
127
|
-
data: {
|
128
|
-
id: '1',
|
129
|
-
type: 'blogs',
|
130
|
-
attributes: { title: 'AMS Hints' }
|
131
|
-
}
|
132
|
-
}
|
133
|
-
assert_equal(expected, actual)
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_meta_key_is_not_present_when_empty_string_with_json_api
|
137
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
138
|
-
@blog,
|
139
|
-
adapter: :json_api,
|
140
|
-
serializer: AlternateBlogSerializer,
|
141
|
-
meta: ''
|
142
|
-
).as_json
|
143
|
-
expected = {
|
144
|
-
data: {
|
145
|
-
id: '1',
|
146
|
-
type: 'blogs',
|
147
|
-
attributes: { title: 'AMS Hints' }
|
148
|
-
}
|
149
|
-
}
|
150
|
-
assert_equal(expected, actual)
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_meta_is_not_present_on_arrays_without_root
|
154
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
155
|
-
[@blog],
|
156
|
-
adapter: :attributes,
|
157
|
-
meta: { total: 10 }
|
158
|
-
).as_json
|
159
|
-
expected = [{
|
160
|
-
id: 1,
|
161
|
-
name: 'AMS Hints',
|
162
|
-
writer: {
|
163
|
-
id: 2,
|
164
|
-
name: 'Steve'
|
165
|
-
},
|
166
|
-
articles: [{
|
167
|
-
id: 3,
|
168
|
-
title: 'AMS',
|
169
|
-
body: nil
|
170
|
-
}]
|
171
|
-
}]
|
172
|
-
assert_equal(expected, actual)
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_meta_is_present_on_arrays_with_root
|
176
|
-
actual = ActiveModelSerializers::SerializableResource.new(
|
177
|
-
[@blog],
|
178
|
-
adapter: :json,
|
179
|
-
meta: { total: 10 },
|
180
|
-
meta_key: 'haha_meta'
|
181
|
-
).as_json
|
182
|
-
expected = {
|
183
|
-
blogs: [{
|
184
|
-
id: 1,
|
185
|
-
name: 'AMS Hints',
|
186
|
-
writer: {
|
187
|
-
id: 2,
|
188
|
-
name: 'Steve'
|
189
|
-
},
|
190
|
-
articles: [{
|
191
|
-
id: 3,
|
192
|
-
title: 'AMS',
|
193
|
-
body: nil
|
194
|
-
}]
|
195
|
-
}],
|
196
|
-
'haha_meta' => {
|
197
|
-
total: 10
|
198
|
-
}
|
199
|
-
}
|
200
|
-
assert_equal(expected, actual)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|