activemodel 5.2.4.1 → 6.0.1

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +115 -66
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -2
  5. data/lib/active_model.rb +1 -1
  6. data/lib/active_model/attribute.rb +3 -4
  7. data/lib/active_model/attribute/user_provided_default.rb +1 -2
  8. data/lib/active_model/attribute_assignment.rb +1 -1
  9. data/lib/active_model/attribute_methods.rb +54 -15
  10. data/lib/active_model/attribute_mutation_tracker.rb +88 -34
  11. data/lib/active_model/attribute_set.rb +2 -10
  12. data/lib/active_model/attribute_set/builder.rb +1 -3
  13. data/lib/active_model/attribute_set/yaml_encoder.rb +1 -2
  14. data/lib/active_model/attributes.rb +60 -33
  15. data/lib/active_model/callbacks.rb +10 -7
  16. data/lib/active_model/conversion.rb +1 -1
  17. data/lib/active_model/dirty.rb +36 -99
  18. data/lib/active_model/errors.rb +104 -20
  19. data/lib/active_model/gem_version.rb +4 -4
  20. data/lib/active_model/naming.rb +19 -3
  21. data/lib/active_model/railtie.rb +6 -0
  22. data/lib/active_model/secure_password.rb +47 -48
  23. data/lib/active_model/serializers/json.rb +10 -9
  24. data/lib/active_model/type/binary.rb +1 -1
  25. data/lib/active_model/type/date.rb +0 -4
  26. data/lib/active_model/type/date_time.rb +3 -7
  27. data/lib/active_model/type/float.rb +0 -2
  28. data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +4 -0
  29. data/lib/active_model/type/helpers/numeric.rb +9 -2
  30. data/lib/active_model/type/helpers/time_value.rb +17 -4
  31. data/lib/active_model/type/integer.rb +7 -19
  32. data/lib/active_model/type/registry.rb +2 -10
  33. data/lib/active_model/type/string.rb +2 -2
  34. data/lib/active_model/type/time.rb +1 -5
  35. data/lib/active_model/validations.rb +0 -2
  36. data/lib/active_model/validations/acceptance.rb +33 -25
  37. data/lib/active_model/validations/clusivity.rb +1 -1
  38. data/lib/active_model/validations/confirmation.rb +2 -2
  39. data/lib/active_model/validations/inclusion.rb +1 -1
  40. data/lib/active_model/validations/length.rb +1 -1
  41. data/lib/active_model/validations/numericality.rb +2 -2
  42. data/lib/active_model/validations/validates.rb +2 -2
  43. data/lib/active_model/validator.rb +1 -1
  44. metadata +11 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83a00a8ccda1f7bc08d9ca9e1e41a41c427e8b4f3000e5b964cf18c0a8ada81f
4
- data.tar.gz: 1b0b6c250e99625842859ec2f155b5bc7417b92660e6a4457e61dcea7f10bc6a
3
+ metadata.gz: 5992f7fea7f331bfcbf56839011642efc7ac14a5ac2f4681606f7f7e41b7e939
4
+ data.tar.gz: c92f05ecae956123a685f85fc896ad787ded8527758ac77a4e33ac0e79702dd3
5
5
  SHA512:
6
- metadata.gz: deb1f4ffc1f48bc58c78492597fb14b3807c33efde5bb8968e187cf6aa2b1a9bb032477e4fec5843e3affbf0045ad9a71e49612f7e8358ce21b99df9f26811c4
7
- data.tar.gz: 863d84cd43e7cbb387604407ad891f4d2fcfd890acc86b5df0be340d36a00e3f3cbb393f0a04b367e8fceda9719f8347315a622b7a538cfa876dfbac010d2491
6
+ metadata.gz: 1e6fb6f24207008629fae8337232c6214e01b8fa80b475c4a2a5ed5f13a2c5c49229363ee9b86d6aaa3d6e070717f8e46ceb2cc92f265ea02d8793bdf5ccf61c
7
+ data.tar.gz: 7c4fa5a56193747e8d247a487c2faccfb33fc3bc5dc378aa46a964c232336f248ee43b73d9c6329c05b626ffcdc562083bba89dd71191573ae00277043f42563
@@ -1,9 +1,19 @@
1
- ## Rails 5.2.4.1 (December 18, 2019) ##
1
+ ## Rails 6.0.1 (November 5, 2019) ##
2
2
 
3
3
  * No changes.
4
4
 
5
5
 
