active_model_serializers 0.9.7 → 0.9.13

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 (68) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +41 -1
  3. data/README.md +1 -1
  4. data/lib/action_controller/serialization.rb +18 -1
  5. data/lib/action_controller/serialization_test_case.rb +3 -0
  6. data/lib/active_model/array_serializer.rb +2 -0
  7. data/lib/active_model/default_serializer.rb +2 -0
  8. data/lib/active_model/serializable/utils.rb +2 -0
  9. data/lib/active_model/serializable.rb +3 -1
  10. data/lib/active_model/serializer/association/has_many.rb +2 -0
  11. data/lib/active_model/serializer/association/has_one.rb +2 -0
  12. data/lib/active_model/serializer/association.rb +3 -3
  13. data/lib/active_model/serializer/config.rb +2 -0
  14. data/lib/active_model/serializer/generators/resource_override.rb +2 -0
  15. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +2 -0
  16. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +2 -0
  17. data/lib/active_model/serializer/railtie.rb +2 -0
  18. data/lib/active_model/serializer/version.rb +3 -1
  19. data/lib/active_model/serializer.rb +36 -13
  20. data/lib/active_model/serializer_support.rb +2 -0
  21. data/lib/active_model_serializers.rb +14 -2
  22. data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
  23. data/test/benchmark/tmp/miniprofiler/{mp_timers_6b9d32ftlk8v5xy2op05 → mp_timers_8083sx03hu72pxz1a4d0} +0 -0
  24. data/test/benchmark/tmp/miniprofiler/{mp_timers_3f0bdfx4nib4hxfb5lmq → mp_timers_fyz2gsml4z0ph9kpoy1c} +0 -0
  25. data/test/benchmark/tmp/miniprofiler/{mp_timers_m78k2w11sd7ba5oh40rw → mp_timers_hjry5rc32imd42oxoi48} +0 -0
  26. data/test/benchmark/tmp/miniprofiler/{mp_timers_7nlok5ykeley83h8bine → mp_timers_m8fpoz2cvt3g9agz0bs3} +0 -0
  27. data/test/benchmark/tmp/miniprofiler/{mp_timers_uc1b3hwqrd4q512gy6c9 → mp_timers_p92m2drnj1i568u3sta0} +0 -0
  28. data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
  29. data/test/benchmark/tmp/miniprofiler/{mp_timers_c3a27vo6uvfijeh105nt → mp_timers_s15t1a6mvxe0z7vjv790} +0 -0
  30. data/test/benchmark/tmp/miniprofiler/{mp_timers_54wkvaidi0avdc8yawyt → mp_timers_x8kal3d17nfds6vp4kcj} +0 -0
  31. data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
  32. data/test/fixtures/poro.rb +35 -3
  33. data/test/integration/action_controller/serialization_test.rb +13 -13
  34. data/test/integration/action_controller/serialization_test_case_test.rb +1 -1
  35. data/test/integration/generators/scaffold_controller_generator_test.rb +20 -20
  36. data/test/test_app.rb +4 -0
  37. data/test/test_helper.rb +7 -0
  38. data/test/unit/active_model/array_serializer/serialization_test.rb +23 -0
  39. data/test/unit/active_model/default_serializer_test.rb +1 -1
  40. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +1 -1
  41. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +1 -1
  42. data/test/unit/active_model/serializer/url_helpers_test.rb +1 -0
  43. metadata +24 -70
  44. data/lib/bulk_cache_fetcher.rb +0 -105
  45. data/test/action_controller/http_cache_test.rb +0 -56
  46. data/test/action_controller/regression_test.rb +0 -35
  47. data/test/benchmark/bm_rails_cache.rb +0 -141
  48. data/test/benchmark/tmp/miniprofiler/mp_timers_16x1i2wkkt8b8zybbfbx +0 -0
  49. data/test/benchmark/tmp/miniprofiler/mp_timers_3aa299ezxdqvog6sfjzr +0 -0
  50. data/test/benchmark/tmp/miniprofiler/mp_timers_b36ymkc1vgu5fy9fvm9k +0 -0
  51. data/test/benchmark/tmp/miniprofiler/mp_timers_eqcbrj14r4c341abi3h6 +0 -0
  52. data/test/benchmark/tmp/miniprofiler/mp_timers_hqodkd691tbr7sbfdiez +0 -0
  53. data/test/benchmark/tmp/miniprofiler/mp_timers_j7y5frtod7cw4qc4cear +0 -0
  54. data/test/benchmark/tmp/miniprofiler/mp_timers_jpxzfgo92l24t7wzt8g1 +0 -0
  55. data/test/benchmark/tmp/miniprofiler/mp_timers_mkvat4ehphrje9n2hu3t +0 -0
  56. data/test/benchmark/tmp/miniprofiler/mp_timers_nd3ymt54l1ga22i2brn8 +0 -0
  57. data/test/benchmark/tmp/miniprofiler/mp_timers_o2dk1yhw6ez9jzxf6p0a +0 -0
  58. data/test/benchmark/tmp/miniprofiler/mp_timers_p76i310zb4y12iqm5o0n +0 -0
  59. data/test/benchmark/tmp/miniprofiler/mp_timers_pi3hvrrkfxnxl300wvah +0 -0
  60. data/test/benchmark/tmp/miniprofiler/mp_timers_q4w5d86flb4u9tsadqvw +0 -0
  61. data/test/benchmark/tmp/miniprofiler/mp_timers_sfa5nundogo63o056n5y +0 -0
  62. data/test/benchmark/tmp/miniprofiler/mp_timers_tkjlmqeauf5gur2u5nob +0 -0
  63. data/test/benchmark/tmp/miniprofiler/mp_timers_tngbj11ilv2i75rqn2d2 +0 -0
  64. data/test/benchmark/tmp/miniprofiler/mp_timers_u4haqgvgybvc1sb5nihw +0 -0
  65. data/test/benchmark/tmp/miniprofiler/mp_timers_uwholuf0iix2vvbo2l3c +0 -0
  66. data/test/benchmark/tmp/miniprofiler/mp_timers_waye238ygft3p4xvppc4 +0 -0
  67. data/test/benchmark/tmp/miniprofiler/mp_timers_ydwo7p117loo2ky40lk3 +0 -0
  68. data/test/benchmark/tmp/miniprofiler/mp_timers_zhgmyfizfa8slppitgua +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2f73f5aff52f42003b604fcc209782d1b074eb9a
