Commit Graph

1959 Commits

Author SHA1 Message Date
Rafael Mendonça França 37fd0e7fe4
Development of Rails 8.0 starts now
🎉
2024-05-13 16:45:20 +00:00
Nick Dower 22bc976576 Tiny update to callbacks docs [ci skip]
The following was added to the `ActiveJob::Callbacks`,
`ActiveModel::Callbacks` and `AbstractController:Callbacks` docs
in #29072:

> NOTE: Calling the same callback multiple times will overwrite
> previous callback definitions.

The comment refers to "calling" callbacks but seems to be about defining
callbacks, as mentioned in the PR description.

In the ActiveJob and AbstractController docs, I believe this will be
misinterpreted as referring to setting callbacks, which, as far as I can
tell, does not have such a restriction.

In the ActiveModel docs, I believe it would be slightly clearer to
replace "calling" with "defining".
2024-02-02 12:11:30 +01:00
Akira Matsuda d10d5aedce
TZ offset minute has to be negated in the Western Hemisphere
When the TZ in the given string contains minus offset, both hour and minute
value has to be negated, but the current code negates hour only.
Hence, for instance in Newfoundland Time Zone (UTC−03:30), it used to return
1 hour advanced value.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-01-17 17:25:37 +09:00
Jean Boussier 2cd568a5da Handle alternative base classes in `define_attribute_methods`
Ref: d429bfb3b6 (r136670440)
2024-01-15 17:20:56 +01:00
Petrik de Heus 8944d804ec
Add examples to #slice and #values_at documentation [ci-skip] (#50679) 2024-01-12 11:58:23 +01:00
Jean Boussier c0b5052d92
Merge pull request #50609 from ricardotk002/use-array-intersect
Replace usage of `Array#?` with `Array#intersect?` for efficiency
2024-01-07 21:15:56 +01:00
Jonathan Hefner 29456080c7 Ensure type_for_attribute method docs are visible [ci-skip]
`ActiveModel::AttributeRegistration` is marked `:nodoc:`, so
`type_for_attribute` must be documented under `ActiveModel::Attributes`,
which includes `ActiveModel::AttributeRegistration`.

Also, RDoc does not correctly interpret `:method:` docs when they are
immediately followed by a visibility keyword.  Therefore, this commit
adds delimiter comments before the keywords, similar to
4726b1ab47.
2024-01-06 18:07:26 -06:00
Ricardo Díaz de154095ed Replace usage of `Array#?` with `Array#intersect?` for efficiency
`Array#intersect?` was introduced in Ruby 3.1.0 and it's more efficient and
useful when the result of the intersection is not needed as the
following benchmarks show:

```
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", path: "./"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"
  gem "benchmark-ips"
end

require "active_support"

SCENARIOS = [
  [(1..100).to_a, (90..200).to_a],    # Case 1
  [("a".."m").to_a, ("j".."z").to_a], # Case 2
  [(1..100).to_a, (101..200).to_a],   # Case 3
]

SCENARIOS.each_with_index do |values, n|
  puts
  puts " Case #{n + 1} ".center(80, "=")
  puts
  Benchmark.ips do |x|
    x.report("Array#?") { !(values[0] & values[1]).empty? }
    x.report("Array#intersect?")      { values[0].intersect?(values[1]) }
    x.compare!
  end
end
```

Results:

