Commit Graph

2608 Commits

Author SHA1 Message Date
Yasuo Honda
2ab10fac93 Configure config.active_support.cache_format_version = 7.1 for RailtieTest
This commit suppresses the `DEPRECATION WARNING: Support for `config.active_support.cache_format_version = 6.1`
has been deprecated and will be removed in Rails 7.2.` warning at `RailtieTest`

This commit sets `config.active_support.cache_format_version = 7.1` explicitly for `RailtieTest`
because https://github.com/rails/rails/pull/48598 deprecates `active_support.cache_format_version = 6.1` and still the default format_version is 6.1, I think this is intended.

4ac237de74/activesupport/lib/active_support/cache.rb (L55)

```
    @format_version = 6.1
```

\### Steps to reproduce
```
git clone https://github.com/rails/rails
cd rails/activemodel
bundle
bin/test test/cases/railtie_test.rb:21
```

\### Without this commit
```
$ bin/test test/cases/railtie_test.rb:21
Run options: --seed 36872

\# Running:

DEPRECATION WARNING: Support for `config.active_support.cache_format_version = 6.1` has been deprecated and will be removed in Rails 7.2.

Check the Rails upgrade guide at https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#new-activesupport-cache-serialization-format
for more information on how to upgrade.
 (called from block (3 levels) in run at /home/yahonda/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/minitest-5.19.0/lib/minitest/test.rb:94)
.

Finished in 0.321429s, 3.1111 runs/s, 3.1111 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
$
```

\### With this commit
```
$ bin/test test/cases/railtie_test.rb:21
Run options: --seed 65282

\# Running:

Finished in 0.006108s, 0.0000 runs/s, 0.0000 assertions/s.
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
$
```
2023-08-25 12:48:39 +09:00
Rafael Mendonça França
8b095c8647
Merge pull request #49021 from Shopify/update-undefine-attribute-methods-docs
[Docs] Update `undefine_attribute_methods` docs
2023-08-23 18:11:51 -04:00
Nikita Vasilevsky
5bc904d37d
[Docs] Update undefine_attribute_methods docs 2023-08-23 22:02:49 +00:00
Nikita Vasilevsky
37342a37fd
Allow redefining to_param delimiter using param_delimiter
This commit allows customizing the delimiter used by `to_param` when
`to_key` returns multiple value. This unblocks supporting more varieties
of composite primary key types in Active Record.
2023-08-23 20:49:22 +00:00
Nikita Vasilevsky
e50fce0fa9
Add new behavior of undefine_attribute_methods to CHANGELOG
In `1818beb3a3ea5fdb498095d4885f8a7e512f24ca` Rails changed the target
where alias attribute methods are defined. It lead to
`undefine_attribute_methods` to clean alias attribute methods along with
the attribute methods. It was an intended behavior change but it wasn't
properly documented and tested. This commit clarifies the new behavior
in the Active Model changelog along with covering the behavior with tests.
2023-08-23 14:41:10 +00:00
Rafael Mendonça França
ed5af00459
Merge pull request #48998 from Shopify/to_key-supports-composite-primary-key
Support composite identifiers in `to_key`
2023-08-22 13:53:19 -04:00
Nikita Vasilevsky
8a5cf4cf44
Support composite identifiers in to_key
This commit adds support for composite identifiers in `to_key`.
Rails 7.1 adds support for composite primary key which means that
composite primary key models' `#id` method returns an `Array` and
`to_key` needs to avoid double-wrapping the value.
2023-08-22 16:13:23 +00:00
Guillermo Iguaran
4ec3a986d5
Merge pull request #48959 from skipkayhil/hm-clean-filters-requires
Remove uneeded requires of core_ext/string/filters
2023-08-18 16:03:44 -07:00
Ian Candy
a88f47d012 Only define attribute methods for class
Because we're using a class_attribute to track all of the attribute_aliases,
each subclass was regenerating the parent class methods, causing some unexpected
deprecation messages.

