diff options
author | John Hawthorn <john@hawthorn.email> | 2018-12-11 11:13:35 -0800 |
---|---|---|
committer | John Hawthorn <john@hawthorn.email> | 2018-12-11 14:24:02 -0800 |
commit | b67a3f5cac4d58c4fd55f1d41a203277aed94041 (patch) | |
tree | 4cf3c7bee8e05a02f1301e6dff2a54e69ec19ab1 /activerecord/lib | |
parent | 574234402433065e08a407be7cbdc900e5e0f1a3 (diff) | |
download | rails-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')
-rw-r--r-- | activerecord/lib/active_record/connection_handling.rb | 23 |
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 |