```
==================================== Case 1 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?    34.221k i/100ms
    Array#intersect?    62.035k i/100ms
Calculating -------------------------------------
             Array#?    343.119k (± 1.1%) i/s -      1.745M in   5.087078s
    Array#intersect?    615.394k (± 1.1%) i/s -      3.102M in   5.040838s

Comparison:
    Array#intersect?:   615393.7 i/s
             Array#?:   343119.4 i/s - 1.79x  slower

==================================== Case 2 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?   103.256k i/100ms
    Array#intersect?   185.104k i/100ms
Calculating -------------------------------------
             Array#?      1.039M (± 1.3%) i/s -      5.266M in   5.066847s
    Array#intersect?      1.873M (± 1.6%) i/s -      9.440M in   5.041740s

Comparison:
    Array#intersect?:  1872932.7 i/s
             Array#?:  1039482.4 i/s - 1.80x  slower

==================================== Case 3 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?    37.070k i/100ms
    Array#intersect?    41.438k i/100ms
Calculating -------------------------------------
             Array#?    370.902k (± 0.8%) i/s -      1.891M in   5.097584s
    Array#intersect?    409.902k (± 1.0%) i/s -      2.072M in   5.055185s

Comparison:
    Array#intersect?:   409901.8 i/s
             Array#?:   370902.3 i/s - 1.11x  slower
```
2024-01-05 15:15:30 -05:00
Jean Boussier 27140247c2 Cleanup `defined?` usage
Now that we dropped support for Ruby 2.7, we no longer
need to check if variables are defined before accessing them
to avoid the undefined variable warning.
2024-01-05 15:05:35 +01:00
Jean Boussier ef65e5fb32 Cleanup usage of ruby2_keywords
Now that we no longer support Ruby 2.7, many `ruby2_keyword` calls
can be eliminated.

The ones that are left could be eliminated but would end up substantially
slower or more compliacated so I left them for now.
2024-01-05 14:40:18 +01:00
Jonathan Hefner b0048c787a Capitalize "Rails" [ci-skip] 2023-12-19 13:16:47 -06:00
Orhan Toy d86b098a8a Document delegated methods in ActiveModel::Errors
Fixes #50187
2023-11-29 06:38:14 +01:00
Daniel Bernier 417b84a0d1
Fix Dirty#*_was documentation
The docs illustrated `*_change`, not `*_was`, leaving the reader to have to guess.
2023-11-22 14:24:57 -05:00
Rafael Mendonça França 139e806e5c
Remove duplication between alias_attribute_method_definition and define_proxy_call 2023-11-22 17:27:05 +00:00
Rafael Mendonça França 8ec3843cd8
Make duplication explicit between alias_attribute_method_definition and define_proxy_call 2023-11-22 17:20:46 +00:00
Rafael Mendonça França 01492e329f
Extract common logic to build mangled method names to a method 2023-11-22 17:13:28 +00:00
Rafael Mendonça França c6e12729fb
Revert "Port `BeforeTypeCast` to Active Model" 2023-11-08 14:44:05 -05:00
MaicolBen 19869e765e Don't mark Float::INFINITY as changed when reassigning it
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-06 08:33:39 -06:00
Jonathan Hefner cfb72c9d37 Port BeforeTypeCast to Active Model
This commit ports `ActiveRecord::AttributeMethods::BeforeTypeCast` to
`ActiveModel::BeforeTypeCast` and includes it in `ActiveModel::Attributes`.
Thus, classes that include `ActiveModel::Attributes` will now
automatically define methods such as `*_before_type_cast`, just as
Active Record models do.

The `ActiveRecord::AttributeMethods::BeforeTypeCast` module is kept for
backward compatibility, but it now merely includes
`ActiveModel::BeforeTypeCast`.

Co-authored-by: Petrik <petrik@deheus.net>
2023-11-03 21:58:01 -05:00
Jonathan Hefner 83f543b876 Port type_for_attribute to Active Model
This moves `type_for_attribute` from `ActiveRecord::ModelSchema::ClassMethods`
to `ActiveModel::AttributeRegistration::ClassMethods`, where
`attribute_types` is also defined.

