active_model_serializers 0.9.11 → 0.9.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1c2dbb7d1b6d7673364e690d624e5a8dffa5fddf2f9ee9d6838ac399ab14a96
4
- data.tar.gz: 5fcef7df6ec5deb692b68ffd8198e438b8bf2bafe812d12ace97087f9eb9c3f8
3
+ metadata.gz: a8649ea9bfdefabb0215cc90a6a47a87d902573ecd0e2c73b6142c9cefe17825
4
+ data.tar.gz: 420e8afdc1ba7cbaaab42d7f8f36c3e10b702587cc7484e270428ded2eeccbdd
5
5
  SHA512:
6
- metadata.gz: 281b4a64ab65e4f2aff6b45a393e684e1d69a5fe203c450268cfc001deb20fce6e1269c9aafd1bf54e847bc5a282cfd28f630cb45096ec750bb531363cd93cbd
7
- data.tar.gz: 13cd7cfa482532515a8629c35c4e5c0847b1dd791494025f4d6177e05f45f5845641b8bc04cfb543a503b8767afcb8505fd1c433afc0cd73c5bcd0d9a8066119
6
+ metadata.gz: 237f372c6dd5e73cb7d5f944d90918a23e6acf0f7aa137f0e2972a275445a549a6cf4ffd9e410386c611e89e1dc0137d5451c6d61702d6c6381e43a41e3434ac
7
+ data.tar.gz: 5d009500e9e616e7d1a7f83adaf7874022f1f039594983e923ef82d4b8f6b8f6268259404b097545a27512b149ec22c8e113a8c0bb51effbef3c0fdd728d7973
data/CHANGELOG.md CHANGED
@@ -1,6 +1,21 @@
1
1
  ## 0.09.x
2
2
 
3
- ### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.11...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)
4
19
 
5
20
  ### [v0.9.11 (2024-04-09)](https://github.com/rails-api/active_model_serializers/compare/v0.9.10...v0.9.11)
6
21
 
@@ -9,9 +9,7 @@ module ActiveModel
9
9
  class Association
10
10
  def initialize(name, options={})
11
11
  if options.has_key?(:include)
12
- ActiveSupport::Deprecation.warn <<-WARN
13
- ** Notice: include was renamed to embed_in_root. **
14
- WARN
12
+ ActiveSupport::Deprecation.warn("** Notice: include was renamed to embed_in_root. **")
15
13
  end
16
14
 
17
15
  @name = name.to_s
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveModel
4
4
  class Serializer
5
- VERSION = '0.9.11'
5
+ VERSION = '0.9.13'
6
6
  end
7
7
  end
@@ -62,20 +62,19 @@ end
62
62
  if resource.respond_to?(:serializer_class)
63
63
  resource.serializer_class
64
64
  elsif resource.respond_to?(:to_ary)
65
- if Object.constants.include?(:ArraySerializer)
65
+ if defined?(::ArraySerializer)
66
66
  ::ArraySerializer
67
67
  else
68
68
  ArraySerializer
69
69
  end
70
70
  else
71
- search_list = build_serializer_class_list(resource, options)
72
- result = search_list.map do |klass_name|
73
- Serializer.serializers_cache.fetch_or_store(klass_name) do
74
- _const_get(klass_name)
75
- end
76
- end
77
-
78
- result.find { |serializer| !serializer.nil? }
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?
76
+ end
77
+ nil
79
78
  end
80
79
  end
81
80
 
@@ -91,15 +90,27 @@ end
91
90
  end
92
91
 
93
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"]
94
99
  attrs.each do |attr|
95
100
  striped_attr = strip_attribute attr
96
101
 
97
102
  @_attributes << striped_attr
98
103
 
99
- define_method striped_attr do
100
- object.read_attribute_for_serialization attr
101
- 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
102
111
  end
112
+ caller = caller_locations(1, 1).first
113
+ class_eval(source.join(";"), caller.path, caller.lineno - 1)
103
114
  end
104
115
 
105
116
  def has_one(*attrs)
@@ -124,22 +135,17 @@ end
124
135
  attr
125
136
  end
126
137
 
127
- def build_serializer_class_list(resource, options)
128
- list = []
129
- list << build_serializer_class(resource, options)
130
- list << build_serializer_class(resource, {})
131
- list << build_serializer_class(resource.class.name.demodulize, {})
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, {})
132
142
  end
133
143
 
134
144
  def build_serializer_class(resource, options)
135
145
  klass_name = +""
136
146
  klass_name << "#{options[:namespace]}::" if options[:namespace]
137
147
  klass_name << options[:prefix].to_s.classify if options[:prefix]
138
- if resource.is_a?(String)
139
- klass_name << "#{resource}Serializer"
140
- else
141
- klass_name << "#{resource.class.name}Serializer"
142
- end
148
+ klass_name << "#{resource.class.name}Serializer"
143
149
  end
144
150
 
145
151
  def associate(klass, *attrs)
@@ -169,7 +175,8 @@ end
169
175
  @context = options[:context]
170
176
  @namespace = options[:namespace]
171
177
  end
172
- 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
173
180
 
