active_model_serializers 0.8.3 → 0.10.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +82 -0
  4. data/.rubocop_todo.yml +315 -0
  5. data/.simplecov +99 -0
  6. data/.travis.yml +26 -20
  7. data/CHANGELOG.md +14 -67
  8. data/CONTRIBUTING.md +31 -0
  9. data/Gemfile +45 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +186 -488
  12. data/Rakefile +33 -12
  13. data/active_model_serializers.gemspec +49 -23
  14. data/appveyor.yml +25 -0
  15. data/docs/README.md +29 -0
  16. data/docs/general/adapters.md +110 -0
  17. data/docs/general/configuration_options.md +11 -0
  18. data/docs/general/getting_started.md +73 -0
  19. data/docs/howto/add_pagination_links.md +112 -0
  20. data/docs/howto/add_root_key.md +51 -0
  21. data/docs/howto/outside_controller_use.md +42 -0
  22. data/lib/action_controller/serialization.rb +31 -31
  23. data/lib/active_model/serializable_resource.rb +70 -0
  24. data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
  25. data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
  26. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
  27. data/lib/active_model/serializer/adapter/json.rb +47 -0
  28. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
  29. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
  30. data/lib/active_model/serializer/adapter/json_api.rb +158 -0
  31. data/lib/active_model/serializer/adapter/null.rb +5 -0
  32. data/lib/active_model/serializer/adapter.rb +159 -0
  33. data/lib/active_model/serializer/array_serializer.rb +40 -0
  34. data/lib/active_model/serializer/association.rb +20 -0
  35. data/lib/active_model/serializer/associations.rb +83 -219
  36. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  37. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  38. data/lib/active_model/serializer/configuration.rb +14 -0
  39. data/lib/active_model/serializer/fieldset.rb +40 -0
  40. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  41. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  42. data/lib/active_model/serializer/lint.rb +129 -0
  43. data/lib/active_model/serializer/railtie.rb +15 -0
  44. data/lib/active_model/serializer/reflection.rb +74 -0
  45. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  46. data/lib/active_model/serializer/utils.rb +35 -0
  47. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +121 -465
  49. data/lib/active_model_serializers.rb +26 -88
  50. data/lib/generators/serializer/USAGE +0 -3
  51. data/lib/generators/serializer/resource_override.rb +12 -0
  52. data/lib/generators/serializer/serializer_generator.rb +8 -13
  53. data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
  54. data/lib/tasks/rubocop.rake +0 -0
  55. data/test/action_controller/adapter_selector_test.rb +53 -0
  56. data/test/action_controller/explicit_serializer_test.rb +134 -0
  57. data/test/action_controller/json_api/linked_test.rb +179 -0
  58. data/test/action_controller/json_api/pagination_test.rb +116 -0
  59. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
  60. data/test/action_controller/serialization_test.rb +420 -0
  61. data/test/active_record_test.rb +9 -0
  62. data/test/adapter/fragment_cache_test.rb +37 -0
  63. data/test/adapter/json/belongs_to_test.rb +47 -0
  64. data/test/adapter/json/collection_test.rb +82 -0
  65. data/test/adapter/json/has_many_test.rb +47 -0
  66. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  67. data/test/adapter/json_api/collection_test.rb +95 -0
  68. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  69. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  70. data/test/adapter/json_api/has_many_test.rb +145 -0
  71. data/test/adapter/json_api/has_one_test.rb +81 -0
  72. data/test/adapter/json_api/json_api_test.rb +37 -0
  73. data/test/adapter/json_api/linked_test.rb +283 -0
  74. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  75. data/test/adapter/json_api/resource_type_config_test.rb +59 -0
  76. data/test/adapter/json_test.rb +47 -0
  77. data/test/adapter/null_test.rb +25 -0
  78. data/test/adapter_test.rb +42 -0
  79. data/test/array_serializer_test.rb +99 -73
  80. data/test/capture_warnings.rb +65 -0
  81. data/test/fixtures/active_record.rb +56 -0
  82. data/test/fixtures/poro.rb +261 -0
  83. data/test/generators/scaffold_controller_generator_test.rb +23 -0
  84. data/test/generators/serializer_generator_test.rb +56 -0
  85. data/test/lint_test.rb +37 -0
  86. data/test/logger_test.rb +18 -0
  87. data/test/poro_test.rb +9 -0
  88. data/test/serializable_resource_test.rb +27 -0
  89. data/test/serializers/adapter_for_test.rb +170 -0
  90. data/test/serializers/association_macros_test.rb +36 -0
  91. data/test/serializers/associations_test.rb +150 -0
  92. data/test/serializers/attribute_test.rb +62 -0
  93. data/test/serializers/attributes_test.rb +57 -0
  94. data/test/serializers/cache_test.rb +165 -0
  95. data/test/serializers/configuration_test.rb +15 -0
  96. data/test/serializers/fieldset_test.rb +25 -0
  97. data/test/serializers/meta_test.rb +121 -0
  98. data/test/serializers/options_test.rb +21 -0
  99. data/test/serializers/root_test.rb +21 -0
  100. data/test/serializers/serializer_for_test.rb +65 -0
  101. data/test/support/rails_app.rb +21 -0
  102. data/test/support/serialization_testing.rb +13 -0
  103. data/test/support/simplecov.rb +6 -0
  104. data/test/support/stream_capture.rb +50 -0
  105. data/test/support/test_case.rb +5 -0
  106. data/test/test_helper.rb +48 -24
  107. data/test/utils/include_args_to_hash_test.rb +79 -0
  108. metadata +219 -47
  109. data/DESIGN.textile +0 -586
  110. data/Gemfile.edge +0 -9
  111. data/bench/perf.rb +0 -43
  112. data/cruft.md +0 -19
  113. data/lib/active_model/array_serializer.rb +0 -104
  114. data/lib/active_record/serializer_override.rb +0 -16
  115. data/lib/generators/resource_override.rb +0 -13
  116. data/lib/generators/serializer/templates/serializer.rb +0 -19
  117. data/test/association_test.rb +0 -592
  118. data/test/caching_test.rb +0 -96
  119. data/test/generators_test.rb +0 -85
  120. data/test/no_serialization_scope_test.rb +0 -34
  121. data/test/serialization_test.rb +0 -392
  122. data/test/serializer_support_test.rb +0 -51
  123. data/test/serializer_test.rb +0 -1465
  124. data/test/test_fakes.rb +0 -217
