Commit Graph

2374 Commits

Author SHA1 Message Date
Jean Boussier
39341de433 Remove useless include_private parameter in define_proxy_call
Since Ruby 2.7 `self.some_private_method` works fine.
So now that Ruby 2.7 is the minimal supported version,
`define_proxy_call` can always prepend `self.`
2021-03-20 14:27:53 -04:00
Ryuta Kamizono
c435af140e Don't use type.cast(value) to emulate unchecked serialized value in unboundable?
I used `type.cast(value)` to emulate unchecked serialized value in
`unboundable?`, since `RangeError` was raised only for the integer type,
so the emulation works enough for the integer type.

But since #41516, Enum types are also not always serializable, so
`type.cast(value)` may also be called for the enum types.

I've delegated `type.cast(value)` to the subtype if an unknown label is
passed to work the emulation even on Enum types in 3b6461b. But it is
strange to delegate to the subtype for the emulation only if an unknown
label is passed.

Instead of using `type.cast(value)` for the emulation, extend
`serializable?` to get unchecked serialized value if the value is not
serializable.
2021-03-15 12:23:40 +09:00
Ryuta Kamizono
4607f13c88 Use serializable? instead of rescue ::RangeError 2021-03-10 21:21:22 +09:00
Ryuta Kamizono
6ee96a8f42 Avoid extra query attribute allocation in _insert_record/_update_record
Since a `BindParam` object always has an attribute object as the value
in the Active Record usage, so `_insert_record`/`_update_record` could
be passed attribute set instead of wrapping casted value by a query
attribute.
2021-03-01 16:24:31 +09:00
Artem Yegorov
89db42b5e1
Add uncountable? method to ActiveModel::Name 2021-02-18 09:55:29 +03:00
Jonathan Hefner
167f5c8065 Fix inline code markup [ci-skip]
RDoc Markup does not support backticks the way Markdown does to mark up
inline code.  Additionally, `<tt>` must be used to mark up inline code
that includes spaces or certain punctuation characters (e.g. quotes).
2021-02-14 11:20:35 -06:00
Ryuta Kamizono
56ea638290 A value acts like time object should respond to getutc and getlocal 2021-02-11 15:05:25 +09:00
Ricardo Díaz
93cbc30f34 Use Enumerator#all? and Enumerator#any? with classes instead of iterations
These methods have changed in Ruby 2.5 to be more akin to grep:

https://bugs.ruby-lang.org/issues/11286

Using classes seems to be faster (and a bit more expressive) than iterating over
the collection items:

```
Warming up --------------------------------------
    #all? with class   504.000  i/100ms
     #all? with proc   189.000  i/100ms
Calculating -------------------------------------
    #all? with class      4.960k (± 1.6%) i/s -     25.200k in   5.082049s
     #all? with proc      1.874k (± 2.8%) i/s -      9.450k in   5.047866s

Comparison:
    #all? with class:     4959.9 i/s
     #all? with proc:     1873.8 i/s - 2.65x  (± 0.00) slower
```

Benchmark script:

```ruby
require "minitest/autorun"
require "benchmark/ips"

class BugTest < Minitest::Test
  def test_enumerators_with_classes
    arr = (1..10000).to_a << nil

    assert_equal arr.all?(Integer), arr.all? { |v| v.is_a?(Integer) }

    Benchmark.ips do |x|
      x.report("#all? with class") do
        arr.all?(Integer)
      end

      x.report("#all? with proc") do
        arr.all? { |v| v.is_a?(Integer) }
      end

      x.compare!
    end
  end
end
```
2021-02-07 01:29:50 -05:00
Ryuta Kamizono
978308ac87 Fix markup in CHANGELOGs [ci skip] 2021-02-07 05:45:53 +09:00
Rafael Mendonça França
1b455e2e9d
Rails 6.2 is now Rails 7.0
We have big plans for the next version of Rails and that
require big versions.
2021-02-04 16:47:16 +00:00
Rafael Mendonça França
6487836af8
Rails 7 requires Ruby 2.7 and prefer Ruby 3+
The code cleanup is comming in later commits but this
already remove support to Ruby < 2.7.
2021-02-04 16:34:53 +00:00
Rafael França
da418dc250
Merge pull request #41217 from Vin0uz/active-model-errors-add-doc
[ci-skip] Adding options example in ActiveModel::Errors doc
2021-02-03 16:18:26 -05:00
Kevin Vinhas
da0e869bd8 Adding options example in ActiveModel::Errors doc
Place errors with variable example under the `type is a symbol` block
Closes #41124
2021-02-03 21:55:10 +01:00
Rafael Mendonça França
9ea15f1927
Improve performance of time type cast for ISO dates
Before this patch we were appeding a new date in front of
an already existing date what was making the fast path of the
type cast to not trigger.