4
- data.tar.gz: 0fce31e1d5f8f01da8c0c9d70c30fee124dc87ff
2
+ SHA256:
3
+ metadata.gz: a8649ea9bfdefabb0215cc90a6a47a87d902573ecd0e2c73b6142c9cefe17825
4
+ data.tar.gz: 420e8afdc1ba7cbaaab42d7f8f36c3e10b702587cc7484e270428ded2eeccbdd
5
5
  SHA512:
6
- metadata.gz: 983ed0d1faf8a9e4cb9cf65e4d71fdc5064016e42190a81aa96f73a37e1e342eecead421ebe69c04cca27db142a622eb966e9fc04b79cc152af5b744d6f97812
7
- data.tar.gz: 97accf62bff15dc099483199c8ca58c974fc2ee227f64bdbd10223924cdbaa923d4fb59c5ea5a8973fb5612c50e8ef9abf50f300cc8b6f64b80dd9b3a181829f
6
+ metadata.gz: 237f372c6dd5e73cb7d5f944d90918a23e6acf0f7aa137f0e2972a275445a549a6cf4ffd9e410386c611e89e1dc0137d5451c6d61702d6c6381e43a41e3434ac
7
+ data.tar.gz: 5d009500e9e616e7d1a7f83adaf7874022f1f039594983e923ef82d4b8f6b8f6268259404b097545a27512b149ec22c8e113a8c0bb51effbef3c0fdd728d7973
data/CHANGELOG.md CHANGED
@@ -1,6 +1,46 @@
1
1
  ## 0.09.x
2
2
 