Instead, we can use a class instance variable to track the attributes aliased locally
in that particular subclass. We then walk up the chain and re-define the attribute methods
if they haven't been defined yet.
2023-08-16 18:23:02 -04:00
Hartley McGuire
ff6e885d59
Remove uneeded requires of core_ext/string/filters
`actionpack/lib/action_dispatch/routing.rb`
- added: 013745151be062aa4d0fc1f2a008a7303fdb6e04
- removed: 93034ad7fea7e00562103a7cd0acfab19bbfadf9

`activejob/lib/active_job/log_subscriber.rb`
- added: b314ab555e0d85e6efb41be94fb5f3a157bb12fe
- removed: 5ab2034730feacfc2caee418f8c0b55191d27427

`activemodel/lib/active_model/errors.rb`
- added: cf7fac7e29bb2816412c949fdaed3d61a923eb23
- removed: 9de6457ab0767ebab7f2c8bc583420fda072e2bd

`activerecord/lib/active_record/core.rb`
- added: b3bfa361c503e107aff4dee5edf79bd7fd3d3725
- removed: e1066f450d1a99c9a0b4d786b202e2ca82a4c3b3

`activesupport/lib/active_support/core_ext/module/introspection.rb`
- added: 358ac36edf1695fcbec0aa21f126a3d8b83d4b5a
- removed: 167b4153cac0069a21e0bb9689cb16f34f6abbaa

`activesupport/lib/active_support/duration.rb`
- added: 75924c4517c8f87712d3f59c11f10152ed57b9d8
- removed: a91ea1d51048342d13fc73f9b09ce4cfd086bb34

`railties/lib/rails/commands/server/server_command.rb`
- added: f2173648938b418d120f5a68d8f3862d8ae9dace
- removed: 553b86fc751c751db504bcbe2d033eb2bb5b6a0b

`railties/lib/rails/command/base.rb`
- added: 6813edc7d926965e5644cd8befaf229a35b9d8ca
- removed: b617a561d865a65cfc140caa0e3c4af4350bfcef
2023-08-16 17:39:25 -04:00
Rafael Mendonça França
40c616c635
Make sure nested base errors are translatable
If the user defined a translation to a nested error on base we should
look it up in the same way we do for the other attributes.

If no translation is set, we fallback to the name of the association.

Fixes #48884.
2023-08-04 19:55:57 +00:00
Rafael Mendonça França
a2d576c303
Use remove instead of split and join 2023-08-02 19:53:58 +00:00
Rafael Mendonça França
6793dd9396
Fix CHANGELOG location 2023-08-02 19:53:57 +00:00
zzak
a788725e75
AM::Error.full_message should strip ":base" from the message 2023-08-02 19:53:55 +00:00
Nikita Vasilevsky
5dbc7b424e
Use call_args in the define_proxy_method namespace.
`define_proxy_call` accepts `call_args` as an argument which impacts
method body generation but it doesn't use `call_args` in the `namespace`
generation which leads to the same cached method to be reused even if
`call_args` differ.

It has never been an issue since `define_proxy_call` was only used to
generate Active Record attribute methods and we were always passing
the same `call_args` per method name.

However, since https://github.com/rails/rails/pull/48533 we are using
`define_proxy_call` to generate alias attribute methods where `call_args`
differ for the same method name which leads to the same cached method
being reused in wrong places.

This commit fixes the issue by making sure `call_args` are being
considered when generating the `namespace` for the method.
2023-07-28 19:28:55 +00:00
Jean Boussier
3ee73bff39
Merge pull request #48533 from Shopify/delay-alias-attribute-defition
Call proxy methods from `alias_attribute` generated methods
2023-07-21 14:08:30 +02:00
Ufuk Kayserilioglu
c2b195e1e3
Change load error messages to use Kernel#warn instead of $stderr.puts
When development tools try to load Rails components, they sometimes end up loading files that will error out since a dependency is missing. In these cases, the tooling can catch the error and change its behaviour.

However, since the warning is printed directly to `$stderr`, the tooling cannot catch and suppress it easily, which ends up causing noise in the output of the tool.

