active_model_serializers_rails_2.3 0.9.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/CHANGELOG.md +87 -0
  2. data/CONTRIBUTING.md +20 -0
  3. data/DESIGN.textile +586 -0
  4. data/MIT-LICENSE +21 -0
  5. data/README.md +793 -0
  6. data/lib/active_model/array_serializer.rb +60 -0
  7. data/lib/active_model/default_serializer.rb +27 -0
  8. data/lib/active_model/serializable.rb +25 -0
  9. data/lib/active_model/serializer.rb +220 -0
  10. data/lib/active_model/serializer/associations.rb +98 -0
  11. data/lib/active_model/serializer/config.rb +31 -0
  12. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  13. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  14. data/lib/active_model/serializer/railtie.rb +10 -0
  15. data/lib/active_model/serializer/version.rb +5 -0
  16. data/lib/active_model/serializer_support.rb +5 -0
  17. data/lib/active_model_serializers.rb +33 -0
  18. data/test/coverage_setup.rb +15 -0
  19. data/test/fixtures/active_record.rb +92 -0
  20. data/test/fixtures/poro.rb +64 -0
  21. data/test/integration/active_record/active_record_test.rb +77 -0
  22. data/test/test_app.rb +11 -0
  23. data/test/test_helper.rb +13 -0
  24. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  25. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  26. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  27. data/test/unit/active_model/array_serializer/serialization_test.rb +182 -0
  28. data/test/unit/active_model/default_serializer_test.rb +13 -0
  29. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
  30. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  31. data/test/unit/active_model/serializer/attributes_test.rb +41 -0
  32. data/test/unit/active_model/serializer/config_test.rb +86 -0
  33. data/test/unit/active_model/serializer/filter_test.rb +49 -0
  34. data/test/unit/active_model/serializer/has_many_test.rb +174 -0
  35. data/test/unit/active_model/serializer/has_one_test.rb +151 -0
  36. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  37. data/test/unit/active_model/serializer/root_test.rb +117 -0
  38. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  39. metadata +127 -0
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class MetaTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ end
9
+
10
+ def test_meta
11
+ profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta: { total: 10 })
12
+
13
+ assert_equal({
14
+ 'profile' => {
15
+ name: 'Name 1',
16
+ description: 'Description 1'
17
+ },
18
+ meta: {
19
+ total: 10
20
+ }
21
+ }, profile_serializer.as_json)
22
+ end
23
+
24
+ def test_meta_using_meta_key
25
+ profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta_key: :my_meta, my_meta: { total: 10 })
26
+
27
+ assert_equal({
28
+ 'profile' => {
29
+ name: 'Name 1',
30
+ description: 'Description 1'
31
+ },
32
+ my_meta: {
33
+ total: 10
34
+ }
35
+ }, profile_serializer.as_json)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,117 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class RootAsOptionTest < Minitest::Test
6
+ def setup
7
+ @old_root = ProfileSerializer._root
8
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ @serializer = ProfileSerializer.new(@profile, root: :initialize)
10
+ ProfileSerializer._root = true
11
+ end
12
+
13
+ def teardown
14
+ ProfileSerializer._root = @old_root
15
+ end
16
+
17
+ def test_root_is_not_displayed_using_serializable_hash
18
+ assert_equal({
19
+ name: 'Name 1', description: 'Description 1'
20
+ }, @serializer.serializable_hash)
21
+ end
22
+
23
+ def test_root_using_as_json
24
+ assert_equal({
25
+ initialize: {
26
+ name: 'Name 1', description: 'Description 1'
27
+ }
28
+ }, @serializer.as_json)
29
+ end
30
+
31
+ def test_root_from_serializer_name
32
+ @serializer = ProfileSerializer.new(@profile)
33
+
34
+ assert_equal({
35
+ 'profile' => {
36
+ name: 'Name 1', description: 'Description 1'
37
+ }
38
+ }, @serializer.as_json)
39
+ end
40
+
41
+ def test_root_as_argument_takes_precedence
42
+ assert_equal({
43
+ argument: {
44
+ name: 'Name 1', description: 'Description 1'
45
+ }
46
+ }, @serializer.as_json(root: :argument))
47
+ end
48
+
49
+ def test_using_false_root_in_initializer_takes_precedence
50
+ ProfileSerializer._root = 'root'
51
+ @serializer = ProfileSerializer.new(@profile, root: false)
52
+
53
+ assert_equal({
54
+ name: 'Name 1', description: 'Description 1'
55
+ }, @serializer.as_json)
56
+ end
57
+
58
+ def test_root_inheritance
59
+ ProfileSerializer._root = 'profile'
60
+
61
+ inherited_serializer_klass = Class.new(ProfileSerializer)
62
+ inherited_serializer_klass._root = 'inherited_profile'
63
+
64
+ another_inherited_serializer_klass = Class.new(ProfileSerializer)
65
+
66
+ assert_equal('inherited_profile',
67
+ inherited_serializer_klass._root)
68
+ assert_equal('profile',
69
+ another_inherited_serializer_klass._root)
70
+ end
71
+ end
72
+
73
+ class RootInSerializerTest < Minitest::Test
74
+ def setup
75
+ @old_root = ProfileSerializer._root
76
+ ProfileSerializer._root = :in_serializer
77
+ profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
78
+ @serializer = ProfileSerializer.new(profile)
79
+ @rooted_serializer = ProfileSerializer.new(profile, root: :initialize)
80
+ end
81
+
82
+ def teardown
83
+ ProfileSerializer._root = @old_root
84
+ end
85
+
86
+ def test_root_is_not_displayed_using_serializable_hash
87
+ assert_equal({
88
+ name: 'Name 1', description: 'Description 1'
89
+ }, @serializer.serializable_hash)
90
+ end
91
+
92
+ def test_root_using_as_json
93
+ assert_equal({
94
+ in_serializer: {
95
+ name: 'Name 1', description: 'Description 1'
96
+ }
97
+ }, @serializer.as_json)
98
+ end
99
+
100
+ def test_root_in_initializer_takes_precedence
101
+ assert_equal({
102
+ initialize: {
103
+ name: 'Name 1', description: 'Description 1'
104
+ }
105
+ }, @rooted_serializer.as_json)
106
+ end
107
+
108
+ def test_root_as_argument_takes_precedence
109
+ assert_equal({
110
+ argument: {
111
+ name: 'Name 1', description: 'Description 1'
112
+ }
113
+ }, @rooted_serializer.as_json(root: :argument))
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class ScopeTest < Minitest::Test
6
+ def setup
7
+ @serializer = ProfileSerializer.new(nil, scope: current_user)
8
+ end
9
+
10
+ def test_scope
11
+ assert_equal('user', @serializer.scope)
12
+ end
13
+
14
+ private
15
+
16
+ def current_user
17
+ 'user'
18
+ end
19
+ end
20
+
21
+ class NestedScopeTest < Minitest::Test
22
+ def setup
23
+ @association = UserSerializer._associations[:profile]
24
+ @old_association = @association.dup
25
+ @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
26
+ @user_serializer = UserSerializer.new(@user, scope: 'user')
27
+ end
28
+
29
+ def teardown
30
+ UserSerializer._associations[:profile] = @old_association
31
+ end
32
+
33
+ def test_scope_passed_through
34
+ @association.serializer_from_options = Class.new(Serializer) do
35
+ def name
36
+ scope
37
+ end
38
+
39
+ attributes :name
40
+ end
41
+
42
+ assert_equal({
43
+ name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
44
+ }, @user_serializer.serializable_hash)
45
+ end
46
+ end
47
+
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model_serializers_rails_2.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0.alpha1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - José Valim
9
+ - Yehuda Katz
10
+ - Santiago Pastorino
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2014-02-28 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ requirement: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '2.3'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '2.3'
32
+ description: Making it easy to serialize models for client-side use
33
+ email:
34
+ - jose.valim@gmail.com
35
+ - wycats@gmail.com
36
+ - santiago@wyeworks.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - README.md
42
+ - CHANGELOG.md
43
+ - CONTRIBUTING.md
44
+ - DESIGN.textile
45
+ - MIT-LICENSE
46
+ - lib/active_model/array_serializer.rb
47
+ - lib/active_model/default_serializer.rb
48
+ - lib/active_model/serializable.rb
49
+ - lib/active_model/serializer/associations.rb
50
+ - lib/active_model/serializer/config.rb
51
+ - lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb
52
+ - lib/active_model/serializer/generators/serializer/templates/controller.rb
53
+ - lib/active_model/serializer/railtie.rb
54
+ - lib/active_model/serializer/version.rb
55
+ - lib/active_model/serializer.rb
56
+ - lib/active_model/serializer_support.rb
57
+ - lib/active_model_serializers.rb
58
+ - test/coverage_setup.rb
59
+ - test/fixtures/active_record.rb
60
+ - test/fixtures/poro.rb
61
+ - test/integration/active_record/active_record_test.rb
62
+ - test/test_app.rb
63
+ - test/test_helper.rb
64
+ - test/unit/active_model/array_serializer/meta_test.rb
65
+ - test/unit/active_model/array_serializer/root_test.rb
66
+ - test/unit/active_model/array_serializer/scope_test.rb
67
+ - test/unit/active_model/array_serializer/serialization_test.rb
68
+ - test/unit/active_model/default_serializer_test.rb
69
+ - test/unit/active_model/serializer/associations/build_serializer_test.rb
70
+ - test/unit/active_model/serializer/associations_test.rb
71
+ - test/unit/active_model/serializer/attributes_test.rb
72
+ - test/unit/active_model/serializer/config_test.rb
73
+ - test/unit/active_model/serializer/filter_test.rb
74
+ - test/unit/active_model/serializer/has_many_test.rb
75
+ - test/unit/active_model/serializer/has_one_test.rb
76
+ - test/unit/active_model/serializer/meta_test.rb
77
+ - test/unit/active_model/serializer/root_test.rb
78
+ - test/unit/active_model/serializer/scope_test.rb
79
+ homepage: https://github.com/rails-api/active_model_serializers
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: 1.9.3
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>'
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.1
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Bringing consistency and object orientation to model serialization. Works
104
+ great for client-side MVC frameworks!
105
+ test_files:
106
+ - test/coverage_setup.rb
107
+ - test/fixtures/active_record.rb
108
+ - test/fixtures/poro.rb
109
+ - test/integration/active_record/active_record_test.rb
110
+ - test/test_app.rb
111
+ - test/test_helper.rb
112
+ - test/unit/active_model/array_serializer/meta_test.rb
113
+ - test/unit/active_model/array_serializer/root_test.rb
114
+ - test/unit/active_model/array_serializer/scope_test.rb
115
+ - test/unit/active_model/array_serializer/serialization_test.rb
116
+ - test/unit/active_model/default_serializer_test.rb
117
+ - test/unit/active_model/serializer/associations/build_serializer_test.rb
118
+ - test/unit/active_model/serializer/associations_test.rb
119
+ - test/unit/active_model/serializer/attributes_test.rb
120
+ - test/unit/active_model/serializer/config_test.rb
121
+ - test/unit/active_model/serializer/filter_test.rb
122
+ - test/unit/active_model/serializer/has_many_test.rb
123
+ - test/unit/active_model/serializer/has_one_test.rb
124
+ - test/unit/active_model/serializer/meta_test.rb
125
+ - test/unit/active_model/serializer/root_test.rb
126
+ - test/unit/active_model/serializer/scope_test.rb
127
+ has_rdoc: