cheap_ams 0.10.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +26 -0
  4. data/CHANGELOG.md +13 -0
  5. data/CONTRIBUTING.md +31 -0
  6. data/Gemfile +35 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +348 -0
  9. data/Rakefile +12 -0
  10. data/appveyor.yml +25 -0
  11. data/cheap_ams.gemspec +49 -0
  12. data/docs/README.md +27 -0
  13. data/docs/general/adapters.md +51 -0
  14. data/docs/general/getting_started.md +73 -0
  15. data/docs/howto/add_pagination_links.md +112 -0
  16. data/docs/howto/add_root_key.md +51 -0
  17. data/lib/action_controller/serialization.rb +62 -0
  18. data/lib/active_model/serializable_resource.rb +84 -0
  19. data/lib/active_model/serializer/adapter/flatten_json.rb +19 -0
  20. data/lib/active_model/serializer/adapter/fragment_cache.rb +82 -0
  21. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +16 -0
  22. data/lib/active_model/serializer/adapter/json.rb +53 -0
  23. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +24 -0
  24. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
  25. data/lib/active_model/serializer/adapter/json_api.rb +183 -0
  26. data/lib/active_model/serializer/adapter/null.rb +11 -0
  27. data/lib/active_model/serializer/adapter.rb +98 -0
  28. data/lib/active_model/serializer/array_serializer.rb +35 -0
  29. data/lib/active_model/serializer/association.rb +21 -0
  30. data/lib/active_model/serializer/associations.rb +97 -0
  31. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  32. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  33. data/lib/active_model/serializer/configuration.rb +14 -0
  34. data/lib/active_model/serializer/fieldset.rb +42 -0
  35. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  36. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  37. data/lib/active_model/serializer/lint.rb +131 -0
  38. data/lib/active_model/serializer/railtie.rb +9 -0
  39. data/lib/active_model/serializer/reflection.rb +74 -0
  40. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  41. data/lib/active_model/serializer/version.rb +5 -0
  42. data/lib/active_model/serializer.rb +205 -0
  43. data/lib/active_model_serializers.rb +29 -0
  44. data/lib/generators/serializer/USAGE +6 -0
  45. data/lib/generators/serializer/resource_override.rb +12 -0
  46. data/lib/generators/serializer/serializer_generator.rb +37 -0
  47. data/lib/generators/serializer/templates/serializer.rb +8 -0
  48. data/test/action_controller/adapter_selector_test.rb +53 -0
  49. data/test/action_controller/explicit_serializer_test.rb +134 -0
  50. data/test/action_controller/json_api/linked_test.rb +180 -0
  51. data/test/action_controller/json_api/pagination_test.rb +116 -0
  52. data/test/action_controller/serialization_scope_name_test.rb +67 -0
  53. data/test/action_controller/serialization_test.rb +426 -0
  54. data/test/adapter/fragment_cache_test.rb +37 -0
  55. data/test/adapter/json/belongs_to_test.rb +47 -0
  56. data/test/adapter/json/collection_test.rb +82 -0
  57. data/test/adapter/json/has_many_test.rb +47 -0
  58. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  59. data/test/adapter/json_api/collection_test.rb +96 -0
  60. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  61. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  62. data/test/adapter/json_api/has_many_test.rb +145 -0
  63. data/test/adapter/json_api/has_one_test.rb +81 -0
  64. data/test/adapter/json_api/json_api_test.rb +38 -0
  65. data/test/adapter/json_api/linked_test.rb +283 -0
  66. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  67. data/test/adapter/json_api/resource_type_config_test.rb +59 -0
  68. data/test/adapter/json_test.rb +47 -0
  69. data/test/adapter/null_test.rb +25 -0
  70. data/test/adapter_test.rb +52 -0
  71. data/test/array_serializer_test.rb +97 -0
  72. data/test/capture_warnings.rb +57 -0
  73. data/test/fixtures/active_record.rb +57 -0
  74. data/test/fixtures/poro.rb +266 -0
  75. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  76. data/test/generators/serializer_generator_test.rb +56 -0
  77. data/test/lint_test.rb +44 -0
  78. data/test/poro_test.rb +9 -0
  79. data/test/serializable_resource_test.rb +27 -0
  80. data/test/serializers/adapter_for_test.rb +50 -0
  81. data/test/serializers/association_macros_test.rb +36 -0
  82. data/test/serializers/associations_test.rb +150 -0
  83. data/test/serializers/attribute_test.rb +62 -0
  84. data/test/serializers/attributes_test.rb +63 -0
  85. data/test/serializers/cache_test.rb +164 -0
  86. data/test/serializers/configuration_test.rb +15 -0
  87. data/test/serializers/fieldset_test.rb +26 -0
  88. data/test/serializers/meta_test.rb +121 -0
  89. data/test/serializers/options_test.rb +21 -0
  90. data/test/serializers/root_test.rb +23 -0
  91. data/test/serializers/serializer_for_test.rb +65 -0
  92. data/test/serializers/urls_test.rb +26 -0
  93. data/test/support/rails_app.rb +21 -0
  94. data/test/support/stream_capture.rb +49 -0
  95. data/test/support/test_case.rb +5 -0
  96. data/test/test_helper.rb +41 -0
  97. metadata +287 -0
