cm-active_model_serializers 0.10.0.rc1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +26 -0
  4. data/CHANGELOG.md +8 -0
  5. data/CONTRIBUTING.md +31 -0
  6. data/Gemfile +17 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +326 -0
  9. data/Rakefile +12 -0
  10. data/cm-active_model_serializers.gemspec +26 -0
  11. data/lib/action_controller/serialization.rb +62 -0
  12. data/lib/active_model/serializer.rb +261 -0
  13. data/lib/active_model/serializer/adapter.rb +87 -0
  14. data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
  15. data/lib/active_model/serializer/adapter/json.rb +52 -0
  16. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
  17. data/lib/active_model/serializer/adapter/json_api.rb +152 -0
  18. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
  19. data/lib/active_model/serializer/adapter/null.rb +11 -0
  20. data/lib/active_model/serializer/array_serializer.rb +32 -0
  21. data/lib/active_model/serializer/configuration.rb +13 -0
  22. data/lib/active_model/serializer/fieldset.rb +40 -0
  23. data/lib/active_model/serializer/version.rb +5 -0
  24. data/lib/active_model_serializers.rb +18 -0
  25. data/lib/generators/serializer/USAGE +6 -0
  26. data/lib/generators/serializer/serializer_generator.rb +37 -0
  27. data/lib/generators/serializer/templates/serializer.rb +8 -0
  28. data/test/action_controller/adapter_selector_test.rb +51 -0
  29. data/test/action_controller/explicit_serializer_test.rb +110 -0
  30. data/test/action_controller/json_api_linked_test.rb +173 -0
  31. data/test/action_controller/serialization_scope_name_test.rb +63 -0
  32. data/test/action_controller/serialization_test.rb +365 -0
  33. data/test/adapter/fragment_cache_test.rb +27 -0
  34. data/test/adapter/json/belongs_to_test.rb +48 -0
  35. data/test/adapter/json/collection_test.rb +73 -0
  36. data/test/adapter/json/has_many_test.rb +36 -0
  37. data/test/adapter/json_api/belongs_to_test.rb +147 -0
  38. data/test/adapter/json_api/collection_test.rb +89 -0
  39. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  40. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  41. data/test/adapter/json_api/has_many_test.rb +106 -0
  42. data/test/adapter/json_api/has_one_test.rb +59 -0
  43. data/test/adapter/json_api/linked_test.rb +257 -0
  44. data/test/adapter/json_test.rb +34 -0
  45. data/test/adapter/null_test.rb +25 -0
  46. data/test/adapter_test.rb +43 -0
  47. data/test/array_serializer_test.rb +43 -0
  48. data/test/fixtures/poro.rb +213 -0
  49. data/test/serializers/adapter_for_test.rb +50 -0
  50. data/test/serializers/associations_test.rb +127 -0
  51. data/test/serializers/attribute_test.rb +38 -0
  52. data/test/serializers/attributes_test.rb +63 -0
  53. data/test/serializers/cache_test.rb +128 -0
  54. data/test/serializers/configuration_test.rb +15 -0
  55. data/test/serializers/fieldset_test.rb +26 -0
  56. data/test/serializers/generators_test.rb +59 -0
  57. data/test/serializers/meta_test.rb +78 -0
  58. data/test/serializers/options_test.rb +21 -0
  59. data/test/serializers/serializer_for_test.rb +65 -0
  60. data/test/serializers/urls_test.rb +26 -0
  61. data/test/test_helper.rb +38 -0
  62. metadata +195 -0