3
- ### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.7...0-9-stable)
3
+ ### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.13...0-9-stable)
4
+
5
+ ### [v0.9.13 (2024-09-17)](https://github.com/rails-api/active_model_serializers/compare/v0.9.12...v0.9.13)
6
+
7
+ - Perf
8
+ - [#2471](https://github.com/rails-api/active_model_serializers/pull/2471) Generate better attribute accessors, that also don't trigger warnings when redefined. (@byroot)
9
+
10
+ ### [v0.9.12 (2024-04-11)](https://github.com/rails-api/active_model_serializers/compare/v0.9.11...v0.9.12)
11
+
12
+ - Fix
13
+ - [#2468](https://github.com/rails-api/active_model_serializers/pull/2468) Fix bug introduced in v0.9.9. Revert "Allow serializer_for to accept String instead of just class objects". (@byroot)
14
+ - Perf
15
+ - [#2466](https://github.com/rails-api/active_model_serializers/pull/2466) Prefer `defined?` to `Object.constants.include?` (@byroot)
16
+ - [#2467](https://github.com/rails-api/active_model_serializers/pull/2467) Lazily compute possible serializer class names (@byroot)
17
+ - Chore
18
+ - [#2469](https://github.com/rails-api/active_model_serializers/pull/2469) Fix various warnings in the test suite (@byroot)
19
+
20
+ ### [v0.9.11 (2024-04-09)](https://github.com/rails-api/active_model_serializers/compare/v0.9.10...v0.9.11)
21
+
22
+ v0.9.10 was built with an extra file included and so was yanked.
23
+ v0.9.11 is the same as v0.9.10 but doesn't have the extra file.
24
+
25
+ ### [v0.9.10 (2024-04-09)](https://github.com/rails-api/active_model_serializers/compare/v0.9.9...v0.9.10)
26
+
27
+ - Fix
28
+ - [#2464](https://github.com/rails-api/active_model_serializers/pull/2464) Do not load ActionController::TestCase in production (@byroot)
29
+ - Perf
30
+ - [#2465](https://github.com/rails-api/active_model_serializers/pull/2465) Make compatible with enable-frozen-string-literal (@byroot)
31
+ - [#2463](https://github.com/rails-api/active_model_serializers/pull/2463) Shape Friendly ActionController:Serialization (@byroot)
32
+ - Chore
33
+ - [#2462](https://github.com/rails-api/active_model_serializers/pull/2462]) Rails 7.1, Ruby 3.3 compat (@byroot)
34
+ - Test
35
+ - [#2447](https://github.com/rails-api/active_model_serializers/pull/2447) Configure CI on GitHub actoins (@Physium)
36
+
37
+ ### [v0.9.9 (2023-05-18)](https://github.com/rails-api/active_model_serializers/compare/v0.9.8...v0.9.8)
38
+
39
+ - [#2446](https://github.com/rails-api/active_model_serializers/pull/2446) Fix Ruby > 2.5 constant lookup. (@Physium)
40
+
41
+ ### [v0.9.8 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.9.7...v0.9.8)
42
+
43
+ - [#2373](https://github.com/rails-api/active_model_serializers/pull/2373) Fix Rails 6.0 deprecation warnings. (@supremebeing7)
4
44
 
5
45
  ### [v0.9.7 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.9.6...v0.9.7)
6
46
 
data/README.md CHANGED
@@ -85,7 +85,7 @@ it exists, use it to serialize the `Post`.
85
85
 
86
86
  This also works with `respond_with`, which uses `to_json` under the hood. Also
87
87
  note that any options passed to `render :json` will be passed to your
88
- serializer and available as `@options` inside.
88
+ serializer and available as `@serialization_options` inside.
89
89
 
90
90
  To specify a custom serializer for an object, you can specify the
91
91
  serializer when you render the object:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/class/attribute'
2
4
 
3
5
  module ActionController
@@ -45,6 +47,12 @@ module ActionController
45
47
  end
46
48
  end
47
49
 
50
+ def initialize(*)
51
+ super
52
+ @namespace_for_serializer = nil
53
+ end
54
+ ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)
55
+
48
56
  [:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
49
57
  define_method renderer_method do |resource, options|
50
58
  serializer = build_json_serializer(resource, options)
@@ -60,7 +68,16 @@ module ActionController
60
68
  private
61
69
 
62
70
  def namespace_for_serializer
63
- @namespace_for_serializer ||= self.class.parent unless self.class.parent == Object
71
+ @namespace_for_serializer ||= namespace_for_class(self.class)
72
+ @namespace_for_serializer unless @namespace_for_serializer == Object
73
+ end
74
+
75
+ def namespace_for_class(klass)
76
+ if Module.method_defined?(:module_parent)
77
+ klass.module_parent
78
+ else
79
+ klass.parent
80
+ end
64
81
  end
65
82
 
66
83
  def default_serializer(resource)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActionController
2
4
  module SerializationAssertions
3
5
  extend ActiveSupport::Concern
@@ -24,6 +26,7 @@ module ActionController
24
26
  @serializers = Hash.new(0)
25
27
  super
26
28
  end
29
+ ruby2_keywords :process if respond_to?(:ruby2_keywords, true)
27
30
 
28
31
  # Asserts that the request was rendered with the appropriate serializers.
29
32
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/default_serializer'
2
4
  require 'active_model/serializable'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializable'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  module Serializable
3
5
  module Utils
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/serializable/utils'
2
4
 
3
5
  module ActiveModel
4
6
  module Serializable
5
- INSTRUMENTATION_KEY = '!serialize.active_model_serializers'.freeze
7
+ INSTRUMENTATION_KEY = '!serialize.active_model_serializers'
6
8
 
7
9
  def self.included(base)
8
10
  base.extend Utils
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Association
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Association
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/default_serializer'
2
4
  require 'active_model/serializer/association/has_one'
3
5
  require 'active_model/serializer/association/has_many'
@@ -7,9 +9,7 @@ module ActiveModel
7
9
  class Association
8
10
  def initialize(name, options={})
9
11
  if options.has_key?(:include)
10
- ActiveSupport::Deprecation.warn <<-WARN
11
- ** Notice: include was renamed to embed_in_root. **
12
- WARN
12
+ ActiveSupport::Deprecation.warn("** Notice: include was renamed to embed_in_root. **")
13
13
  end
14
14
 
15
15
  @name = name.to_s
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class Config
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/generators'
2
4
  require 'rails/generators/rails/resource/resource_generator'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/generators'
2
4
  require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rails
2
4
  module Generators
3
5
  class SerializerGenerator < NamedBase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Railtie < Rails::Railtie
3
5
  initializer 'generators' do |app|
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
- VERSION = '0.9.7'
5
+ VERSION = '0.9.13'
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/array_serializer'
2
4
  require 'active_model/serializable'
3
5
  require 'active_model/serializer/association'
@@ -60,16 +62,19 @@ end
60
62
  if resource.respond_to?(:serializer_class)
61
63
  resource.serializer_class
62
64
  elsif resource.respond_to?(:to_ary)
63
- if Object.constants.include?(:ArraySerializer)
65
+ if defined?(::ArraySerializer)
64
66
  ::ArraySerializer
65
67
  else
66
68
  ArraySerializer
67
69
  end
68
70
  else
69
- klass_name = build_serializer_class(resource, options)
70
- Serializer.serializers_cache.fetch_or_store(klass_name) do
71
- _const_get(klass_name)
71
+ each_possible_serializer(resource, options) do |klass_name|
72
+ serializer = Serializer.serializers_cache.fetch_or_store(klass_name) do
73
+ _const_get(klass_name)
74
+ end
75
+ return serializer unless serializer.nil?
72
76
  end
77
+ nil
73
78
  end
74
79
  end
75
80
 
@@ -85,15 +90,27 @@ end
85
90
  end
86
91
 
87
92
  def attributes(*attrs)
93
+ # Use `class_eval` rather than `define_method` for faster access
94
+ # and avoid retaining objects in the closure.
95
+ # Batch all methods in a single `class_eval` for efficiceny,
96
+ # and define all methods on the same line so the eventual backtrace
97
+ # properly maps to the `attributes` call.
98
+ source = ["# frozen_string_literal: true\n"]
88
99
  attrs.each do |attr|
89
100
  striped_attr = strip_attribute attr
90
101
 
91
102
  @_attributes << striped_attr
92
103
 
93
- define_method striped_attr do
94
- object.read_attribute_for_serialization attr
95
- end unless method_defined?(attr)
104
+ unless method_defined?(attr)
105
+ source <<
106
+ "def #{striped_attr}" <<
107
+ "object.read_attribute_for_serialization(#{attr.inspect})" <<
108
+ "end" <<
109
+ "alias_method :#{striped_attr}, :#{striped_attr}" # suppress method redefinition warning
110
+ end
96
111
  end
112
+ caller = caller_locations(1, 1).first
113
+ class_eval(source.join(";"), caller.path, caller.lineno - 1)
97
114
  end
98
115
 
99
116
  def has_one(*attrs)
@@ -118,12 +135,17 @@ end
118
135
  attr
119
136
  end
120
137
 
138
+ def each_possible_serializer(resource, options)
139
+ yield build_serializer_class(resource, options)
140
+ yield build_serializer_class(resource, {})
141
+ yield build_serializer_class(resource.class.name.demodulize, {})
142
+ end
143
+
121
144
  def build_serializer_class(resource, options)
122
- "".tap do |klass_name|
123
- klass_name << "#{options[:namespace]}::" if options[:namespace]
124
- klass_name << options[:prefix].to_s.classify if options[:prefix]
125
- klass_name << "#{resource.class.name}Serializer"
126
- end
145
+ klass_name = +""
146
+ klass_name << "#{options[:namespace]}::" if options[:namespace]
147
+ klass_name << options[:prefix].to_s.classify if options[:prefix]
148
+ klass_name << "#{resource.class.name}Serializer"
127
149
  end
128
150
 
129
151
  def associate(klass, *attrs)
@@ -153,7 +175,8 @@ end
153
175
  @context = options[:context]
154
176
  @namespace = options[:namespace]
155
177
  end
156
- attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
178
+ attr_accessor :object, :scope, :root, :meta_key, :meta, :context, :polymorphic
179
+ attr_writer :key_format
157
180
 
158
181
  def json_key
159
182
  key = if root == true || root.nil?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  module SerializerSupport
3
5
  alias read_attribute_for_serialization send
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model'
2
4
  require 'active_model/serializer'
3
5
  require 'active_model/serializer_support'
@@ -7,12 +9,22 @@ require 'active_model/serializer/railtie' if defined?(Rails)
7
9
  begin
8
10
  require 'action_controller'
9
11
  require 'action_controller/serialization'
10
- require 'action_controller/serialization_test_case'
11
12
 
12
13
  ActiveSupport.on_load(:action_controller) do
13
14
  if ::ActionController::Serialization.enabled
14
15
  ActionController::Base.send(:include, ::ActionController::Serialization)
15
- ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
16
+
17
+ # action_controller_test_case load hook was added in Rails 5.1
18
+ # https://github.com/rails/rails/commit/0510208dd1ff23baa619884c0abcae4d141fae53
19
+ if ActiveSupport::VERSION::STRING < '5.1'
20
+ require 'action_controller/serialization_test_case'
21
+ ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
22
+ else
23
+ ActiveSupport.on_load(:action_controller_test_case) do
24
+ require 'action_controller/serialization_test_case'
25
+ ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
26
+ end
27
+ end
16
28
  end
17
29
  end
18
30
  rescue LoadError
@@ -18,6 +18,19 @@ end
18
18
  ###
19
19
  ## Models
20
20
  ###
21
+
22
+ module TestNamespace2
23
+ class Test < Model
24
+ attr_writer :sub_test
25
+
26
+ def sub_test
27
+ @sub_test ||= TestNamespace2::SubTest.new(name: 'N1', description: 'D1')
28
+ end
29
+ end
30
+
31
+ class SubTest < Model; end
32
+ end
33
+
21
34
  class User < Model
22
35
  def profile
23
36
  @profile ||= Profile.new(name: 'N1', description: 'D1')
@@ -101,6 +114,24 @@ end
101
114
  ###
102
115
  ## Serializers
103
116
  ###
117
+
118
+ module TestNamespace2
119
+ class TestSerializer < ActiveModel::Serializer
120
+ attributes :name, :email
121
+
122
+ has_one :sub_test
123
+ end
124
+
125
+ class SubTestSerializer < ActiveModel::Serializer
126
+ def description
127
+ description = object.read_attribute_for_serialization(:description)
128
+ scope ? "#{description} - #{scope}" : description
129
+ end
130
+
131
+ attributes :name, :description
132
+ end
133
+ end
134
+
104
135
  class UserSerializer < ActiveModel::Serializer
105
136
  attributes :name, :email
106
137
 
@@ -113,14 +144,14 @@ end
113
144
 
114
145
  class SelfReferencingUserParentSerializer < ActiveModel::Serializer
115
146
  attributes :name
116
- has_one :type, serializer: TypeSerializer, embed: :ids, include: true
147
+ has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true
117
148
  end
118
149
 
119
150
  class SelfReferencingUserSerializer < ActiveModel::Serializer
120
151
  attributes :name
121
152
 
122
- has_one :type, serializer: TypeSerializer, embed: :ids, include: true
123
- has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, include: true
153
+ has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true
154
+ has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, embed_in_root: true
124
155
  end
125
156
 
126
157
  class UserInfoSerializer < ActiveModel::Serializer
@@ -145,6 +176,7 @@ end
145
176
  class PostSerializer < ActiveModel::Serializer
146
177
  attributes :title, :body
147
178
 
179
+ alias_method :title, :title # silence method redefinition warning
148
180
  def title
149
181
  keyword = serialization_options[:highlight_keyword]
150
182
  title = object.read_attribute_for_serialization(:title)
@@ -13,7 +13,7 @@ module ActionController
13
13
 
14
14
  def test_render_using_implicit_serializer
15
15
  get :render_using_implicit_serializer
16
- assert_equal 'application/json', @response.content_type
16
+ assert_includes @response.content_type, 'application/json'
17
17
  assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body
18
18
  end
19
19
  end
@@ -35,7 +35,7 @@ module ActionController
35
35
 
36
36
  def test_render_using_implicit_serializer_and_scope
37
37
  get :render_using_implicit_serializer_and_scope
38
- assert_equal 'application/json', @response.content_type
38
+ assert_includes @response.content_type, 'application/json'
39
39
  assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body
40
40
  end
41
41
  end
@@ -65,7 +65,7 @@ module ActionController
65
65
 
66
66
  def test_render_using_scope_set_in_default_serializer_options
67
67
  get :render_using_scope_set_in_default_serializer_options
68
- assert_equal 'application/json', @response.content_type
68
+ assert_includes @response.content_type, 'application/json'
69
69
  assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
70
70
  end
71
71
  end
@@ -91,7 +91,7 @@ module ActionController
91
91
 
92
92
  def test_render_using_implicit_serializer_and_explicit_scope
93
93
  get :render_using_implicit_serializer_and_explicit_scope
94
- assert_equal 'application/json', @response.content_type
94
+ assert_includes @response.content_type, 'application/json'
95
95
  assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
96
96
  end
97
97
  end
@@ -117,7 +117,7 @@ module ActionController
117
117
 
118
118
  def test_render_overriding_serialization_scope
119
119
  get :render_overriding_serialization_scope
120
- assert_equal 'application/json', @response.content_type
120
+ assert_includes @response.content_type, 'application/json'
121
121
  assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
122
122
  end
123
123
  end
@@ -141,7 +141,7 @@ module ActionController
141
141
 
142
142
  def test_render_calling_serialization_scope
143
143
  get :render_calling_serialization_scope
144
- assert_equal 'application/json', @response.content_type
144
+ assert_includes @response.content_type, 'application/json'
145
145
  assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body
146
146
  end
147
147
  end
@@ -157,7 +157,7 @@ module ActionController
157
157
 
158
158
  def test_render_using_json_dump
159
159
  get :render_using_json_dump
160
- assert_equal 'application/json', @response.content_type
160
+ assert_includes @response.content_type, 'application/json'
161
161
  assert_equal '{"hello":"world"}', @response.body
162
162
  end
163
163
  end
@@ -173,7 +173,7 @@ module ActionController
173
173
 
174
174
  def test_render_using_rails_behavior
175
175
  get :render_using_rails_behavior
176
- assert_equal 'application/json', @response.content_type
176
+ assert_includes @response.content_type, 'application/json'
177
177
  assert_equal '[{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}]', @response.body
178
178
  end
179
179
  end
@@ -189,7 +189,7 @@ module ActionController
189
189
 
190
190
  def test_render_array
191
191
  get :render_array
192
- assert_equal 'application/json', @response.content_type
192
+ assert_includes @response.content_type, 'application/json'
193
193
  assert_equal '{"my":[{"name":"Name 1","description":"Description 1"}]}', @response.body
194
194
  end
195
195
  end
@@ -205,7 +205,7 @@ module ActionController
205
205
 
206
206
  def test_render_array
207
207
  get :render_array
208
- assert_equal 'application/json', @response.content_type
208
+ assert_includes @response.content_type, 'application/json'
209
209
  assert_equal '{"webLog":[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]}', @response.body
210
210
  end
211
211
  end
@@ -223,7 +223,7 @@ module ActionController
223
223
 
224
224
  def test_render_without_root
225
225
  get :render_without_root
226
- assert_equal 'application/json', @response.content_type
226
+ assert_includes @response.content_type, 'application/json'
227
227
  assert_equal '{"name":"Name 1","displayName":"Display Name 1"}', @response.body
228
228
  end
229
229
  end
@@ -242,7 +242,7 @@ module ActionController
242
242
 
243
243
  def test_render_array_without_root
244
244
  get :render_array_without_root
245
- assert_equal 'application/json', @response.content_type
245
+ assert_includes @response.content_type, 'application/json'
246
246
  assert_equal '[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]', @response.body
247
247
  end
248
248
  end
@@ -278,7 +278,7 @@ module ActionController
278
278
  @association.embed_in_root = true
279
279
 
280
280
  get :render_array_embeding_in_root
281
- assert_equal 'application/json', @response.content_type
281
+ assert_includes @response.content_type, 'application/json'
282
282
 
283
283
  assert_equal("{\"my\":[{\"name\":\"Name 1\",\"email\":\"mail@server.com\",\"profile_id\":#{@controller.user.profile.object_id}}],\"profiles\":[{\"name\":\"N1\",\"description\":\"D1\"}]}", @response.body)
284
284
  end
@@ -9,7 +9,7 @@ module ActionController
9
9
  end
10
10
 
11
11
  def render_text
12
- render text: 'ok'
12
+ render plain: 'ok'
13
13
  end
14
14
 
15
15
  def render_template
@@ -17,44 +17,44 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
17
17
 
18
18
  assert_file 'app/controllers/accounts_controller.rb' do |content|
19
19
  assert_instance_method :index, content do |m|
20
- assert_match /@accounts = Account\.all/, m
21
- assert_match /format.html/, m
22
- assert_match /format.json \{ render json: @accounts \}/, m
20
+ assert_match(/@accounts = Account\.all/, m)
21
+ assert_match(/format.html/, m)
22
+ assert_match(/format.json \{ render json: @accounts \}/, m)
23
23
  end
24
24
 
25
25
  assert_instance_method :show, content do |m|
26
- assert_match /format.html/, m
27
- assert_match /format.json \{ render json: @account \}/, m
26
+ assert_match(/format.html/, m)
27
+ assert_match(/format.json \{ render json: @account \}/, m)
28
28
  end
29
29
 
30
30
  assert_instance_method :new, content do |m|
31
- assert_match /@account = Account\.new/, m
31
+ assert_match(/@account = Account\.new/, m)
32
32
  end
33
33
 
34
34
  assert_instance_method :edit, content do |m|
35
- assert m.blank?
35
+ assert_predicate m, :blank?
36
36
  end
37
37
 
38
38
  assert_instance_method :create, content do |m|
39
- assert_match /@account = Account\.new\(account_params\)/, m
40
- assert_match /@account\.save/, m
41
- assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m
42
- assert_match /format\.json \{ render json: @account, status: :created \}/, m
43
- assert_match /format\.html \{ render action: 'new' \}/, m
44
- assert_match /format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m
39
+ assert_match(/@account = Account\.new\(account_params\)/, m)
40
+ assert_match(/@account\.save/, m)
41
+ assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m)
42
+ assert_match(/format\.json \{ render json: @account, status: :created \}/, m)
43
+ assert_match(/format\.html \{ render action: 'new' \}/, m)
44
+ assert_match(/format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m)
45
45
  end
46
46
 
47
47
  assert_instance_method :update, content do |m|
48
- assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m
49
- assert_match /format\.json \{ head :no_content \}/, m
50
- assert_match /format\.html \{ render action: 'edit' \}/, m
51
- assert_match /format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m
48
+ assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m)
49
+ assert_match(/format\.json \{ head :no_content \}/, m)
50
+ assert_match(/format\.html \{ render action: 'edit' \}/, m)
51
+ assert_match(/format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m)
52
52
  end
53
53
 
54
54
  assert_instance_method :destroy, content do |m|
55
- assert_match /@account\.destroy/, m
56
- assert_match /format\.html { redirect_to accounts_url \}/, m
57
- assert_match /format\.json \{ head :no_content \}/, m
55
+ assert_match(/@account\.destroy/, m)
56
+ assert_match(/format\.html { redirect_to accounts_url \}/, m)
57
+ assert_match(/format\.json \{ head :no_content \}/, m)
58
58
  end
59
59
 
60
60
  assert_match(/def account_params/, content)