The following benchmark was used to drive this implementation:

https://gist.github.com/rafaelfranca/0f71c00613f924fcbcdb14b95e6f7661

Closes #41316.
2021-02-03 00:10:15 +00:00
Ryuta Kamizono
ddaceddeab
Merge pull request #41166 from kamipo/raise_unknown_type_error_on_definition_time
Raise unknown type error on the definition time
2021-01-20 18:24:40 +09:00
Rafael Mendonça França
077c66d5d6
Rename master to main in all code references 2021-01-19 20:46:33 +00:00
Ryuta Kamizono
26e1fe4938 Raise unknown type error on the definition time
If unknow type is given for attribute (`attribute :foo, :unknown`),
unknown type error isn't raised on the definition time but runtime.

It should be raised on the definition time.
2021-01-19 15:52:12 +09:00
Ryuta Kamizono
e889cc55b0 Make ActiveModel's attribute behavior follow ActiveRecord's 2021-01-18 07:54:36 +09:00
Ryuta Kamizono
63835772bb
Merge pull request #41019 from intrip/40902-fix-numericality-validator
Use round(scale) in  ActiveModel NumericalityValidator
2021-01-14 17:14:31 +09:00
Jacopo
6657e3428b Change Numericaly validator to use round
f72f743 introduces truncate(scale) in the Numericality validator.
This behaviour conflicts with AR decimal type conversion,
which uses round(scale) instead.

Changes the Numericality validator in order to use
round(scale) for consistency.
2021-01-13 22:37:01 +01:00
Ryuta Kamizono
4db95d8432 Restore the ability that update/destroy optimistic locking object without default
The ability has lost due to reverted #39321 in #41049.

We should allow updating with dirty locking value to work the documented
usage, but if casted value has no difference (i.e. regarded as no dirty),
identify the object by the original (uninitialized default) value.
2021-01-12 09:27:11 +09:00
Ryuta Kamizono
8c60a2169a Add round up assertions for decimal tests 2021-01-12 06:16:21 +09:00
Ryuta Kamizono
d24dc88dad
Revert "Fix update with dirty locking column to not match latest object accidentally" 2021-01-08 18:06:05 +09:00
Michal Papis
2486e887de
Add validate numericality in range 2021-01-05 22:56:58 +01:00
Ryuta Kamizono
2b0b5a75c0 Bump license years to 2021 [ci skip] 2021-01-01 12:21:20 +09:00
Rafael Mendonça França
4740a2a02c
Add changelog entry for #40961 2020-12-29 19:25:04 +00:00
Rafael França
67a1cb6c19
Merge pull request #40961 from luk4s/add_activemodel_naming_locale_arg
change ActiveModel::Name initialize arguments to hash and add locale
2020-12-29 14:20:00 -05:00
Lukáš Pokorný
d20caa6df1
add locale argument to ActiveModel::Name initialize 2020-12-29 10:39:35 +01:00
Ryuta Kamizono
6c4306be71 Move set_options_for_callback into Callbacks::ClassMethods
https://buildkite.com/rails/rails/builds/73736#864bc31e-384d-4e38-8165-0d3256ac3f3d/968-979
2020-12-29 16:20:13 +09:00
Rafael Mendonça França
b4cab6a854
Make sure the :if options of callbacks is not mutated
In some cases, the framework was mutating the :if option of callbacks.
Since #38323, those options are frozen, so the framework could raise
exception when trying to mutate those options if they were being resued
with method like `with_options`.
2020-12-29 03:56:54 +00:00
alpaca-tc
8034a439c7 Reuse the same ActiveModel::Type::Value 2020-12-23 18:09:50 +09:00
Rafael Mendonça França
b24ed15baa
Rename the method to match what it is doing 2020-12-09 17:10:22 +00:00
Ryuta Kamizono
720a60e68c Split the options checks from read_attribute_for_validation 2020-12-09 16:43:26 +09:00
Ryuta Kamizono
1dee4990cd allow_nil should work for casted value in NumericalityValidator
It is a regression for 4cc438a1df75e4c230f19cafe9258dbab969cd27.

