Commit Graph

2618 Commits

Author SHA1 Message Date
Shouichi Kamiya
51ac8b9f6f Enable Minitest/LiteralAsActualArgument
There are assertions that expected/actual arguments are passed in the
reversed order by mistake. Enabling the LiteralAsActualArgument rule
prevents this mistake from happening.

The existing tests were auto-corrected by rubocop with a bit of
indentation adjustment.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-13 10:09:32 +09:00
Rafael Mendonça França
699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00:00
Jonathan Hefner
4a08111ec6
Merge pull request #49172 from akhilgkrishnan/ruby-code-block-intentation-fix
[ci-skip] Ruby code block indentation issue fix
2023-09-06 17:24:29 -05:00
Nikita Vasilevsky
0f5563bd40
Define alias attribute methods in define_attribute_methods
`undefine_attribute_methods` now removes alias attribute methods along
with attribute methods. This commit changes `define_attribute_methods` to
redefine methods back if any alias attributes were declared which provides
applications and libraries an option to bring the alias methods back
after using `undefine_attribute_methods`.
2023-09-06 20:06:39 +00:00
Akhil G Krishnan
61a9c1a92a Ruby code block indentation issue fix
[skip ci] indentation fix

[skip ci] review changes added

[skip ci] indentation fix
2023-09-07 01:14:48 +05:30
Rafael Mendonça França
ed873f1389
Merge pull request #49065 from RuhmUndAnsehen/fix-_to_partial_path-model_name
Fix ActiveModel::Conversion._to_partial_path not using a model's model_name.
2023-09-01 16:41:47 -04:00
Nikita Vasilevsky
0df4df4d15
Add CHANGELOG entries for ActiveModel::Conversion#to_key changes
`8a5cf4cf4415ae1cdad7feecfb27149c151b0b10` made changes to `to_key` in order
to support composite identifiers. This commit adds CHANGELOG entries for those.
2023-09-01 15:41:05 +00:00
Ian Candy
eae26caec3 Clarify deprecation warning for alias_attribute
We ran into a few cases at GitHub where we were using alias_attribute
incorrectly and the new behavior either didn't warn or raised an unclear
deprecation warning. This attempts to add clarity to the deprecation reason
when you try to alias something that isn't actually an attribute.

Previously, trying to alias a generated attribute method, such as `attribute_in_database`, would
still hit `define_proxy_call`, because we were only checking the owner of the target method.

In the future, we should probably raise if you try to use alias_attribute for a non-attribute.

Note that we don't raise the warning for abstract classes, because the attribute may be implemented
by a child class. We could potentially figure out a way to raise in these cases as well, but this
hopefully is good enough for now.

Finally, I also updated the way we're setting `local_alias_attributes` when `alias_attribute` is
first called. This was causing problems since we now use `alias_attribute` early in the
`load_schema!` call for some models: https://buildkite.com/rails/rails/builds/98910
2023-08-30 13:19:58 -04:00
Gannon McGibbon
a7cc807cb9 Fix to_param parameter generation for partial composite keys
Adds tests for url_for use with composite primary key models. Fixes bug
related to new CPK model to_param generation.
2023-08-29 14:04:03 -05:00
RuhmUndAnsehen
3a3951a3a8 Fix ActiveModel::Conversion._to_partial_path not using a model's model_name.
The current implementation of _to_partial_path composes the part of two bits, `collection' and `element'.
ActiveModel::Name also contains these fields, and they are derived the same way _to_partial_path does it.
However, _to_partial_path doesn't use the information in model_name, and solely relies on its own computations instead.
This works for all standard cases, but not necessarily for models that provide a non-standard model_name.

This commit fixes that and has _to_partial_path use model_name if the class responds to it.
2023-08-28 22:05:34 +02:00
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