This change makes Rails print these warnings using `Kernel#warn` instead, which can be suppressed by the tooling.
2023-07-21 00:38:12 +03:00
Nikita Vasilevsky
1818beb3a3
Call proxy methods from alias_attribute generated methods
This commit changes bodies of methods generated by `alias_attribute`
along with generating these methods lazily.

Previously the body of the `alias_attribute :new_title, :title` was
`def new_title; title; end`. This commit changes it to
`def new_title; attribute("title"); end`.

This allows for `alias_attribute` to be used to alias attributes named
with a reserved names like `id`:
```ruby
  class Topic < ActiveRecord::Base
    self.primary_key = :title
    alias_attribute :id_value, :id
  end

  Topic.new.id_value # => 1
```
2023-07-17 22:22:28 +00:00
Jean Boussier
29205d7928 Eagerly cast serialized query attributes
Fix: https://github.com/rails/rails/issues/48652
Ref: https://github.com/rails/rails/pull/46048
Ref: https://github.com/rails/rails/issues/46044
Ref: https://github.com/rails/rails/pull/34303
Ref: https://github.com/rails/rails/pull/39160
Close: https://github.com/rails/rails/pull/48705

This deep_dup was introduced to prevent the value stored in the query
cache to be later mutated.

The problem is that `ActiveRecord::Base#dup` will return a copy
of the record but with the primary key set to nil. One could
argue that `#dup` shouldn't behave this way, but I think this ship
has sailed (or has it?).

My initial fix was to instead always call `type.cast` eagerly so that we'd dup
serialized types in a more correct way. However there is a test
that explictly ensure this doesn't happen: https://github.com/rails/rails/pull/39160

The reason isn't 100% clear to me, but if I get it correctly, it's to avoid
a potentially costly operation upfront.

So instead we only eagerly cast serialized attributes only, so protect against
future mutations.

Mutable types are still deep duped.
2023-07-13 11:44:33 +02:00
Lewis Buckley
08a79ce284
Add a load hook for ActiveModel::Model
ActiveRecord::Base has a dedicated ActiveSupport load hook. This adds an
additional hook for ActiveModel::Model, so that when ActiveModel is
being used without ActiveRecord, it can still be modified.
2023-07-09 13:08:34 +01:00
Guillermo Iguaran
b790387597
Merge pull request #48616 from p8/activerecord/document-dirty
Document `ActiveRecord::Dirty` module in the API docs
2023-06-30 13:07:13 -07:00
John Hawthorn
e8e8517f39
Merge pull request #48614 from jhawthorn/faster_to_json
Improve performance of ActiveSupport::JSON.encode
2023-06-30 12:10:54 -07:00
Petrik
469dc61706 Fix some formatting in ActiveModel::Dirty docs 2023-06-30 21:05:01 +02:00
Petrik
4b3920ac9e Document ActiveRecord::AttributeMethods::Dirty module in the API docs
Currently it is a bit unclear which dirty methods can be called on
Active Record models. You have to know that methods from ActiveModel::Dirty
are included.

It also unclear if methods can be invoked in the form of
`saved_change_to_name?` unless you read the documentation of the
`saved_change_to_attribute?` method.

By adding an introduction to the module we can show which methods are
defined specifically for Active Record, and how to call them, very
similar to the ActiveModel::Dirty introduction.
Linking to ActiveModel::Dirty makes it's also easier to find methods
defined there.
2023-06-30 11:02:16 +02:00
John Hawthorn
66db67436d Avoid extra pass on AS::JSON.dump with no options
JSONGemEncoder.encode previously would always perform two passes. First
it would call `.as_json(options)`, but then would perform a second pass
"jsonify" to recursively call `.as_json` (this time without options)
until the data converges into a "JSON-ready" representation.

When options are not given, the second pass should be equivalent to the
first, so we can detect that, and only perform the "jsonify" step.