@@ -1,95 +1,33 @@
1
- require "active_support"
2
- require "active_support/core_ext/string/inflections"
3
- require "active_support/notifications"
4
- require "active_model"
5
- require "active_model/array_serializer"
6
- require "active_model/serializer"
7
- require "active_model/serializer/associations"
8
- require "set"
9
-
10
- if defined?(Rails)
11
- module ActiveModel
12
- class Railtie < Rails::Railtie
13
- generators do |app|
14
- app ||= Rails.application # Rails 3.0.x does not yield `app`
15
-
16
- Rails::Generators.configure!(app.config.generators)
17
- Rails::Generators.hidden_namespaces.uniq!
18
- require_relative "generators/resource_override"
19
- end
20
-
21
- initializer "include_routes.active_model_serializer" do |app|
22
- ActiveSupport.on_load(:active_model_serializers) do
23
- include app.routes.url_helpers
24
- end
25
- end
26
-
27
- initializer "caching.active_model_serializer" do |app|
28
- ActiveModel::Serializer.perform_caching = app.config.action_controller.perform_caching
29
- ActiveModel::ArraySerializer.perform_caching = app.config.action_controller.perform_caching
30
-
31
- ActiveModel::Serializer.cache = Rails.cache
32
- ActiveModel::ArraySerializer.cache = Rails.cache
33
- end
34
- end
35
- end
36
- end
37
-
38
- module ActiveModel::SerializerSupport
39
- extend ActiveSupport::Concern
40
-
41
- module ClassMethods #:nodoc:
42
- if "".respond_to?(:safe_constantize)
43
- def active_model_serializer
44
- "#{self.name}Serializer".safe_constantize
45
- end
46
- else
47
- def active_model_serializer
48
- begin
49
- "#{self.name}Serializer".constantize
50
- rescue NameError => e
51
- raise unless e.message =~ /uninitialized constant/
52
- end
53
- end
54
- end
55
- end
56
-
57
- # Returns a model serializer for this object considering its namespace.
58
- def active_model_serializer
59
- self.class.active_model_serializer
60
- end
61
-
62
- alias :read_attribute_for_serialization :send
63
- end
64
-
65
- module ActiveModel::ArraySerializerSupport
66
- def active_model_serializer
67
- ActiveModel::ArraySerializer
68
- end
69
- end
70
-
71
- Array.send(:include, ActiveModel::ArraySerializerSupport)
72
- Set.send(:include, ActiveModel::ArraySerializerSupport)
73
-
74
- {
75
- :active_record => 'ActiveRecord::Relation',
76
- :mongoid => 'Mongoid::Criteria'
77
- }.each do |orm, rel_class|
78
- ActiveSupport.on_load(orm) do
79
- include ActiveModel::SerializerSupport
80
- rel_class.constantize.send(:include, ActiveModel::ArraySerializerSupport)
1
+ require 'logger'
2
+ require 'active_model'
3
+ require 'active_support/railtie'
4
+ require 'action_controller'
5
+ require 'action_controller/railtie'
6
+ module ActiveModelSerializers
7
+ mattr_accessor :logger
8
+ self.logger = Rails.logger || Logger.new(IO::NULL)
9
+
10
+ module_function
11
+
12
+ def silence_warnings
13
+ verbose = $VERBOSE
14
+ $VERBOSE = nil
15
+ yield
16
+ ensure
17
+ $VERBOSE = verbose
81
18
  end