`NumericalityValidator` basically takes the value before typecasting,
but `allow_nil` should work for the typecasted value for the
compatibility.

Fixes #40750.
2020-12-08 23:25:56 +09:00
Rafael Mendonça França
59f7f5889e
Start Rails 6.2 development 🎉 2020-12-03 01:35:29 +00:00
alpaca-tc
3d1cb9a1f3 Handle frozen conditions in validate
Co-authored-by: Rafael França <rafael@franca.dev>
2020-11-06 12:06:03 +09:00
alpaca-tc
e4e62fbdc6 ActiveModel::Model can be frozen again 2020-11-05 20:52:27 +09:00
Rafael Mendonça França
8389f9902c
Preparing for 6.1.0.rc1 release 2020-11-02 21:12:47 +00:00
Rafael Mendonça França
7eed607b12
Fix CHANGELOG [ci skip] 2020-11-02 18:16:45 +00:00
Rafael França
af91d9a1c5
Merge pull request #40434 from filipe-sabella/pass-in-base-in-validation-messages
Pass in base to Error.human_attribute_names
2020-11-02 13:15:53 -05:00
Rafael Mendonça França
e14e78bf44
Revert "Add test to make sure this method will not be removed again"
This reverts commit d93a5d385e5bc2392a1f47dc2885e353898b62e1.

Revert "Revert "Remove unused internal methods in ActiveModel::Attributes""

This reverts commit 2d7967204e7f7d5ba846b0a6ed51088c7a7db365.

Reason: read_attribute was added in 6.1 as a performance optimization
and it is not needed anymore and write_attribute only existed to make
possible to call something that is not `attribute=` with send. We don't
need those methods internally and since they were never part of the
public API we can remove them.
2020-10-30 01:54:16 +00:00
Rafael Mendonça França
d93a5d385e
Add test to make sure this method will not be removed again 2020-10-28 21:26:39 +00:00
Rafael Mendonça França
2d7967204e
Revert "Remove unused internal methods in ActiveModel::Attributes"
This reverts commit 13bd289b448fb0186b2e932f306704ce2efb2fb6.
2020-10-28 21:26:38 +00:00
Ryuta Kamizono
e47068c3f3 Use attribute_before_type_cast in the internal
`read_attribute_before_type_cast` has become bit slower since #40395.
2020-10-27 16:38:32 +09:00
Ryuta Kamizono
13bd289b44 Remove unused internal methods in ActiveModel::Attributes 2020-10-27 16:11:24 +09:00
Akira Matsuda
fbe7bcf1d6 These test model accessors are public methods 2020-10-27 12:13:32 +09:00
Akira Matsuda
98fda672a6 *_digest is defined as a public method 2020-10-27 12:13:32 +09:00
Ryuta Kamizono
92ff708476 Re-enable Layout/SpaceAroundOperators cop
We prefer space around operators, but `Layout/SpaceAroundOperators` cop
was temporarily disabled in #36943 since that cop changed to check
alignment strictly somehow.

In RuboCop 1.0.0, that is fixed by https://github.com/rubocop-hq/rubocop/pull/8906.

Related https://github.com/rails/rails/pull/38034#discussion_r359845661,
https://github.com/rails/rails/pull/39770#discussion_r448829561.
2020-10-23 16:12:15 +09:00
Filipe Sabella
ac677fb1e3 Update CHANGELOG 2020-10-22 16:50:33 -03:00
Filipe Sabella
bc7b730891 Pass in base to Error.human_attribute_names
There are validation cases in which the human_attribute_name depends on
other fields of the base class.

For instance, an Address model that depends on the selected country to
localize the attribute name to be shown in error messages. E.g. the
:address1 and :address2 attributes can be displayed as very different
strings depending on whether the address is in the US or in Japan.
2020-10-22 16:50:00 -03:00