Co-authored-by: Petrik <petrik@deheus.net>
2023-11-03 17:24:25 -05:00
Jonathan Hefner ed2839dd9e
Merge pull request #49836 from skipkayhil/hm-assign-attribute-respond-to
Remove respond_to? in assign_attribute happy path
2023-10-29 15:43:04 -05:00
Hartley McGuire 7a9a537e9d
Remove respond_to? in assign_attribute happy path
Kernel#respond_to? is generally slow, and so it should be avoided
especially in hot loops. In this case, assign_attributes ends up calling
respond_to? for each attribute being assigned. The purpose of the check
here is to raise UnknownAttributeError when the attribute setter method
doesn't exist.

This commit moves the respond_to? inside a rescue. For a single
attribute being assigned, the performance is about the same. However,
the performance is ~10% better for 10 attributes being assigned and
~30% better for 100 attributes. There is a tradeoff here since using
rescue for control flow is generally not good for performance, however
in this case the ~10% decrease in performance only applies to the
exceptional case when attempting to assign an unknown attribute.

This also partially reverts a225d4bec5.
The commit message doesn't have much info so it's unclear to me why this
was changed.

Benchmark:

```
require "active_record"
require "benchmark/ips"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    100.times do |i|
      t.text :"body_#{i}"
    end
  end
end

class Post < ActiveRecord::Base
end

one_attribute = { "body_1" => "1" }
ten_attributes = {}
hundered_attributes = {}

invalid_attribute = { "body__1" => "1" }

100.times do |i|
  ten_attributes["body_#{i}"] = i.to_s if i < 11
  hundered_attributes["body_#{i}"] = i.to_s
end

Benchmark.ips do |x|
  x.report("1 attribute") { Post.new(one_attribute) }
  x.report("10 attribute") { Post.new(ten_attributes) }
  x.report("100 attribute") { Post.new(hundered_attributes) }
  x.report("invalid attribute") do
    Post.new(invalid_attribute)
  rescue ActiveModel::UnknownAttributeError
  end
end
```

Before:

```
Warming up --------------------------------------
         1 attribute     1.090k i/100ms
        10 attribute   805.000  i/100ms
       100 attribute   251.000  i/100ms
   invalid attribute   966.000  i/100ms
Calculating -------------------------------------
         1 attribute     10.882k (± 1.3%) i/s -     54.500k in   5.009085s
        10 attribute      8.070k (± 1.2%) i/s -     41.055k in   5.088110s
       100 attribute      2.548k (± 1.6%) i/s -     12.801k in   5.026321s
   invalid attribute      9.687k (± 2.5%) i/s -     49.266k in   5.089246s
```

After:

```
Warming up --------------------------------------
         1 attribute     1.098k i/100ms
        10 attribute   884.000  i/100ms
       100 attribute   341.000  i/100ms
   invalid attribute   868.000  i/100ms
Calculating -------------------------------------
         1 attribute     11.004k (± 1.1%) i/s -     55.998k in   5.089635s
        10 attribute      8.817k (± 1.8%) i/s -     44.200k in   5.014699s
       100 attribute      3.410k (± 0.4%) i/s -     17.391k in   5.100166s
   invalid attribute      8.695k (± 0.9%) i/s -     44.268k in   5.091846s
```
2023-10-29 14:16:03 -04:00
Jonathan Hefner 34be48ec35 Do not rely on dup in forgetting_assignment optimization
Follow-up to #46282.

The purpose of the optimization from #46282 is to avoid unnecessary
`deserialize` / `cast` / `serialize` calls associated with invoking
`value_for_database` on an attribute that has not changed.  `dup`ing the
attribute accomplishes that, but `dup`ing the attribute's value is not
appropriate for some value types.  For example, a value of the type
`ActiveModel::Type::ImmutableString` requires `clone` instead of `dup`,
and a value of the type `ActiveRecord::Type::Json` likely requires
`deep_dup`.  In some cases the only appropriate method may be
`deserialize(serialize(value))`, such as when a serializer for the type
`ActiveRecord::Type::Serialized` deserializes `ActiveRecord::Base`
instances.  (In that case, `dup`ing the value would clear its `id`, and
`clone`ing the value would only produce a shallow copy.)  However,
`deserialize(serialize(value))` is expensive and would defeat the
purpose of the optimization.