The only user-visible effect of this should be that we will pass no
options to `as_json` instead of an empty Hash, but implementations of
`as_json` should already be expected to accept that.
2023-06-29 15:37:57 -07:00
Petrik
ee58c8e6be Document generated dirty attribute methods [ci-skip]
Active Model generates methods for each attribute to handle dirty
changes. As these methods are generated they can't be found when
searching for them in the API documentation.

We can use the `:method:` directive to document them in the form of
`*_will_change`, `*_previously_was`, etc.
This notation is already used for documenting ActiveModel::Dirty.

An alternative could be documenting the methods as:
`[attribute_name]_will_change`, `[attribute_name]_previously_was`, etc...
But that adds more noise to the method.
2023-06-29 22:19:12 +02:00
zzak
dd89f600f7
🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
Rafael Mendonça França
3fc95e163d
Merge pull request #48065 from nvasilevski/disallow-assertionless-tests-in-active-model
Disallow assertionless tests in Active Model
2023-06-09 17:59:56 -04:00
Matthew Draper
308dd9c504 Ensure binary-destined values have binary encoding during type cast 2023-05-29 18:21:20 +09:30
zzak
722d4f6582
Unlink Rails constant from READMEs 2023-05-29 15:14:44 +09:00
zzak
d7d24b02c1
Fix linking on ActiveModel::API 2023-05-29 12:40:48 +09:00
zzak
8674d53cd0
Fix heading for StrictValidationFailed 2023-05-27 06:56:26 +09:00
zzak
38bef29064
Replace all occurrences of '<tt>(\w+::\w+::\w+)</tt>' with '+$1+'
E.g.:

* <tt>Rails::Command::NotesCommand</tt> -> +Rails::Command::NotesCommand+

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
2023-05-25 06:56:17 +09:00
zzak
e3c73fd183
Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+'
E.g.:

* <tt>ActiveRecord::Base</tt> -> +ActiveRecord::Base+

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2023-05-25 06:52:32 +09:00
Jean Boussier
108617eb74 Fix change_in_place? for binary serialized columns
Followup: https://github.com/rails/rails/pull/40383
Fix: https://github.com/rails/rails/issues/48255
Fix: https://github.com/rails/rails/pull/48262

If the serialized attribute is backed by a binary column, we must ensure
that both the `raw_old_value` and the `raw_new_value` are casted to
`Binary::Data`.

Additionally, `Binary::Data` must cast it's backing string in
`Encoding::BINARY` otherwise comparison of strings containing bytes
outside the ASCII range will fail.
2023-05-22 13:01:42 +02:00
zzak
0ec9e750ea
Merge pull request #48083 from skipkayhil/document-dirty-methods
Document ActiveModel::Dirty dispatch targets [ci skip]
2023-05-19 06:39:25 +09:00
Eileen M. Uchitelle
bc34f356e3
Merge pull request #48177 from eileencodes/unrevert-46444
Revert "Merge pull request #46444 from eileencodes/revert-forgetting-…
2023-05-09 13:29:28 -04:00
Jonathan Hefner
d6da86a476 Prevent non-anonymous modules from becoming frozen
In #48106, `Module#deep_dup` was changed to return the module itself
(not a copy) when the module is not anonymous.  However, that causes
non-anonymous modules to become frozen via `value.deep_dup.freeze` when
passed to `ActiveModel::Type::Helpers::Mutable#immutable_value`.  So,
for example, class attributes can no longer be set on the module.

To prevent such issues, this commit removes the `freeze` from
`immutable_value`.  `immutable_value` is only called by
`ActiveRecord::PredicateBuilder#build_bind_attribute`, which only cares
that other code cannot mutate the value, not that the value is actually
frozen.
2023-05-09 10:51:00 -05:00
eileencodes
38141681cc
Revert "Merge pull request #46444 from eileencodes/revert-forgetting-assignment-changes"
This reverts commit 586436d370126322a1470c10c1fde73cf8dc8875, reversing
changes made to 866e053732636e64685a72e22d76e7988d06a000.