174
181
  def json_key
175
182
  key = if root == true || root.nil?
@@ -144,14 +144,14 @@ end
144
144
 
145
145
  class SelfReferencingUserParentSerializer < ActiveModel::Serializer
146
146
  attributes :name
147
- has_one :type, serializer: TypeSerializer, embed: :ids, include: true
147
+ has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true
148
148
  end
149
149
 
150
150
  class SelfReferencingUserSerializer < ActiveModel::Serializer
151
151
  attributes :name
152
152
 
153
- has_one :type, serializer: TypeSerializer, embed: :ids, include: true
154
- 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
155
155
  end
156
156
 
157
157
  class UserInfoSerializer < ActiveModel::Serializer
@@ -176,6 +176,7 @@ end
176
176
  class PostSerializer < ActiveModel::Serializer
177
177
  attributes :title, :body
178
178
 
179
+ alias_method :title, :title # silence method redefinition warning
179
180
  def title
180
181
  keyword = serialization_options[:highlight_keyword]
181
182
  title = object.read_attribute_for_serialization(:title)
@@ -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)
data/test/test_app.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  class TestApp < Rails::Application
2
+ if config.respond_to?(:load_defaults)
3
+ config.load_defaults("#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}")
4
+ end
5
+
2
6
  if Rails.version.to_s.first >= '4'
3
7
  config.eager_load = false
4
8
  config.secret_key_base = 'abc123'
@@ -1,4 +1,3 @@
1
- # frozen_string_literal: true
2
- class AccountSerializer < ActiveModel::Serializer
1
+ class AccountSerializer < MySerializer
3
2
  attributes :id
4
3
  end
@@ -49,7 +49,7 @@ module ActiveModel
49
49
  def object.serializer_class; CustomSerializer; end
50
50
 
51
51
  assert_equal CustomSerializer, Serializer.serializer_for(object)
52
- assert_equal CustomSerializer, Serializer.serializer_for('Custom')
52
+ assert_nil Serializer.serializer_for('Custom')
53
53
  end
54
54
  end
55
55
 
@@ -4,7 +4,7 @@ module ActiveModel
4
4
  class DefaultSerializer
5
5
  class Test < Minitest::Test
6
6
  def test_serialize_objects
7
- assert_equal(nil, DefaultSerializer.new(nil).serializable_object)
7
+ assert_nil(DefaultSerializer.new(nil).serializable_object)
8
8
  assert_equal(1, DefaultSerializer.new(1).serializable_object)
9
9
  assert_equal('hi', DefaultSerializer.new('hi').serializable_object)
10
10
  end
@@ -21,6 +21,7 @@ module ActiveModel
21
21
  serializer = Class.new(ActiveModel::Serializer) do
22
22
  attributes :url
23
23
 
24
+ alias_method :url, :url # silence redefinition warning
24
25
  def url
25
26
  profile_url(id: object.object_id)
26
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 0.9.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-04-09 00:00:00.000000000 Z
13
+ date: 2024-09-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
@@ -115,12 +115,7 @@ files:
115
115
  - test/integration/generators/serializer_generator_test.rb
116
116
  - test/test_app.rb
117
117
  - test/test_helper.rb
118
- - test/tmp/app/assets/javascripts/accounts.js
119
- - test/tmp/app/assets/stylesheets/accounts.css
120
- - test/tmp/app/controllers/accounts_controller.rb
121
- - test/tmp/app/helpers/accounts_helper.rb
122
118
  - test/tmp/app/serializers/account_serializer.rb
123
- - test/tmp/config/routes.rb
124
119
  - test/unit/active_model/array_serializer/except_test.rb
125
120
  - test/unit/active_model/array_serializer/key_format_test.rb
126
121
  - test/unit/active_model/array_serializer/meta_test.rb
@@ -198,12 +193,7 @@ test_files:
198
193
  - test/integration/generators/serializer_generator_test.rb
199
194
  - test/test_app.rb
200
195
  - test/test_helper.rb
201
- - test/tmp/app/assets/javascripts/accounts.js
202
- - test/tmp/app/assets/stylesheets/accounts.css
203
- - test/tmp/app/controllers/accounts_controller.rb
204
- - test/tmp/app/helpers/accounts_helper.rb
205
196
  - test/tmp/app/serializers/account_serializer.rb
206
- - test/tmp/config/routes.rb
207
197
  - test/unit/active_model/array_serializer/except_test.rb
208
198
  - test/unit/active_model/array_serializer/key_format_test.rb
209
199
  - test/unit/active_model/array_serializer/meta_test.rb
@@ -1,2 +0,0 @@
1
- // Place all the behaviors and hooks related to the matching controller here.
2
- // All this logic will automatically be available in application.js.
@@ -1,4 +0,0 @@
1
- /*
2
- Place all the styles related to the matching controller here.
3
- They will automatically be included in application.css.
4
- */
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
- class AccountsController < ApplicationController
3
- end
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
- module AccountsHelper
3
- end
@@ -1,2 +0,0 @@
1
- # frozen_string_literal: true
2
- Rails.application.routes.draw {}