diff --git a/CHANGELOG.md b/CHANGELOG.md index cc34c198..df3a2563 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 3.6.0 +* [#871](https://github.com/CanCanCommunity/cancancan/pull/871): Make `with_accessible_by_strategy` thread safe ([@NikosVlagoidis][]) * [#849](https://github.com/CanCanCommunity/cancancan/pull/849): Update tests matrix. ([@coorasse][]) * [#843](https://github.com/CanCanCommunity/cancancan/pull/843): Compress duplicate rules. ([@MrChoclate][]) * [#841](https://github.com/CanCanCommunity/cancancan/pull/841): New https://cancancan.dev website. ([@pandermatt][]) @@ -716,3 +717,4 @@ Please read the [guide on migrating from CanCanCan 2.x to 3.0](https://github.co [@MrChoclate]: https://github.com/MrChoclate [@pandermatt]: https://github.com/pandermatt [@kalsan]: https://github.com/kalsan +[@NikosVlagoidis]: https://github.com/NikosVlagoidis diff --git a/lib/cancan/config.rb b/lib/cancan/config.rb index 0fd8893f..f192cf44 100644 --- a/lib/cancan/config.rb +++ b/lib/cancan/config.rb @@ -48,9 +48,7 @@ def self.with_rules_compressor_enabled(value) # `distinct` is not reliable in some cases. See # https://github.com/CanCanCommunity/cancancan/pull/605 def self.accessible_by_strategy - return @accessible_by_strategy if @accessible_by_strategy - - @accessible_by_strategy = default_accessible_by_strategy + Thread.current[:can_can_accessible_by_strategy] ||= default_accessible_by_strategy end def self.default_accessible_by_strategy @@ -71,20 +69,18 @@ def self.accessible_by_strategy=(value) raise ArgumentError, 'accessible_by_strategy = :subquery requires ActiveRecord 5 or newer' end - @accessible_by_strategy = value + Thread.current[:can_can_accessible_by_strategy] = value end def self.with_accessible_by_strategy(value) return yield if value == accessible_by_strategy - validate_accessible_by_strategy!(value) - begin strategy_was = accessible_by_strategy - @accessible_by_strategy = value + self.accessible_by_strategy = value yield ensure - @accessible_by_strategy = strategy_was + self.accessible_by_strategy = strategy_was end end