6
- ## Rails 5.2.4 (November 27, 2019) ##
6
+ ## Rails 6.0.0 (August 16, 2019) ##
7
+
8
+ * No changes.
9
+
10
+
11
+ ## Rails 6.0.0.rc2 (July 22, 2019) ##
12
+
13
+ * No changes.
14
+
15
+
16
+ ## Rails 6.0.0.rc1 (April 24, 2019) ##
7
17
 
8
18
  * Type cast falsy boolean symbols on boolean attribute as false.
9
19
 
@@ -11,8 +21,36 @@
11
21
 
12
22
  *Ryuta Kamizono*
13
23
 
24
+ * Change how validation error translation strings are fetched: The new behavior
25
+ will first try the more specific keys, including doing locale fallback, then try
26
+ the less specific ones.
27
+
28
+ For example, this is the order in which keys will now be tried for a `blank`
29
+ error on a `product`'s `title` attribute with current locale set to `en-US`:
30
+
31
+ en-US.activerecord.errors.models.product.attributes.title.blank
32
+ en-US.activerecord.errors.models.product.blank
33
+ en-US.activerecord.errors.messages.blank
34
+
35
+ en.activerecord.errors.models.product.attributes.title.blank
36
+ en.activerecord.errors.models.product.blank
37
+ en.activerecord.errors.messages.blank
14
38
 
15
- ## Rails 5.2.3 (March 27, 2019) ##
39
+ en-US.errors.attributes.title.blank
40
+ en-US.errors.messages.blank
41
+
42
+ en.errors.attributes.title.blank
43
+ en.errors.messages.blank
44
+
45
+ *Hugo Vacher*
46
+
47
+
48
+ ## Rails 6.0.0.beta3 (March 11, 2019) ##
49
+
50
+ * No changes.
51
+
52
+
53
+ ## Rails 6.0.0.beta2 (February 25, 2019) ##
16
54
 
17
55
  * Fix date value when casting a multiparameter date hash to not convert
18
56
  from Gregorian date to Julian date.
@@ -20,109 +58,120 @@
20
58
  Before:
21
59
 
22
60
  Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
23
- => #<Day id: nil, day: "0001-01-03", created_at: nil, updated_at: nil>
61
+ # => #<Day id: nil, day: "0001-01-03", created_at: nil, updated_at: nil>
24
62
 
25
63
  After:
26
64
 
27
65
  Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
28
- => #<Day id: nil, day: "0001-01-01", created_at: nil, updated_at: nil>
66
+ # => #<Day id: nil, day: "0001-01-01", created_at: nil, updated_at: nil>
29
67
 
30
68
  Fixes #28521.
31
69
 
32
70
  *Sayan Chakraborty*
33
71
 
34
- * Fix numericality equality validation of `BigDecimal` and `Float`
35
- by casting to `BigDecimal` on both ends of the validation.
72
+ * Fix year value when casting a multiparameter time hash.
36
73
 
37
- *Gannon McGibbon*
74
+ When assigning a hash to a time attribute that's missing a year component
75
+ (e.g. a `time_select` with `:ignore_date` set to `true`) then the year
76
+ defaults to 1970 instead of the expected 2000. This results in the attribute
77
+ changing as a result of the save.
38
78
 
79
+ Before:
80
+ ```
81
+ event = Event.new(start_time: { 4 => 20, 5 => 30 })
82
+ event.start_time # => 1970-01-01 20:30:00 UTC
83
+ event.save
84
+ event.reload
85
+ event.start_time # => 2000-01-01 20:30:00 UTC
86
+ ```
39
87
 
40
- ## Rails 5.2.2.1 (March 11, 2019) ##
88
+ After:
89
+ ```
90
+ event = Event.new(start_time: { 4 => 20, 5 => 30 })
91
+ event.start_time # => 2000-01-01 20:30:00 UTC
92
+ event.save
93
+ event.reload
94
+ event.start_time # => 2000-01-01 20:30:00 UTC
95
+ ```
41
96
 
42
- * No changes.
97
+ *Andrew White*
43
98
 
44
99
 
45
- ## Rails 5.2.2 (December 04, 2018) ##
100
+ ## Rails 6.0.0.beta1 (January 18, 2019) ##
46
101
 
47
- * Fix numericality validator to still use value before type cast except Active Record.
102
+ * Internal calls to `human_attribute_name` on an `Active Model` now pass attributes as strings instead of symbols
103
+ in some cases.
48
104
 
49
- Fixes #33651, #33686.
105
+ This is in line with examples in Rails docs and puts the code in line with the intention -
106
+ the potential use of strings or symbols.
50
107
 
51
- *Ryuta Kamizono*
108
+ It is recommended to cast the attribute input to your desired type as if you you are overriding that methid.
52
109
 
