aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_handling.rb
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2018-12-11 11:13:35 -0800
committerJohn Hawthorn <john@hawthorn.email>2018-12-11 14:24:02 -0800
commitb67a3f5cac4d58c4fd55f1d41a203277aed94041 (patch)
tree4cf3c7bee8e05a02f1301e6dff2a54e69ec19ab1 /activerecord/lib/active_record/connection_handling.rb
parent574234402433065e08a407be7cbdc900e5e0f1a3 (diff)
downloadrails-b67a3f5cac4d58c4fd55f1d41a203277aed94041.tar.gz
rails-b67a3f5cac4d58c4fd55f1d41a203277aed94041.tar.bz2
rails-b67a3f5cac4d58c4fd55f1d41a203277aed94041.zip
Add AR::Base.connected_to?
This can be used to check the currently connected role. It's meant to mirror AR::Base.connected_to
Diffstat (limited to 'activerecord/lib/active_record/connection_handling.rb')
-rw-r--r--activerecord/lib/active_record/connection_handling.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb
index 3ce9aad5fc..558cdeccf2 100644
--- a/activerecord/lib/active_record/connection_handling.rb
+++ b/activerecord/lib/active_record/connection_handling.rb
@@ -130,6 +130,29 @@ module ActiveRecord
end
end
+ # Returns true if role is the current connected role.
+ #
+ # ActiveRecord::Base.connected_to(role: :writing) do
+ # ActiveRecord::Base.connected_to?(role: :writing) #=> true
+ # ActiveRecord::Base.connected_to?(role: :reading) #=> false
+ # end
+ def connected_to?(role:)
+ current_role == role.to_sym
+ end
+
+ # Returns the symbol representing the current connected role.
+ #
+ # ActiveRecord::Base.connected_to(role: :writing) do
+ # ActiveRecord::Base.current_role #=> :writing
+ # end
+ #
+ # ActiveRecord::Base.connected_to(role: :reading) do
+ # ActiveRecord::Base.current_role #=> :reading
+ # end
+ def current_role
+ connection_handlers.key(connection_handler)
+ end
+
def lookup_connection_handler(handler_key) # :nodoc:
connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
end