Instead of `dup`ing the attribute, this commit changes the optimization
to use `with_value_from_database(value_before_type_cast)`, which
parallels `with_value_from_database(value_for_database)` in the base
implementation.  This drops the (cast) value entirely, causing a fresh
copy to be deserialized if the attribute is subsequently read.  In cases
where the attribute is _not_ subsequently read, this will actually be
more efficient since no extra work is performed.  And in cases where the
attribute _is_ subsequently read, it will still be more efficient than
`deserialize(serialize(value))`.

Fixes #49809.
2023-10-28 16:26:48 -05:00
Jean Boussier c28e4f2434 Use double quotes more consistenly in doc and error messages
For better or worse, the Rails guide settled on double quotes
and a large part of the community also use rubocop which enforce
them by default.

So we might as well try to follow that style when providing code
snippets in the documentation or error messages.

Fix: https://github.com/rails/rails/issues/49822

I certainly didn't get them all, but consistency should be significantly
improved.
2023-10-28 11:38:49 +02:00
Dmitry Pogrebnoy 3ed02297a7 Make `==(other)` method of AttributeSet safe
The `==(other)` should be able to work with any instances without exception. To do so, we check if the other instance is an AttributeSet.

Fixes#49670
2023-10-19 09:08:52 +02:00
Rafael Mendonça França 3e810adef5
Avoid __method__
Just be explicit about what we are trying to do here.
2023-10-16 13:49:20 +00:00
Jonathan Hefner e0a55b038f Use ActiveModel::AttributeRegistration in AR
This refactors the `ActiveRecord::Attributes` module to use
`ActiveModel::AttributeRegistration`.  This also replaces the block form
of the `attribute` method (which was support by only Active Record) with
`decorate_attributes` (which is supported by both Active Model and
Active Record).  The block form of the `attribute` method was a private
API, so no deprecation is necessary.
2023-10-15 16:08:35 -05:00
Jonathan Hefner 31bb0b4aee Support Active Model attribute type decoration
This adds a `decorate_attributes` method to Active Model to support
attribute type decoration.  `decorate_attributes` is a private,
low-level API that is intended to be wrapped by high-level APIs like
`ActiveRecord::Base::normalizes` and `ActiveRecord::Base::enum`.
2023-10-15 15:53:16 -05:00
Jonathan Hefner 007ea58ec9 Fix typo in method names [ci-skip] 2023-10-07 12:02:22 -05:00
Rafael Mendonça França fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Rafael Mendonça França acfa045405
Revert typography change in user facing errors
This change would force a lot of existing applications and libraries
to update their tests.

We included it in the beta to collect feedback from the community and
we had some comments about how negative this change would be.

Developers that care about the typography of their error messages
can easily change it in their applications using the translation
files, so there is no need to inflict pain in the upgrade process
by changing the default in the framework.

Revert "Merge PR #45463"

This reverts commit 9f60cd8dc7, reversing
changes made to 35d574dbfd.
2023-09-26 21:45:03 +00:00
Rafael Mendonça França 699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00: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
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
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
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
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: 013745151b
- removed: 93034ad7fe

`activejob/lib/active_job/log_subscriber.rb`
- added: b314ab555e
- removed: 5ab2034730

`activemodel/lib/active_model/errors.rb`
- added: cf7fac7e29
- removed: 9de6457ab0

`activerecord/lib/active_record/core.rb`
- added: b3bfa361c5
- removed: e1066f450d

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

`activesupport/lib/active_support/duration.rb`
- added: 75924c4517
- removed: a91ea1d510

`railties/lib/rails/commands/server/server_command.rb`
- added: f217364893
- removed: 553b86fc75

`railties/lib/rails/command/base.rb`
- added: 6813edc7d9
- removed: b617a561d8
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
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