82
19
  end
83
20
 
84
- begin
85
- require 'action_controller'
86
- require 'action_controller/serialization'
21
+ require 'active_model/serializer'
22
+ require 'active_model/serializable_resource'
23
+ require 'active_model/serializer/version'
87
24
 
88
- ActiveSupport.on_load(:action_controller) do
89
- include ::ActionController::Serialization
25
+ require 'action_controller/serialization'
26
+ ActiveSupport.on_load(:action_controller) do
27
+ include ::ActionController::Serialization
28
+ ActionDispatch::Reloader.to_prepare do
29
+ ActiveModel::Serializer.serializers_cache.clear
90
30
  end
91
- rescue LoadError => ex
92
- # rails on installed, continuing
93
31
  end
94
32
 
95
- ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
33
+ require 'active_model/serializer/railtie'
@@ -4,6 +4,3 @@ Description:
4
4
  Example:
5
5
  `rails generate serializer Account name created_at`
6
6
 
7
- For TestUnit it creates:
8
- Serializer: app/serializers/account_serializer.rb
9
- TestUnit: test/unit/account_serializer_test.rb
@@ -0,0 +1,12 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/rails/resource/resource_generator'
3
+
4
+ module Rails
5
+ module Generators
6
+ class ResourceGenerator
7
+ def add_serializer
8
+ invoke 'serializer'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,21 +1,18 @@
1
1
  module Rails
2
2
  module Generators
3
3
  class SerializerGenerator < NamedBase
4
- source_root File.expand_path("../templates", __FILE__)
5
- check_class_collision :suffix => "Serializer"
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ check_class_collision :suffix => 'Serializer'
6
6
 
7
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7
+ argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
8
8
 
9
- class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
9
+ class_option :parent, :type => :string, :desc => 'The parent class for the generated serializer'
10
10
 
11
11
  def create_serializer_file
12
- template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
12
+ template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
13
13
  end
14
14
 
15
15
  private