This is an unrevert of https://github.com/rails/rails/pull/46282. I
should have reverted the revert sooner but it kept falling off my radar.

Closes #46446
2023-05-09 11:19:12 -04:00
Jean Boussier
b717a5a0d4 Revert "Merge pull request #47352 from basecamp/ar-freeze-cached"
This reverts commit 2c20f90ebae3fdd4d7e3351aeaffc4ad2c472069, reversing
changes made to 912096d4ce930b8e7e5d91e0c86bae2091fda0e4.
2023-05-02 10:53:14 +02:00
Jorge Manrubia
f195e033ac Freeze casted values instead of original values in database queries
This deals with a problem introduced in #7743ab95b8e15581f432206245c691434a3993d1a751b9d451170956d59457a9R8
that was preventing query `Class` serialized attributes. Duplicating the original
`Class` argument generates an anonymous class that can't be serialized as YAML.

This change makes query attributes hasheable based on their frozen casted values
to prevent the problem.

This solution is based on an idea by @matthewd from https://github.com/rails/rails/issues/47338#issuecomment-1424402777.
2023-05-02 10:09:20 +02:00
Hartley McGuire
7c1a5639c4
Document ActiveModel::Dirty dispatch targets
As part of my Railsconf talk I mentioned that attribute_changed? was an
undocumented method. Sage was surprised by this, and suggested that we
should probably be documenting this, since *_changed? methods are part
of the public API of Active Model.

I discussed this further with Rafael at Railsconf and we believe that in
addition to attribute_changed?, we should probably be documenting the
other dispatch targets in ActiveModel::Dirty as well.

Co-authored-by: Sage Griffin <sage@sagetheprogrammer.com>
2023-04-27 14:45:05 -04:00
Nikita Vasilevsky
2342f39845
Disallow assertionless tests in Active Model
This commit adds an `after_teardown` logic to the `activemodel` test
suite which ensures that performed test incremented the `assertions`
counter at least once. Otherwise it raises an `AssertionlessTest` error.

This leads to a requirement for tests to be verbose about assertions
even if technically it may not be needed. For example a test like:
```ruby
def test_submitting_a_review_doesnt_raise
  review.submit!
end
```

will have to at least become

```ruby
def test_submitting_a_review_doesnt_raise
  assert_nothing_raised { review.submit! }
end
```

or preferably it should perform a semantically meaningful assertion
that will imply not exception being raised, for example:

```ruby
def test_submitting_a_review_doesnt_raise
  review.submit!

  assert_not_nil review.submitted_at
end
```

Overall while the requirement is being defensive it improves
readability of the tests along with ensuring that we will never
end up having tests that test nothing.
2023-04-26 14:51:32 +00:00
Petrik de Heus
f2327e8df1
Merge pull request #48024 from p8/docs/headers-active-model-types
Fix ActiveModel type headings [ci-skip]
2023-04-22 12:09:47 +02:00
Petrik de Heus
0d02e5d4c3
Merge pull request #48022 from p8/docs/fix-header-level
Fix header level for ActiveModel::Naming and DelegatedType
2023-04-22 12:08:52 +02:00
Petrik
5a3daa626d Fix ActiveModel type headings [ci-skip] 2023-04-22 12:02:41 +02:00
Petrik
17b3e05133 Fix header level for ActiveModel::Naming and DelegatedType
A class should have a h1 instead of a h2.
2023-04-22 11:25:27 +02:00
Petrik
3042111944 Add some missing headers to Active Model Type docs [ci-skip] 2023-04-22 11:22:16 +02:00
Christopher Lake
42a4fcecf2
Update CHANGELOG example
CHANGELOG was not updated with subsequent changes to the PR #47708
2023-04-21 15:35:53 -07:00
Guillermo Iguaran
a60785cc27 Update password validation and error messages
- Simplify password validation to only check byte size for BCrypt limit (72 bytes)
- Replace specific error messages with a single "is too long" message
- Update test cases to reflect new error message

Co-authored-by: ChatGPT
2023-04-19 15:57:51 -07:00