@@ -0,0 +1,128 @@
1
+ require 'test_helper'
2
+ module ActiveModel
3
+ class Serializer
4
+ class CacheTest < Minitest::Test
5
+ def setup
6
+ ActionController::Base.cache_store.clear
7
+ @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
8
+ @blog = Blog.new(id: 999, name: "Custom blog")
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
+ @role = Role.new(name: 'Great Author')
13
+ @location = Location.new(lat: '-23.550520', lng: '-46.633309')
14
+ @place = Place.new(name: 'Amazing Place')
15
+ @author.posts = [@post]
16
+ @author.roles = [@role]
17
+ @role.author = @author
18
+ @author.bio = @bio
19
+ @bio.author = @author
20
+ @post.comments = [@comment]
21
+ @post.author = @author
22
+ @comment.post = @post
23
+ @comment.author = @author
24
+ @post.blog = @blog
25
+ @location.place = @place
26
+
27
+ @location_serializer = LocationSerializer.new(@location)
28
+ @bio_serializer = BioSerializer.new(@bio)
29
+ @role_serializer = RoleSerializer.new(@role)
30
+ @post_serializer = PostSerializer.new(@post)
31
+ @author_serializer = AuthorSerializer.new(@author)
32
+ @comment_serializer = CommentSerializer.new(@comment)
33
+ end
34
+
35
+ def test_cache_definition
36
+ assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
37
+ assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
38
+ assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
39
+ end
40
+
41
+ def test_cache_key_definition
42
+ assert_equal('post', @post_serializer.class._cache_key)
43
+ assert_equal('writer', @author_serializer.class._cache_key)
44
+ assert_equal(nil, @comment_serializer.class._cache_key)
45
+ end
46
+
47
+ def test_cache_key_interpolation_with_updated_at
48
+ author = render_object_with_cache(@author)
49
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
50
+ 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}").to_json)
51
+ end
52
+
53
+ def test_default_cache_key_fallback
54
+ comment = render_object_with_cache(@comment)
55
+ assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
56
+ end
57
+
58
+ def test_cache_options_definition
59
+ assert_equal({expires_in: 0.1}, @post_serializer.class._cache_options)
60
+ assert_equal(nil, @author_serializer.class._cache_options)
61
+ assert_equal({expires_in: 1.day}, @comment_serializer.class._cache_options)
62
+ end
63
+
64
+ def test_fragment_cache_definition
65
+ assert_equal([:name], @role_serializer.class._cache_only)
66
+ assert_equal([:content], @bio_serializer.class._cache_except)
67
+ end
68
+
69
+ def test_associations_separately_cache
70
+ ActionController::Base.cache_store.clear
71
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
72
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
73
+
74
+ post = render_object_with_cache(@post)
75
+
76
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
77
+ assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
78
+ end
79
+
80
+ def test_associations_cache_when_updated
81
+ # Clean the Cache
82
+ ActionController::Base.cache_store.clear
83
+
84
+ # Generate a new Cache of Post object and each objects related to it.
85
+ render_object_with_cache(@post)
86
+
87
+ # Check if if cache the objects separately
88
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
89
+ assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
90
+
91
+ # Simulating update on comments relationship with Post
92
+ new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
93
+ new_comment_serializer = CommentSerializer.new(new_comment)
94
+ @post.comments = [new_comment]
95
+
96
+ # Ask for the serialized object
97
+ render_object_with_cache(@post)
98
+
99
+ # Check if the the new comment was cached
100
+ assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
101
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
102
+ end
103
+
104
+ def test_fragment_fetch_with_virtual_associations
105
+ expected_result = {
106
+ id: @location.id,
107
+ lat: @location.lat,
108
+ lng: @location.lng,
109
+ place: 'Nowhere'
110
+ }
111
+
112
+ hash = render_object_with_cache(@location)
113
+
114
+ assert_equal(hash, expected_result)
115
+ assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
116
+ end
117
+
118
+ private
119
+ def render_object_with_cache(obj)
120
+ serializer_class = ActiveModel::Serializer.serializer_for(obj)
121
+ serializer = serializer_class.new(obj)
122
+ adapter = ActiveModel::Serializer.adapter.new(serializer)
123
+ adapter.serializable_hash
124
+ end
125
+ end
126
+ end
127
+ end
128
+
@@ -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 :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,59 @@
1
+ class Foo < Rails::Application
2
+ if Rails.version.to_s.start_with? '4'
3
+ config.eager_load = false
4
+ config.secret_key_base = 'abc123'
5
+ end
6
+ end
7
+
8
+ Rails.application.load_generators
9
+
10
+ require 'generators/serializer/serializer_generator'
11
+
12
+ class SerializerGeneratorTest < Rails::Generators::TestCase
13
+ destination File.expand_path("../tmp", __FILE__)
14
+ setup :prepare_destination
15
+
16
+ tests Rails::Generators::SerializerGenerator
17
+ arguments %w(account name:string description:text business:references)
18
+
19
+ def test_generates_a_serializer
20
+ run_generator
21
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
22
+ end
23
+
24
+ def test_generates_a_namespaced_serializer
25
+ run_generator ["admin/account"]
26
+ assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
27
+ end
28
+
29
+ def test_uses_application_serializer_if_one_exists
30
+ Object.const_set(:ApplicationSerializer, Class.new)
31
+ run_generator
32
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
33
+ ensure
34
+ Object.send :remove_const, :ApplicationSerializer
35
+ end
36
+
37
+ def test_uses_given_parent
38
+ Object.const_set(:ApplicationSerializer, Class.new)
39
+ run_generator ["Account", "--parent=MySerializer"]
40
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
41
+ ensure
42
+ Object.send :remove_const, :ApplicationSerializer
43
+ end
44
+
45
+ def test_generates_attributes_and_associations
46
+ run_generator
47
+ assert_file "app/serializers/account_serializer.rb" do |serializer|
48
+ assert_match(/^ attributes :id, :name, :description$/, serializer)
49
+ assert_match(/^ attribute :business$/, serializer)
50
+ end
51
+ end
52
+
53
+ def test_with_no_attributes_does_not_add_extra_space
54
+ run_generator ["account"]
55
+ assert_file "app/serializers/account_serializer.rb" do |content|
56
+ assert_no_match /\n\nend/, content
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,78 @@
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
+ adapter = load_adapter(root: "blog", meta: {total: 10})
16
+ expected = {
17
+ "blog" => {
18
+ id: 1,
19
+ title: "AMS Hints"
20
+ },
21
+ "meta" => {
22
+ total: 10
23
+ }
24
+ }
25
+ assert_equal expected, adapter.as_json
26
+ end
27
+
28
+ def test_meta_is_not_included_when_root_is_missing
29
+ adapter = load_adapter(meta: {total: 10})
30
+ expected = {
31
+ id: 1,
32
+ title: "AMS Hints"
33
+ }
34
+ assert_equal expected, adapter.as_json
35
+ end
36
+
37
+ def test_meta_key_is_used
38
+ adapter = load_adapter(root: "blog", meta: {total: 10}, meta_key: "haha_meta")
39
+ expected = {
40
+ "blog" => {
41
+ id: 1,
42
+ title: "AMS Hints"
43
+ },
44
+ "haha_meta" => {
45
+ total: 10
46
+ }
47
+ }
48
+ assert_equal expected, adapter.as_json
49
+ end
50
+
51
+ def test_meta_is_not_used_on_arrays
52
+ serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta")
53
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
54
+ expected = [{
55
+ id: 1,
56
+ name: "AMS Hints",
57
+ writer: {
58
+ id: 2,
59
+ name: "Steve"
60
+ },
61
+ articles: [{
62
+ id: 3,
63
+ title: "AMS",
64
+ body: nil
65
+ }]
66
+ }]
67
+ assert_equal expected, adapter.as_json
68
+ end
69
+
70
+ private
71
+
72
+ def load_adapter(options)
73
+ serializer = AlternateBlogSerializer.new(@blog, options)
74
+ ActiveModel::Serializer::Adapter::Json.new(serializer)
75
+ end
76
+ end
77
+ end
78
+ 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,65 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class SerializerForTest < Minitest::Test
6
+ class ArraySerializerTest < Minitest::Test
7
+ def setup
8
+ @array = [1, 2, 3]
9
+ @previous_array_serializer = ActiveModel::Serializer.config.array_serializer
10
+ end
11
+
12
+ def teardown
13
+ ActiveModel::Serializer.config.array_serializer = @previous_array_serializer
14
+ end
15
+
16
+ def test_serializer_for_array
17
+ serializer = ActiveModel::Serializer.serializer_for(@array)
18
+ assert_equal ActiveModel::Serializer.config.array_serializer, serializer
19
+ end
20
+
21
+ def test_overwritten_serializer_for_array
22
+ new_array_serializer = Class.new
23
+ ActiveModel::Serializer.config.array_serializer = new_array_serializer
24
+ serializer = ActiveModel::Serializer.serializer_for(@array)
25
+ assert_equal new_array_serializer, serializer
26
+ end
27
+ end
28
+
29
+ class SerializerTest < Minitest::Test
30
+ class MyProfile < Profile
31
+ end
32
+ class CustomProfile
33
+ def serializer_class; ProfileSerializer; end
34
+ end
35
+
36
+ def setup
37
+ @profile = Profile.new
38
+ @my_profile = MyProfile.new
39
+ @custom_profile = CustomProfile.new
40
+ @model = ::Model.new
41
+ end
42
+
43
+ def test_serializer_for_existing_serializer
44
+ serializer = ActiveModel::Serializer.serializer_for(@profile)
45
+ assert_equal ProfileSerializer, serializer
46
+ end
47
+
48
+ def test_serializer_for_not_existing_serializer
49
+ serializer = ActiveModel::Serializer.serializer_for(@model)
50
+ assert_equal nil, serializer
51
+ end
52
+
53
+ def test_serializer_inherited_serializer
54
+ serializer = ActiveModel::Serializer.serializer_for(@my_profile)
55
+ assert_equal ProfileSerializer, serializer
56
+ end
57
+
58
+ def test_serializer_custom_serializer
59
+ serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
60
+ assert_equal ProfileSerializer, serializer
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class UrlsTest < Minitest::Test
6
+
7
+ def setup
8
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ @post = Post.new({ title: 'New Post', body: 'Body' })
10
+ @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
11
+ @post.comments = [@comment]
12
+
13
+ @profile_serializer = ProfileSerializer.new(@profile)
14
+ @post_serializer = PostSerializer.new(@post)
15
+ end
16
+
17
+ def test_urls_definition
18
+ assert_equal([:posts, :comments], @profile_serializer.class._urls)
19
+ end
20
+
21
+ def test_url_definition
22
+ assert_equal([:comments], @post_serializer.class._urls)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'rails'
4
+ require 'action_controller'
5
+ require 'action_controller/test_case'
6
+ require 'action_controller/railtie'
7
+ require 'active_support/json'
8
+ require 'minitest/autorun'
9
+ # Ensure backward compatibility with Minitest 4
10
+ Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
11
+
12
+ class Foo < Rails::Application
13
+ if Rails.version.to_s.start_with? '4'
14
+ config.action_controller.perform_caching = true
15
+ config.active_support.test_order = :random
16
+ ActionController::Base.cache_store = :memory_store
17
+ end
18
+ end
19
+
20
+ require "active_model_serializers"
21
+
22
+ require 'fixtures/poro'
23
+
24
+ module TestHelper
25
+ Routes = ActionDispatch::Routing::RouteSet.new
26
+ Routes.draw do
27
+ get ':controller(/:action(/:id))'
28
+ get ':controller(/:action)'
29
+ end
30
+
31
+ ActionController::Base.send :include, Routes.url_helpers
32
+ end
33
+
34
+ ActionController::TestCase.class_eval do
35
+ def setup
36
+ @routes = TestHelper::Routes
37
+ end
38
+ end