110
+ *Martin Larochelle*
53
111
 
54
- ## Rails 5.2.1.1 (November 27, 2018) ##
112
+ * Add `ActiveModel::Errors#of_kind?`.
55
113
 
56
- * No changes.
57
-
58
-
59
- ## Rails 5.2.1 (August 07, 2018) ##
60
-
61
- * No changes.
114
+ *bogdanvlviv*, *Rafael Mendonça França*
62
115
 
116
+ * Fix numericality equality validation of `BigDecimal` and `Float`
117
+ by casting to `BigDecimal` on both ends of the validation.
63
118
 
64
- ## Rails 5.2.0 (April 09, 2018) ##
119
+ *Gannon McGibbon*
65
120
 
66
- * Do not lose all multiple `:includes` with options in serialization.
121
+ * Add `#slice!` method to `ActiveModel::Errors`.
67
122
 
68
- *Mike Mangino*
123
+ *Daniel López Prat*
69
124
 
70
- * Models using the attributes API with a proc default can now be marshalled.
125
+ * Fix numericality validator to still use value before type cast except Active Record.
71
126
 
72
- Fixes #31216.
127
+ Fixes #33651, #33686.
73
128
 
74
- *Sean Griffin*
129
+ *Ryuta Kamizono*
75
130
 
76
- * Fix to working before/after validation callbacks on multiple contexts.
131
+ * Fix `ActiveModel::Serializers::JSON#as_json` method for timestamps.
77
132
 
78
- *Yoshiyuki Hirano*
133
+ Before:
134
+ ```
135
+ contact = Contact.new(created_at: Time.utc(2006, 8, 1))
136
+ contact.as_json["created_at"] # => 2006-08-01 00:00:00 UTC
137
+ ```
79
138
 
80
- * Execute `ConfirmationValidator` validation when `_confirmation`'s value is `false`.
139
+ After:
140
+ ```
141
+ contact = Contact.new(created_at: Time.utc(2006, 8, 1))
142
+ contact.as_json["created_at"] # => "2006-08-01T00:00:00.000Z"
143
+ ```
81
144
 
82
- *bogdanvlviv*
145
+ *Bogdan Gusiev*
83
146
 
84
- * Allow passing a Proc or Symbol to length validator options.
147
+ * Allows configurable attribute name for `#has_secure_password`. This
148
+ still defaults to an attribute named 'password', causing no breaking
149
+ change. There is a new method `#authenticate_XXX` where XXX is the
150
+ configured attribute name, making the existing `#authenticate` now an
151
+ alias for this when the attribute is the default 'password'.
85
152
 
86
- *Matt Rohrer*
153
+ Example:
87
154
 
88
- * Add method `#merge!` for `ActiveModel::Errors`.
155
+ class User < ActiveRecord::Base
156
+ has_secure_password :recovery_password, validations: false
157
+ end
89
158
 
90
- *Jahfer Husain*
159
+ user = User.new()
160
+ user.recovery_password = "42password"
161
+ user.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uX..."
162
+ user.authenticate_recovery_password('42password') # => user
91
163
 
92
- * Fix regression in numericality validator when comparing Decimal and Float input
93
- values with more scale than the schema.
164
+ *Unathi Chonco*
94
165
 
95
- *Bradley Priest*
166
+ * Add `config.active_model.i18n_customize_full_message` in order to control whether
167
+ the `full_message` error format can be overridden at the attribute or model
168
+ level in the locale files. This is `false` by default.
96
169
 
97
- * Fix methods `#keys`, `#values` in `ActiveModel::Errors`.
170
+ *Martin Larochelle*
98
171
 
99
- Change `#keys` to only return the keys that don't have empty messages.
172
+ * Rails 6 requires Ruby 2.5.0 or newer.
100
173
 
101
- Change `#values` to only return the not empty values.
174
+ *Jeremy Daer*, *Kasper Timm Hansen*
102
175
 
103
- Example:
104
176
 
105
- # Before
106
- person = Person.new
107
- person.errors.keys # => []
108
- person.errors.values # => []
109
- person.errors.messages # => {}
110
- person.errors[:name] # => []
111
- person.errors.messages # => {:name => []}
112
- person.errors.keys # => [:name]
113
- person.errors.values # => [[]]
114
-
115
- # After
116
- person = Person.new
117
- person.errors.keys # => []
118
- person.errors.values # => []
119
- person.errors.messages # => {}
120
- person.errors[:name] # => []
121
- person.errors.messages # => {:name => []}
122
- person.errors.keys # => []
123
- person.errors.values # => []
124
-
125
- *bogdanvlviv*
126
-
127
-
128
- Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activemodel/CHANGELOG.md) for previous changes.
177
+ Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activemodel/CHANGELOG.md) for previous changes.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2004-2018 David Heinemeier Hansson
1
+ Copyright (c) 2004-2019 David Heinemeier Hansson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -5,6 +5,8 @@ They allow for Action Pack helpers to interact with non-Active Record models,
5
5
  for example. Active Model also helps with building custom ORMs for use outside of