16
- def generate_id_method
17
- RUBY_VERSION =~ /1\.8/
18
- end
19
16
 
20
17
  def attributes_names
21
18
  [:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
@@ -28,15 +25,13 @@ module Rails
28
25
  def parent_class_name
29
26
  if options[:parent]
30
27
  options[:parent]
31
- # Only works on 3.2
32
- # elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
33
- # "ApplicationSerializer"
34
28
  elsif defined?(::ApplicationSerializer)
35
- "ApplicationSerializer"
29
+ 'ApplicationSerializer'
36
30
  else
37
- "ActiveModel::Serializer"
31
+ 'ActiveModel::Serializer'
38
32
  end
39
33
  end
40
34
  end
41
35
  end
42
36
  end
37
+
@@ -0,0 +1,8 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Serializer < <%= parent_class_name %>
3
+ attributes <%= attributes_names.map(&:inspect).join(", ") %>
4
+ <% association_names.each do |attribute| -%>
5
+ has_one :<%= attribute %>
6
+ <% end -%>
7
+ end
8
+ <% end -%>
File without changes
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class AdapterSelectorTest < ActionController::TestCase
6
+ class AdapterSelectorTestController < ActionController::Base
7
+ def render_using_default_adapter
8
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ render json: @profile
10
+ end
11
+
12
+ def render_using_adapter_override
13
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
14
+ render json: @profile, adapter: :json_api
15
+ end
16
+
17
+ def render_skipping_adapter
18
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
19
+ render json: @profile, adapter: false
20
+ end
21
+ end
22
+
23
+ tests AdapterSelectorTestController
24
+
25
+ def test_render_using_default_adapter
26
+ get :render_using_default_adapter
27
+ assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
28
+ end
29
+
30
+ def test_render_using_adapter_override
31
+ get :render_using_adapter_override
32
+
33
+ expected = {
34
+ data: {
35
+ id: assigns(:profile).id.to_s,
36
+ type: 'profiles',
37
+ attributes: {
38
+ name: 'Name 1',
39
+ description: 'Description 1',
40
+ }
41
+ }
42
+ }
43
+
44
+ assert_equal expected.to_json, response.body
45
+ end
46
+
47
+ def test_render_skipping_adapter
48
+ get :render_skipping_adapter
49
+ assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class ExplicitSerializerTest < ActionController::TestCase
6
+ class ExplicitSerializerTestController < ActionController::Base
7
+ def render_using_explicit_serializer
8
+ @profile = Profile.new(name: 'Name 1',
9
+ description: 'Description 1',
10
+ comments: 'Comments 1')
11
+ render json: @profile, serializer: ProfilePreviewSerializer
12
+ end
13
+
14
+ def render_array_using_explicit_serializer
15
+ array = [
16
+ Profile.new(name: 'Name 1',
17
+ description: 'Description 1',
18
+ comments: 'Comments 1'),
19
+ Profile.new(name: 'Name 2',
20
+ description: 'Description 2',
21
+ comments: 'Comments 2')
22
+ ]
23
+ render json: array,
24
+ serializer: PaginatedSerializer,
25
+ each_serializer: ProfilePreviewSerializer
26
+ end
27
+
28
+ def render_array_using_implicit_serializer
29
+ array = [
30
+ Profile.new(name: 'Name 1',
31
+ description: 'Description 1',
32
+ comments: 'Comments 1'),
33
+ Profile.new(name: 'Name 2',
34
+ description: 'Description 2',
35
+ comments: 'Comments 2')
36
+ ]
37
+ render json: array,
38
+ each_serializer: ProfilePreviewSerializer
39
+ end
40
+
41
+ def render_array_using_explicit_serializer_and_custom_serializers
42
+ @post = Post.new(title: 'New Post', body: 'Body')
43
+ @author = Author.new(name: 'Jane Blogger')
44
+ @author.posts = [@post]
45
+ @post.author = @author
46
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
47
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
48
+ @post.comments = [@first_comment, @second_comment]
49
+ @first_comment.post = @post
50
+ @first_comment.author = nil
51
+ @second_comment.post = @post
52
+ @second_comment.author = nil
53
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
54
+ @post.blog = @blog
55
+
56
+ render json: [@post], each_serializer: PostPreviewSerializer
57
+ end
58
+
59
+ def render_using_explicit_each_serializer
60
+ location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
61
+ place = Place.new(id: 1337, name: 'Amazing Place', locations: [location])
62
+
63
+ render json: place, each_serializer: PlaceSerializer
64
+ end
65
+ end
66
+
67
+ tests ExplicitSerializerTestController
68
+
69
+ def test_render_using_explicit_serializer
70
+ get :render_using_explicit_serializer
71
+
72
+ assert_equal 'application/json', @response.content_type
73
+ assert_equal '{"name":"Name 1"}', @response.body
74
+ end
75
+
76
+ def test_render_array_using_explicit_serializer
77
+ get :render_array_using_explicit_serializer
78
+ assert_equal 'application/json', @response.content_type
79
+
80
+ expected = [
81
+ { 'name' => 'Name 1' },
82
+ { 'name' => 'Name 2' }
83
+ ]
84
+
85
+ assert_equal expected.to_json, @response.body
86
+ end
87
+
88
+ def test_render_array_using_implicit_serializer
89
+ get :render_array_using_implicit_serializer
90
+ assert_equal 'application/json', @response.content_type
91
+
92
+ expected = [
93
+ { 'name' => 'Name 1' },
94
+ { 'name' => 'Name 2' }
95
+ ]
96
+ assert_equal expected.to_json, @response.body
97
+ end
98
+
99
+ def test_render_array_using_explicit_serializer_and_custom_serializers
100
+ get :render_array_using_explicit_serializer_and_custom_serializers
101
+
102
+ expected = [
103
+ { 'title' => 'New Post',
104
+ 'body' => 'Body',
105
+ 'id' => assigns(:post).id,
106
+ 'comments' => [{ 'id' => 1 }, { 'id' => 2 }],
107
+ 'author' => { 'id' => assigns(:author).id }
108
+ }
109
+ ]
110
+
111
+ assert_equal expected.to_json, @response.body
112
+ end
113
+
114
+ def test_render_using_explicit_each_serializer
115
+ get :render_using_explicit_each_serializer
116
+
117
+ expected = {
118
+ id: 1337,
119
+ name: 'Amazing Place',
120
+ locations: [
121
+ {
122
+ id: 42,
123
+ lat: '-23.550520',
124
+ lng: '-46.633309',
125
+ place: 'Nowhere' # is a virtual attribute on LocationSerializer
126
+ }
127
+ ]
128
+ }
129
+
130
+ assert_equal expected.to_json, response.body
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,179 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class JsonApi
6
+ class LinkedTest < ActionController::TestCase
7
+ class LinkedTestController < ActionController::Base
8
+ def setup_post
9
+ ActionController::Base.cache_store.clear
10
+ @role1 = Role.new(id: 1, name: 'admin')
11
+ @role2 = Role.new(id: 2, name: 'colab')
12
+ @author = Author.new(id: 1, name: 'Steve K.')
13
+ @author.posts = []
14
+ @author.bio = nil
15
+ @author.roles = [@role1, @role2]
16
+ @role1.author = @author
17
+ @role2.author = @author
18
+ @author2 = Author.new(id: 2, name: 'Anonymous')
19
+ @author2.posts = []
20
+ @author2.bio = nil
21
+ @author2.roles = []
22
+ @post = Post.new(id: 1, title: 'New Post', body: 'Body')
23
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
24
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
25
+ @post.comments = [@first_comment, @second_comment]
26
+ @post.author = @author
27
+ @first_comment.post = @post
28
+ @first_comment.author = @author2
29
+ @second_comment.post = @post
30
+ @second_comment.author = nil
31
+ @post2 = Post.new(id: 2, title: 'Another Post', body: 'Body')
32
+ @post2.author = @author
33
+ @post2.comments = []
34
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
35
+ @post.blog = @blog
36
+ @post2.blog = @blog
37
+ end
38
+
39
+ def render_resource_without_include
40
+ setup_post
41
+ render json: @post, adapter: :json_api
42
+ end
43
+
44
+ def render_resource_with_include
45
+ setup_post
46
+ render json: @post, include: [:author], adapter: :json_api
47
+ end
48
+
49
+ def render_resource_with_nested_include
50
+ setup_post
51
+ render json: @post, include: [comments: [:author]], adapter: :json_api
52
+ end
53
+
54
+ def render_resource_with_nested_has_many_include
55
+ setup_post
56
+ render json: @post, include: 'author.roles', adapter: :json_api
57
+ end
58
+
59
+ def render_resource_with_missing_nested_has_many_include
60
+ setup_post
61
+ @post.author = @author2 # author2 has no roles.
62
+ render json: @post, include: [author: [:roles]], adapter: :json_api
63
+ end
64
+
65
+ def render_collection_with_missing_nested_has_many_include
66
+ setup_post
67
+ @post.author = @author2
68
+ render json: [@post, @post2], include: [author: [:roles]], adapter: :json_api
69
+ end
70
+
71
+ def render_collection_without_include
72
+ setup_post
73
+ render json: [@post], adapter: :json_api
74
+ end
75
+
76
+ def render_collection_with_include
77
+ setup_post
78
+ render json: [@post], include: 'author, comments', adapter: :json_api
79
+ end
80
+ end
81
+
82
+ tests LinkedTestController
83
+
84
+ def test_render_resource_without_include
85
+ get :render_resource_without_include
86
+ response = JSON.parse(@response.body)
87
+ refute response.key? 'included'
88
+ end
89
+
90
+ def test_render_resource_with_include
91
+ get :render_resource_with_include
92
+ response = JSON.parse(@response.body)
93
+ assert response.key? 'included'
94
+ assert_equal 1, response['included'].size
95
+ assert_equal 'Steve K.', response['included'].first['attributes']['name']
96
+ end
97
+
98
+ def test_render_resource_with_nested_has_many_include
99
+ get :render_resource_with_nested_has_many_include
100
+ response = JSON.parse(@response.body)
101
+ expected_linked = [
102
+ {
103
+ 'id' => '1',
104
+ 'type' => 'authors',
105
+ 'attributes' => {
106
+ 'name' => 'Steve K.'
107
+ },
108
+ 'relationships' => {
109
+ 'posts' => { 'data' => [] },
110
+ 'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
111
+ 'bio' => { 'data' => nil }
112
+ }
113
+ }, {
114
+ 'id' => '1',
115
+ 'type' => 'roles',
116
+ 'attributes' => {
117
+ 'name' => 'admin',
118
+ 'description' => nil,
119
+ 'slug' => 'admin-1'
120
+ },
121
+ 'relationships' => {
122
+ 'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
123
+ }
124
+ }, {
125
+ 'id' => '2',
126
+ 'type' => 'roles',
127
+ 'attributes' => {
128
+ 'name' => 'colab',
129
+ 'description' => nil,
130
+ 'slug' => 'colab-2'
131
+ },
132
+ 'relationships' => {
133
+ 'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
134
+ }
135
+ }
136
+ ]
137
+ assert_equal expected_linked, response['included']
138
+ end
139
+
140
+ def test_render_resource_with_nested_include
141
+ get :render_resource_with_nested_include
142
+ response = JSON.parse(@response.body)
143
+ assert response.key? 'included'
144
+ assert_equal 3, response['included'].size
145
+ end
146
+
147
+ def test_render_collection_without_include
148
+ get :render_collection_without_include
149
+ response = JSON.parse(@response.body)
150
+ refute response.key? 'included'
151
+ end
152
+
153
+ def test_render_collection_with_include
154
+ get :render_collection_with_include
155
+ response = JSON.parse(@response.body)
156
+ assert response.key? 'included'
157
+ end
158
+
159
+ def test_render_resource_with_nested_attributes_even_when_missing_associations
160
+ get :render_resource_with_missing_nested_has_many_include
161
+ response = JSON.parse(@response.body)
162
+ assert response.key? 'included'
163
+ refute has_type?(response['included'], 'roles')
164
+ end
165
+
166
+ def test_render_collection_with_missing_nested_has_many_include
167
+ get :render_collection_with_missing_nested_has_many_include
168
+ response = JSON.parse(@response.body)
169
+ assert response.key? 'included'
170
+ assert has_type?(response['included'], 'roles')
171
+ end
172
+
173
+ def has_type?(collection, value)
174
+ collection.detect { |i| i['type'] == value }
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+ require 'will_paginate/array'
3
+ require 'kaminari'
4
+ require 'kaminari/hooks'
5
+ ::Kaminari::Hooks.init
6
+
7
+ module ActionController
8
+ module Serialization
9
+ class JsonApi
10
+ class PaginationTest < ActionController::TestCase
11
+ KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'
12
+ WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'
13
+
14
+ class PaginationTestController < ActionController::Base
15
+ def setup
16
+ @array = [
17
+ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
18
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
19
+ Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
20
+ ]
21
+ end
22
+
23
+ def using_kaminari
24
+ setup
25
+ Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
26
+ end
27
+
28
+ def using_will_paginate
29
+ setup
30
+ @array.paginate(page: params[:page][:number], per_page: params[:page][:size])
31
+ end
32
+
33
+ def render_pagination_using_kaminari
34
+ render json: using_kaminari, adapter: :json_api
35
+ end
36
+
37
+ def render_pagination_using_will_paginate
38
+ render json: using_will_paginate, adapter: :json_api
39
+ end
40
+
41
+ def render_array_without_pagination_links
42
+ setup
43
+ render json: @array, adapter: :json_api
44
+ end
45
+ end
46
+
47
+ tests PaginationTestController
48
+
49
+ def test_render_pagination_links_with_will_paginate
50
+ expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
51
+ 'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
52
+ 'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
53
+ 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
54
+ 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
55
+
56
+ get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
57
+ response = JSON.parse(@response.body)
58
+ assert_equal expected_links, response['links']
59
+ end
60
+
61
+ def test_render_only_last_and_next_pagination_links
62
+ expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
63
+ 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
64
+ 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
65
+ get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
66
+ response = JSON.parse(@response.body)
67
+ assert_equal expected_links, response['links']
68
+ end
69
+
70
+ def test_render_pagination_links_with_kaminari
71
+ expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
72
+ 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
73
+ 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
74
+ 'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
75
+ 'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
76
+ get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
77
+ response = JSON.parse(@response.body)
78
+ assert_equal expected_links, response['links']
79
+ end
80
+
81
+ def test_render_only_prev_and_first_pagination_links
82
+ expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
83
+ 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
84
+ 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
85
+ get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
86
+ response = JSON.parse(@response.body)
87
+ assert_equal expected_links, response['links']
88
+ end
89
+
90
+ def test_render_only_last_and_next_pagination_links_with_additional_params
91
+ expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
92
+ 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
93
+ 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
94
+ get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional'
95
+ response = JSON.parse(@response.body)
96
+ assert_equal expected_links, response['links']
97
+ end
98
+
99
+ def test_render_only_prev_and_first_pagination_links_with_additional_params
100
+ expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
101
+ 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
102
+ 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
103
+ get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional'
104
+ response = JSON.parse(@response.body)
105
+ assert_equal expected_links, response['links']
106
+ end
107
+
108
+ def test_array_without_pagination_links
109
+ get :render_array_without_pagination_links, page: { number: 2, size: 1 }
110
+ response = JSON.parse(@response.body)
111
+ refute response.key? 'links'
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end