@@ -0,0 +1,150 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AssociationsTest < Minitest::Test
6
+ class Model
7
+ def initialize(hash={})
8
+ @attributes = hash
9
+ end
10
+
11
+ def read_attribute_for_serialization(name)
12
+ @attributes[name]
13
+ end
14
+
15
+ def method_missing(meth, *args)
16
+ if meth.to_s =~ /^(.*)=$/
17
+ @attributes[$1.to_sym] = args[0]
18
+ elsif @attributes.key?(meth)
19
+ @attributes[meth]
20
+ else
21
+ super
22
+ end
23
+ end
24
+ end
25
+
26
+ def setup
27
+ @author = Author.new(name: 'Steve K.')
28
+ @author.bio = nil
29
+ @author.roles = []
30
+ @blog = Blog.new({ name: 'AMS Blog' })
31
+ @post = Post.new({ title: 'New Post', body: 'Body' })
32
+ @tag = Tag.new({name: '#hashtagged'})
33
+ @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
34
+ @post.comments = [@comment]
35
+ @post.tags = [@tag]
36
+ @post.blog = @blog
37
+ @comment.post = @post
38
+ @comment.author = nil
39
+ @post.author = @author
40
+ @author.posts = [@post]
41
+
42
+ @post_serializer = PostSerializer.new(@post, {custom_options: true})
43
+ @author_serializer = AuthorSerializer.new(@author)
44
+ @comment_serializer = CommentSerializer.new(@comment)
45
+ end
46
+
47
+ def test_has_many_and_has_one
48
+ @author_serializer.associations.each do |association|
49
+ key = association.key
50
+ serializer = association.serializer
51
+ options = association.options
52
+
53
+ case key
54
+ when :posts
55
+ assert_equal({}, options)
56
+ assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
57
+ when :bio
58
+ assert_equal({}, options)
59
+ assert_nil serializer
60
+ when :roles
61
+ assert_equal({}, options)
62
+ assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
63
+ else
64
+ flunk "Unknown association: #{key}"
65
+ end
66
+ end
67
+ end
68
+
69
+ def test_has_many_with_no_serializer
70
+ PostWithTagsSerializer.new(@post).associations.each do |association|
71
+ key = association.key
72
+ serializer = association.serializer
73
+ options = association.options
74
+
75
+ assert_equal key, :tags
76
+ assert_equal serializer, nil
77
+ assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json
78
+ end
79
+ end
80
+
81
+ def test_serializer_options_are_passed_into_associations_serializers
82
+ association = @post_serializer
83
+ .associations
84
+ .detect { |assoc| assoc.key == :comments }
85
+
86
+ assert association.serializer.first.custom_options[:custom_options]
87
+ end
88
+
89
+ def test_belongs_to
90
+ @comment_serializer.associations.each do |association|
91
+ key = association.key
92
+ serializer = association.serializer
93
+
94
+ case key
95
+ when :post
96
+ assert_kind_of(PostSerializer, serializer)
97
+ when :author
98
+ assert_nil serializer
99
+ else
100
+ flunk "Unknown association: #{key}"
101
+ end
102
+
103
+ assert_equal({}, association.options)
104
+ end
105
+ end
106
+
107
+ def test_belongs_to_with_custom_method
108
+ assert(
109
+ @post_serializer.associations.any? do |association|
110
+ association.key == :blog
111
+ end
112
+ )
113
+ end
114
+
115
+ def test_associations_inheritance
116
+ inherited_klass = Class.new(PostSerializer)
117
+
118
+ assert_equal(PostSerializer._reflections, inherited_klass._reflections)
119
+ end
120
+
121
+ def test_associations_inheritance_with_new_association
122
+ inherited_klass = Class.new(PostSerializer) do
123
+ has_many :top_comments, serializer: CommentSerializer
124
+ end
125
+
126
+ assert(
127
+ PostSerializer._reflections.all? do |reflection|
128
+ inherited_klass._reflections.include?(reflection)
129
+ end
130
+ )
131
+
132
+ assert(
133
+ inherited_klass._reflections.any? do |reflection|
134
+ reflection.name == :top_comments
135
+ end
136
+ )
137
+ end
138
+
139
+ def test_associations_custom_keys
140
+ serializer = PostWithCustomKeysSerializer.new(@post)
141
+
142
+ expected_association_keys = serializer.associations.map(&:key)
143
+
144
+ assert expected_association_keys.include? :reviews
145
+ assert expected_association_keys.include? :writer
146
+ assert expected_association_keys.include? :site
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AttributeTest < Minitest::Test
6
+ def setup
7
+ @blog = Blog.new({ id: 1, name: 'AMS Hints', type: "stuff" })
8
+ @blog_serializer = AlternateBlogSerializer.new(@blog)
9
+ end
10
+
11
+ def test_attributes_definition
12
+ assert_equal([:id, :title],
13
+ @blog_serializer.class._attributes)
14
+ end
15
+
16
+ def test_json_serializable_hash
17
+ adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
18
+ assert_equal({blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
19
+ end
20
+
21
+ def test_attribute_inheritance_with_key
22
+ inherited_klass = Class.new(AlternateBlogSerializer)
23
+ blog_serializer = inherited_klass.new(@blog)
24
+ adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(blog_serializer)
25
+ assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash)
26
+ end
27
+
28
+ def test_multiple_calls_with_the_same_attribute
29
+ serializer_class = Class.new(ActiveModel::Serializer) do
30
+ attribute :title
31
+ attribute :title
32
+ end
33
+
34
+ assert_equal([:title], serializer_class._attributes)
35
+ end
36
+
37
+ def test_id_attribute_override
38
+ serializer = Class.new(ActiveModel::Serializer) do
39
+ attribute :name, key: :id
40
+ end
41
+
42
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
43
+ assert_equal({ blog: { id: "AMS Hints" } }, adapter.serializable_hash)
44
+ end
45
+
46
+ def test_type_attribute
47
+ attribute_serializer = Class.new(ActiveModel::Serializer) do
48
+ attribute :id, key: :type
49
+ end
50
+ attributes_serializer = Class.new(ActiveModel::Serializer) do
51
+ attributes :type
52
+ end
53
+
54
+ adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog))
55
+ assert_equal({ blog: { type: 1} }, adapter.serializable_hash)
56
+
57
+ adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
58
+ assert_equal({ blog: { type: "stuff" } }, adapter.serializable_hash)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AttributesTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ @profile_serializer = ProfileSerializer.new(@profile)
9
+ @comment = Comment.new(id: 1, body: "ZOMG!!", date: "2015")
10
+ @serializer_klass = Class.new(CommentSerializer)
11
+ @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
12
+ attributes :date, :likes
13
+ end
14
+ end
15
+
16
+ def test_attributes_definition
17
+ assert_equal([:name, :description],
18
+ @profile_serializer.class._attributes)
19
+ end
20
+
21
+ def test_attributes_with_fields_option
22
+ assert_equal({name: 'Name 1'},
23
+ @profile_serializer.attributes(fields: [:name]))
24
+ end
25
+
26
+ def test_required_fields
27
+ assert_equal({name: 'Name 1', description: 'Description 1'},
28
+ @profile_serializer.attributes(fields: [:name, :description], required_fields: [:name]))
29
+
30
+ end
31
+
32
+ def test_attributes_inheritance_definition
33
+ assert_equal([:id, :body], @serializer_klass._attributes)
34
+ end
35
+
36
+ def test_attributes_inheritance
37
+ serializer = @serializer_klass.new(@comment)
38
+ assert_equal({id: 1, body: "ZOMG!!"},
39
+ serializer.attributes)
40
+ end
41
+
42
+ def test_attribute_inheritance_with_new_attribute_definition
43
+ assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
44
+ assert_equal([:id, :body], CommentSerializer._attributes)
45
+ end
46
+
47
+ def test_attribute_inheritance_with_new_attribute
48
+ serializer = @serializer_klass_with_new_attributes.new(@comment)
49
+ assert_equal({id: 1, body: "ZOMG!!", date: "2015", likes: nil},
50
+ serializer.attributes)
51
+ end
52
+
53
+ def test_multiple_calls_with_the_same_attribute
54
+ serializer_class = Class.new(ActiveModel::Serializer) do
55
+ attributes :id, :title
56
+ attributes :id, :title, :title, :body
57
+ end
58
+
59
+ assert_equal([:id, :title, :body], serializer_class._attributes)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,164 @@
1
+ require 'test_helper'
2
+ require 'tempfile'
3
+ module ActiveModel
4
+ class Serializer
5
+ class CacheTest < Minitest::Test
6
+ def setup
7
+ ActionController::Base.cache_store.clear
8
+ @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
9
+ @post = Post.new(title: 'New Post', body: 'Body')
10
+ @bio = Bio.new(id: 1, content: 'AMS Contributor')
11
+ @author = Author.new(name: 'Joao M. D. Moura')
12
+ @blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: [])
13
+ @role = Role.new(name: 'Great Author')
14
+ @location = Location.new(lat: '-23.550520', lng: '-46.633309')
15
+ @place = Place.new(name: 'Amazing Place')
16
+ @author.posts = [@post]
17
+ @author.roles = [@role]
18
+ @role.author = @author
19
+ @author.bio = @bio
20
+ @bio.author = @author
21
+ @post.comments = [@comment]
22
+ @post.author = @author
23
+ @comment.post = @post
24
+ @comment.author = @author
25
+ @post.blog = @blog
26
+ @location.place = @place
27
+
28
+ @location_serializer = LocationSerializer.new(@location)
29
+ @bio_serializer = BioSerializer.new(@bio)
30
+ @role_serializer = RoleSerializer.new(@role)
31
+ @post_serializer = PostSerializer.new(@post)
32
+ @author_serializer = AuthorSerializer.new(@author)
33
+ @comment_serializer = CommentSerializer.new(@comment)
34
+ @blog_serializer = BlogSerializer.new(@blog)
35
+ end
36
+
37
+ def test_cache_definition
38
+ assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
39
+ assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
40
+ assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
41
+ end
42
+
43
+ def test_cache_key_definition
44
+ assert_equal('post', @post_serializer.class._cache_key)
45
+ assert_equal('writer', @author_serializer.class._cache_key)
46
+ assert_equal(nil, @comment_serializer.class._cache_key)
47
+ end
48
+
49
+ def test_cache_key_interpolation_with_updated_at
50
+ render_object_with_cache(@author)
51
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
52
+ assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
53
+ end
54
+
55
+ def test_default_cache_key_fallback
56
+ render_object_with_cache(@comment)
57
+ assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
58
+ end
59
+
60
+ def test_cache_options_definition
61
+ assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
62
+ assert_equal(nil, @blog_serializer.class._cache_options)
63
+ assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
64
+ end
65
+
66
+ def test_fragment_cache_definition
67
+ assert_equal([:name], @role_serializer.class._cache_only)
68
+ assert_equal([:content], @bio_serializer.class._cache_except)
69
+ end
70
+
71
+ def test_associations_separately_cache
72
+ ActionController::Base.cache_store.clear
73
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
74
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
75
+
76
+ Timecop.freeze(Time.now) do
77
+ render_object_with_cache(@post)
78
+
79
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
80
+ assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
81
+ end
82
+ end
83
+
84
+ def test_associations_cache_when_updated
85
+ # Clean the Cache
86
+ ActionController::Base.cache_store.clear
87
+
88
+ Timecop.freeze(Time.now) do
89
+ # Generate a new Cache of Post object and each objects related to it.
90
+ render_object_with_cache(@post)
91
+
92
+ # Check if it cached the objects separately
93
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
94
+ assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
95
+
96
+ # Simulating update on comments relationship with Post
97
+ new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
98
+ new_comment_serializer = CommentSerializer.new(new_comment)
99
+ @post.comments = [new_comment]
100
+
101
+ # Ask for the serialized object
102
+ render_object_with_cache(@post)
103
+
104
+ # Check if the the new comment was cached
105
+ assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
106
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
107
+ end
108
+ end
109
+
110
+ def test_fragment_fetch_with_virtual_associations
111
+ expected_result = {
112
+ id: @location.id,
113
+ lat: @location.lat,
114
+ lng: @location.lng,
115
+ place: 'Nowhere'
116
+ }
117
+
118
+ hash = render_object_with_cache(@location)
119
+
120
+ assert_equal(hash, expected_result)
121
+ assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
122
+ end
123
+
124
+ def test_uses_file_digest_in_cache_key
125
+ render_object_with_cache(@blog)
126
+ assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
127
+ end
128
+
129
+ def test_cache_digest_definition
130
+ assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
131
+ end
132
+
133
+ def test_serializer_file_path_on_nix
134
+ path = "/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
135
+ caller_line = "#{path}:1:in `<top (required)>'"
136
+ assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
137
+ end
138
+
139
+ def test_serializer_file_path_on_windows
140
+ path = "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
141
+ caller_line = "#{path}:1:in `<top (required)>'"
142
+ assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
143
+ end
144
+
145
+ def test_digest_caller_file
146
+ contents = "puts 'AMS rocks'!"
147
+ file = Tempfile.new("some_ruby.rb")
148
+ file.write(contents)
149
+ path = file.path
150
+ caller_line = "#{path}:1:in `<top (required)>'"
151
+ file.close
152
+ assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents)
153
+ ensure
154
+ file.unlink
155
+ end
156
+
157
+ private
158
+ def render_object_with_cache(obj)
159
+ ActiveModel::SerializableResource.new(obj).serializable_hash
160
+ end
161
+ end
162
+ end
163
+ end
164
+
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class ConfigurationTest < Minitest::Test
6
+ def test_array_serializer
7
+ assert_equal ActiveModel::Serializer::ArraySerializer, ActiveModel::Serializer.config.array_serializer
8
+ end
9
+
10
+ def test_default_adapter
11
+ assert_equal :flatten_json, ActiveModel::Serializer.config.adapter
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class FieldsetTest < Minitest::Test
6
+
7
+ def test_fieldset_with_hash
8
+ fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']})
9
+
10
+ assert_equal(
11
+ {:post=>[:id, :title], :coment=>[:body]},
12
+ fieldset.fields
13
+ )
14
+ end
15
+
16
+ def test_fieldset_with_array_of_fields_and_root_name
17
+ fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
18
+
19
+ assert_equal(
20
+ {:post => [:title]},
21
+ fieldset.fields
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,121 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class MetaTest < Minitest::Test
6
+ def setup
7
+ ActionController::Base.cache_store.clear
8
+ @blog = Blog.new(id: 1,
9
+ name: 'AMS Hints',
10
+ writer: Author.new(id: 2, name: "Steve"),
11
+ articles: [Post.new(id: 3, title: "AMS")])
12
+ end
13
+
14
+ def test_meta_is_present_with_root
15
+ serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10})
16
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
17
+ expected = {
18
+ blog: {
19
+ id: 1,
20
+ title: "AMS Hints"
21
+ },
22
+ "meta" => {
23
+ total: 10
24
+ }
25
+ }
26
+ assert_equal expected, adapter.as_json
27
+ end
28
+
29
+ def test_meta_is_not_included_when_root_is_missing
30
+ # load_adapter uses FlattenJson Adapter
31
+ adapter = load_adapter(meta: {total: 10})
32
+ expected = {
33
+ id: 1,
34
+ title: "AMS Hints"
35
+ }
36
+ assert_equal expected, adapter.as_json
37
+ end
38
+
39
+ def test_meta_key_is_used
40
+ serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
41
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
42
+ expected = {
43
+ blog: {
44
+ id: 1,
45
+ title: "AMS Hints"
46
+ },
47
+ "haha_meta" => {
48
+ total: 10
49
+ }
50
+ }
51
+ assert_equal expected, adapter.as_json
52
+ end
53
+
54
+ def test_meta_key_is_used_with_json_api
55
+ serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
56
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
57
+ expected = {
58
+ data: {
59
+ id: "1",
60
+ type: "blogs",
61
+ attributes: { title: "AMS Hints" }
62
+ },
63
+ "haha_meta" => { total: 10 }
64
+ }
65
+ assert_equal expected, adapter.as_json
66
+ end
67
+
68
+ def test_meta_is_not_present_on_arrays_without_root
69
+ serializer = ArraySerializer.new([@blog], meta: {total: 10})
70
+ # FlattenJSON doesn't have support to root
71
+ adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
72
+ expected = [{
73
+ id: 1,
74
+ name: "AMS Hints",
75
+ writer: {
76
+ id: 2,
77
+ name: "Steve"
78
+ },
79
+ articles: [{
80
+ id: 3,
81
+ title: "AMS",
82
+ body: nil
83
+ }]
84
+ }]
85
+ assert_equal expected, adapter.as_json
86
+ end
87
+
88
+ def test_meta_is_present_on_arrays_with_root
89
+ serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
90
+ # JSON adapter adds root by default
91
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
92
+ expected = {
93
+ blogs: [{
94
+ id: 1,
95
+ name: "AMS Hints",
96
+ writer: {
97
+ id: 2,
98
+ name: "Steve"
99
+ },
100
+ articles: [{
101
+ id: 3,
102
+ title: "AMS",
103
+ body: nil
104
+ }]
105
+ }],
106
+ 'haha_meta' => {
107
+ total: 10
108
+ }
109
+ }
110
+ assert_equal expected, adapter.as_json
111
+ end
112
+
113
+ private
114
+
115
+ def load_adapter(options)
116
+ options = options.merge(adapter: :flatten_json, serializer: AlternateBlogSerializer)
117
+ ActiveModel::SerializableResource.new(@blog, options)
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class OptionsTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new(name: 'Name 1', description: 'Description 1')
8
+ end
9
+
10
+ def test_options_are_accessible
11
+ @profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
12
+ assert @profile_serializer.arguments_passed_in?
13
+ end
14
+
15
+ def test_no_option_is_passed_in
16
+ @profile_serializer = ProfileSerializer.new(@profile)
17
+ refute @profile_serializer.arguments_passed_in?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class RootTest < Minitest::Test
6
+
7
+ def setup
8
+ @virtual_value = VirtualValue.new(id: 1)
9
+ end
10
+
11
+ def test_overwrite_root
12
+ serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'})
13
+ assert_equal('smth', serializer.json_key)
14
+ end
15
+
16
+ def test_underscore_in_root
17
+ serializer = VirtualValueSerializer.new(@virtual_value)
18
+ assert_equal('virtual_value', serializer.json_key)
19
+ end
20
+
21
+ end
22
+ end
23
+ end