6
6
  the Rails framework.
7
7
 
8
+ You can read more about Active Model in the {Active Model Basics}[https://edgeguides.rubyonrails.org/active_model_basics.html] guide.
9
+
8
10
  Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
9
11
  interact with Action Pack helpers, it was required to either copy chunks of
10
12
  code from Rails, or monkey patch entire helpers to make them handle objects
@@ -239,7 +241,7 @@ The latest version of Active Model can be installed with RubyGems:
239
241
 
240
242
  Source code can be downloaded as part of the Rails project on GitHub
241
243
 
242
- * https://github.com/rails/rails/tree/5-2-stable/activemodel
244
+ * https://github.com/rails/rails/tree/master/activemodel
243
245
 
244
246
 
245
247
  == License
@@ -253,7 +255,7 @@ Active Model is released under the MIT license:
253
255
 
254
256
  API documentation is at:
255
257
 
256
- * http://api.rubyonrails.org
258
+ * https://api.rubyonrails.org
257
259
 
258
260
  Bug reports for the Ruby on Rails project can be filed here:
259
261
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  #--
4
- # Copyright (c) 2004-2018 David Heinemeier Hansson
4
+ # Copyright (c) 2004-2019 David Heinemeier Hansson
5
5
  #
6
6
  # Permission is hereby granted, free of charge, to any person obtaining
7
7
  # a copy of this software and associated documentation files (the
@@ -133,10 +133,6 @@ module ActiveModel
133
133
  end
134
134
 
135
135
  protected
136
-
137
- attr_reader :original_attribute
138
- alias_method :assigned?, :original_attribute
139
-
140
136
  def original_value_for_database
141
137
  if assigned?
142
138
  original_attribute.original_value_for_database
@@ -146,6 +142,9 @@ module ActiveModel
146
142
  end
147
143
 
148
144
  private
145
+ attr_reader :original_attribute
146
+ alias :assigned? :original_attribute
147
+
149
148
  def initialize_dup(other)
150
149
  if defined?(@value) && @value.duplicable?
151
150
  @value = @value.dup
@@ -44,8 +44,7 @@ module ActiveModel
44
44
  end
45
45
  end
46
46
 
47
- protected
48
-
47
+ private
49
48
  attr_reader :user_provided_value
50
49
  end
51
50
  end
@@ -27,7 +27,7 @@ module ActiveModel
27
27
  # cat.status # => 'sleeping'
28
28
  def assign_attributes(new_attributes)
29
29
  if !new_attributes.respond_to?(:stringify_keys)
30
- raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
30
+ raise ArgumentError, "When assigning attributes, you must pass a hash as an argument, #{new_attributes.class} passed."
31
31
  end
32
32
  return if new_attributes.empty?
33
33
 
@@ -286,12 +286,12 @@ module ActiveModel
286
286
  method_name = matcher.method_name(attr_name)
287
287
 
288
288
  unless instance_method_already_implemented?(method_name)
289
- generate_method = "define_method_#{matcher.method_missing_target}"
289
+ generate_method = "define_method_#{matcher.target}"
290
290
 
291
291
  if respond_to?(generate_method, true)
292
292
  send(generate_method, attr_name.to_s)
293
293
  else
294
- define_proxy_call true, generated_attribute_methods, method_name, matcher.method_missing_target, attr_name.to_s
294
+ define_proxy_call true, generated_attribute_methods, method_name, matcher.target, attr_name.to_s
295
295
  end
296
296
  end
297
297
  end
@@ -352,53 +352,54 @@ module ActiveModel
352
352
 
353
353
  def attribute_method_matchers_matching(method_name)
354
354
  attribute_method_matchers_cache.compute_if_absent(method_name) do
355
- # Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix
356
- # will match every time.
355
+ # Bump plain matcher to last place so that only methods that do not
356
+ # match any other pattern match the actual attribute name.
357
+ # This is currently only needed to support legacy usage.
357
358
  matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1)
358
- matchers.map { |method| method.match(method_name) }.compact
359
+ matchers.map { |matcher| matcher.match(method_name) }.compact
359
360
  end
360
361
  end
361
362
 
