Commit Graph

25 Commits

Author SHA1 Message Date
Alex Ghiculescu
1284df5286 ActiveSupport::CurrentAttributes: raise if a restricted attribute name is used.
Attributes such as `set` and `reset` should not be used as they clash with the  `CurrentAttributes` public API. This PR raises an `ArgumentError` if a restricted attribute name is used.
2023-01-15 17:07:11 -07:00
Alex Ghiculescu
be155f135a Support using a method name to define the ActiveSupport::CurrentAttributes.resets callback
Previously you could only use a block, which was inconsistent with other callback APIs in Rails. Now, you can use a block or a method name. The block API is still recommended in the docs.

```ruby
class Current < ActiveSupport::CurrentAttributes
  resets { Time.zone = nil }
  resets :clear_time_zone
end
```

This also works for `before_reset`.

```ruby
class Current < ActiveSupport::CurrentAttributes
  before_reset { Time.zone = nil }
  before_reset :clear_time_zone
end
```
2022-10-05 11:05:58 -05:00
Jean Boussier
540d2f41f6 Introduce ActiveSupport::IsolatedExecutionState for internal use
Many places in Active Support and Rails in general use `Thread.current#[]`
to store "request (or job) local data". This often cause problems with
`Enumerator` because it runs in a different fiber.

On the other hand, some places migrated to `Thread#thread_variable_get`
which cause issues with fiber based servers (`falcon`).

Based on this, I believe the isolation level should be an application
configuration.

For backward compatibility it could ship with `:fiber` isolation as a default
but longer term :thread would make more sense as it would work fine for
all deployment targets except falcon.

Ref: https://github.com/rails/rails/pull/38905
Ref: https://github.com/rails/rails/pull/39428
Ref: https://github.com/rails/rails/pull/34495
(and possibly many others)
2021-11-18 15:55:15 +01:00
Jean Boussier
6bad959565 Extract ActiveSupport::ExecutionContext out of ActiveRecord::QueryLogs
I'm working on a standardized error reporting interface
(https://github.com/rails/rails/issues/43472) and it has the same need
for a `context` than Active Record's query logs.

Just like query logs, when reporting an error you want to know what the
current controller or job is, etc.

So by extracting it we can allow both API to use the same store.
2021-11-10 09:36:02 +01:00
Jean Boussier
7b5096947e Call Executor#wrap around each test
It's `Rails.application.executor.wrap` that is responsible for
clearing request/job local state such as `CurrentAttributes`.

Instead of including an ad hoc helper to clear `CurrentAttributes` it's
better to run the executor so we properly clear other states as well.

However it means all executor hooks now need to be re-entrant.
2021-10-28 15:18:29 +02:00
Jean Boussier
d2734111dd
Revert "Call Executor#wrap around each test" 2021-10-28 00:35:07 +02:00
Jean Boussier
265c1e9f2d Call Executor#wrap around each test
It's `Rails.application.executor.wrap` that is responsible for
clearing request/job local state such as `CurrentAttributes`.

Instead of including an ad hoc helper to clear `CurrentAttributes` it's
better to run the executor so we properly clear other states as well.
2021-10-27 12:46:22 +02:00
Étienne Barrié
8195cd5f99 Allow using thread variables for CurrentAttributes instances 2021-07-21 16:53:32 +02:00
John Bampton
54e526e473 chore: fix grammar, spelling and minor whitespace fix 2021-04-13 21:35:50 +10:00
Ryuta Kamizono
7b9cfde741 Fix method_missing delegation to not expand positional hash argument
Follow up to #41518, and similar to 3a85ced1a03c3e4fa81e316d06a65a75c760cf8c.
2021-03-02 22:29:29 +09:00
Marcin Kolodziej
9d6b2b38fd Fix proxying keyword arguments for ActiveSupport::CurrentAttributes.
Also fixes `respond_to?` not working for methods that have not yet been delegated.
2021-02-22 17:42:48 +01:00
Étienne Barrié
0400be279b Allow access to CurrentAttributes in test teardown 2020-11-09 15:15:42 -05:00
Kasper Timm Hansen
3b953da779
Catch Time.zone before TestHelper resets it to UTC
This is most easiest done by switching to before_setup, which fits since
we're also testing the ordering of the reset calls provided by the
TestHelper.
2020-06-02 01:31:14 +02:00
Kasper Timm Hansen
c962409ba6
Reset ActiveSupport::CurrentAttributes even where executor doesn't wrap
Currently there's a problem with ActiveSupport::CurrentAttributes where
they don't reset unless there's a controller or a job executing.

This is because we correctly hook into the controller/job executor to
reset them.

However, we were missing plain tests, so this is that.
2020-05-31 22:26:24 +02:00
Michael Grosser
203998c916
allow running each test with pure ruby path/to/test.rb
also:
 - makes test dependencies obvious
 - makes tests runnable from within subfolders
2019-12-18 08:49:19 -06:00
Akira Matsuda
af2129b4c7 Use try only when we're unsure if the receiver would respond_to the method 2019-08-01 17:58:00 +09:00
Rosa Gutierrez
2ce8455cc9 Support before_reset callback in CurrentAttributes
This is useful when we need to do some work associated to `Current.reset`
but that work depends on the values of the current attributes themselves.
This cannot be done in the supported `resets` callback because when the
block is executed, CurrentAttributes's instance has already been reset.

For symmetry, `after_reset` is defined as alias of `resets`.
2019-01-30 16:10:06 +01:00
Rafael Mendonça França
feb1ddae02 Merge remote-tracking branch 'origin/master' into unlock-minitest 2017-08-01 17:34:14 -04:00
Kasper Timm Hansen
aad42dce10 Merge branch 'master' into unlock-minitest 2017-07-15 21:17:27 +02:00
Koichi ITO
ac717d65a3 [Active Support] rubocop -a --only Layout/EmptyLineAfterMagicComment 2017-07-11 13:12:32 +09:00
Kir Shatrov
72950568dd Use frozen-string-literal in ActiveSupport 2017-07-09 15:08:29 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
yuuji.yaginuma
966715d528 Reset time zone to previous value
Since `CurrentAttributesTest` changed the global time zone, it is
necessary to restore the original value after changing the test.

The reproduction step:

```
./bin/test -w --seed 5549 test/current_attributes_test.rb test/core_ext/date_ext_test.rb
```
2017-05-30 07:58:00 +09:00
David Heinemeier Hansson
24a864437e ActiveSupport::CurrentAttributes provides a thread-isolated attributes singleton (#29180)
* Add ActiveSupport::CurrentAttributes to provide a thread-isolated attributes singleton

* Need to require first

* Move stubs into test namespace.

Thus they won't conflict with other Current and Person stubs.

* End of the line for you, whitespace!

* Support super in attribute methods.

Define instance level accessors in an included module such that
`super` in an overriden accessor works, akin to Active Model.

* Spare users the manual require.

Follow the example of concerns, autoload in the top level Active Support file.

* Add bidelegation support

* Rename #expose to #set. Simpler, clearer

* Automatically reset every instance.

Skips the need for users to actively embed something that resets
their CurrentAttributes instances.

* Fix test name; add tangible name value when blank.

* Try to ensure we run after a request as well.

* Delegate all missing methods to the instance

This allows regular `delegate` to serve, so we don't need bidelegate.

* Properly test resetting after execution cycle.

Also remove the stale puts debugging.

* Update documentation to match new autoreset
2017-05-26 20:00:27 +02:00