forked from magnusvk/counter_culture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use
with_connection
where possible
rails/rails#51349 - this allows rails 7.2 applications to raise an error or emit a deprecation warning whenever `ActiveRecord::Base.connection` is used. this makes sure that in rails 7.1 and after we use the new, preferred method.
- Loading branch information
1 parent
e87ab36
commit 0a8f498
Showing
3 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module CounterCulture | ||
class WithConnection | ||
def initialize(recipient) | ||
@recipient = recipient | ||
end | ||
|
||
attr_reader :recipient | ||
|
||
def call | ||
if rails_7_2_or_greater? | ||
recipient.with_connection do |connection| | ||
yield connection | ||
end | ||
elsif rails_7_1? | ||
recipient.connection_pool.with_connection do |connection| | ||
yield connection | ||
end | ||
else | ||
yield recipient.connection | ||
end | ||
end | ||
|
||
private | ||
|
||
def rails_7_1? | ||
Gem::Requirement.new('~> 7.1.0').satisfied_by?(Gem::Version.new(Rails.version)) | ||
end | ||
|
||
def rails_7_2_or_greater? | ||
Gem::Requirement.new('>= 7.2.0').satisfied_by?(Gem::Version.new(Rails.version)) | ||
end | ||
end | ||
end |