362
363
  # Define a method `name` in `mod` that dispatches to `send`
363
364
  # using the given `extra` args. This falls back on `define_method`
364
365
  # and `send` if the given names cannot be compiled.
365
- def define_proxy_call(include_private, mod, name, send, *extra)
366
+ def define_proxy_call(include_private, mod, name, target, *extra)
366
367
  defn = if NAME_COMPILABLE_REGEXP.match?(name)
367
368
  "def #{name}(*args)"
368
369
  else
369
370
  "define_method(:'#{name}') do |*args|"
370
371
  end
371
372
 
372
- extra = (extra.map!(&:inspect) << "*args").join(", ".freeze)
373
+ extra = (extra.map!(&:inspect) << "*args").join(", ")
373
374
 
374
- target = if CALL_COMPILABLE_REGEXP.match?(send)
375
- "#{"self." unless include_private}#{send}(#{extra})"
375
+ body = if CALL_COMPILABLE_REGEXP.match?(target)
376
+ "#{"self." unless include_private}#{target}(#{extra})"
376
377
  else
377
- "send(:'#{send}', #{extra})"
378
+ "send(:'#{target}', #{extra})"
378
379
  end
379
380
 
380
381
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
381
382
  #{defn}
382
- #{target}
383
+ #{body}
383
384
  end
384
385
  RUBY
385
386
  end
386
387
 
387
388
  class AttributeMethodMatcher #:nodoc:
388
- attr_reader :prefix, :suffix, :method_missing_target
389
+ attr_reader :prefix, :suffix, :target
389
390
 
390
- AttributeMethodMatch = Struct.new(:target, :attr_name, :method_name)
391
+ AttributeMethodMatch = Struct.new(:target, :attr_name)
391
392
 
392
393
  def initialize(options = {})
393
394
  @prefix, @suffix = options.fetch(:prefix, ""), options.fetch(:suffix, "")
394
395
  @regex = /^(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})$/
395
- @method_missing_target = "#{@prefix}attribute#{@suffix}"
396
+ @target = "#{@prefix}attribute#{@suffix}"
396
397
  @method_name = "#{prefix}%s#{suffix}"
397
398
  end
398
399
 
399
400
  def match(method_name)
400
401
  if @regex =~ method_name
401
- AttributeMethodMatch.new(method_missing_target, $1, method_name)
402
+ AttributeMethodMatch.new(target, $1)
402
403
  end
403
404
  end
404
405
 
@@ -474,5 +475,43 @@ module ActiveModel
474
475
  def _read_attribute(attr)
475
476
  __send__(attr)
476
477
  end
478
+
479
+ module AttrNames # :nodoc:
480
+ DEF_SAFE_NAME = /\A[a-zA-Z_]\w*\z/
481
+
482
+ # We want to generate the methods via module_eval rather than
483
+ # define_method, because define_method is slower on dispatch.
484
+ # Evaluating many similar methods may use more memory as the instruction
485
+ # sequences are duplicated and cached (in MRI). define_method may
486
+ # be slower on dispatch, but if you're careful about the closure
487
+ # created, then define_method will consume much less memory.
488
+ #
489
+ # But sometimes the database might return columns with
490
+ # characters that are not allowed in normal method names (like
491
+ # 'my_column(omg)'. So to work around this we first define with
492
+ # the __temp__ identifier, and then use alias method to rename
493
+ # it to what we want.
494
+ #
495
+ # We are also defining a constant to hold the frozen string of
496
+ # the attribute name. Using a constant means that we do not have
497
+ # to allocate an object on each call to the attribute method.
498
+ # Making it frozen means that it doesn't get duped when used to
499
+ # key the @attributes in read_attribute.
500
+ def self.define_attribute_accessor_method(mod, attr_name, writer: false)
501
+ method_name = "#{attr_name}#{'=' if writer}"
502
+ if attr_name.ascii_only? && DEF_SAFE_NAME.match?(attr_name)
503
+ yield method_name, "'#{attr_name}'.freeze"
504
+ else
505
+ safe_name = attr_name.unpack1("h*")
506
+ const_name = "ATTR_#{safe_name}"
507
+ const_set(const_name, attr_name) unless const_defined?(const_name)
508
+ temp_method_name = "__temp__#{safe_name}#{'=' if writer}"
509
+ attr_name_expr = "::ActiveModel::AttributeMethods::AttrNames::#{const_name}"
510
+ yield temp_method_name, attr_name_expr
511
+ mod.alias_method method_name, temp_method_name
512
+ mod.undef_method temp_method_name
513
+ end
514
+ end
515
+ end
477
516
  end
478
517
  end