From b77e1ae6f78461a1721150538158480d99bd899b Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 15 May 2015 14:34:22 +0200 Subject: [PATCH 1/3] Don't require DB conncetion in AttrEncrypted. --- .../attr_encrypted_no_db_connection.rb | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 config/initializers/attr_encrypted_no_db_connection.rb diff --git a/config/initializers/attr_encrypted_no_db_connection.rb b/config/initializers/attr_encrypted_no_db_connection.rb new file mode 100644 index 0000000000..72e257a013 --- /dev/null +++ b/config/initializers/attr_encrypted_no_db_connection.rb @@ -0,0 +1,29 @@ +module AttrEncrypted + module Adapters + module ActiveRecord + protected + + def attribute_instance_methods_as_symbols + # We add accessor methods of the db columns to the list of instance + # methods returned to let ActiveRecord define the accessor methods + # for the db columns + if connection_established? && table_exists? + columns_hash.keys.inject(super) {|instance_methods, column_name| instance_methods.concat [column_name.to_sym, :"#{column_name}="]} + else + super + end + end + + def connection_established? + begin + # use with_connection so the connection doesn't stay pinned to the thread. + ActiveRecord::Base.connection_pool.with_connection { + ActiveRecord::Base.connection.active? + } + rescue Exception + false + end + end + end + end +end From ba07c9f7f599cecac2c0840484f8bfc62d9e716b Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 15 May 2015 14:56:04 +0200 Subject: [PATCH 2/3] Improve fix. --- .../attr_encrypted_no_db_connection.rb | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/config/initializers/attr_encrypted_no_db_connection.rb b/config/initializers/attr_encrypted_no_db_connection.rb index 72e257a013..e270028f45 100644 --- a/config/initializers/attr_encrypted_no_db_connection.rb +++ b/config/initializers/attr_encrypted_no_db_connection.rb @@ -1,25 +1,24 @@ module AttrEncrypted module Adapters module ActiveRecord - protected - - def attribute_instance_methods_as_symbols - # We add accessor methods of the db columns to the list of instance - # methods returned to let ActiveRecord define the accessor methods - # for the db columns - if connection_established? && table_exists? - columns_hash.keys.inject(super) {|instance_methods, column_name| instance_methods.concat [column_name.to_sym, :"#{column_name}="]} + def attribute_instance_methods_as_symbols_with_no_db_connection + if connection_established? + # Call version from AttrEncrypted::Adapters::ActiveRecord + attribute_instance_methods_as_symbols_without_no_db_connection else - super + # Call version from AttrEncrypted (`super` with regards to AttrEncrypted::Adapters::ActiveRecord) + AttrEncrypted.instance_method(:attribute_instance_methods_as_symbols).bind(self).call end end + alias_method_chain :attribute_instance_methods_as_symbols, :no_db_connection + + private + def connection_established? begin - # use with_connection so the connection doesn't stay pinned to the thread. - ActiveRecord::Base.connection_pool.with_connection { - ActiveRecord::Base.connection.active? - } + # Use with_connection so the connection doesn't stay pinned to the thread. + ActiveRecord::Base.connection_pool.with_connection { |con| con.active? } rescue Exception false end From 61ceb45088d9bfb04890866718f87686bfb5f3c1 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 15 May 2015 15:32:49 +0200 Subject: [PATCH 3/3] Fix. --- .../attr_encrypted_no_db_connection.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/config/initializers/attr_encrypted_no_db_connection.rb b/config/initializers/attr_encrypted_no_db_connection.rb index e270028f45..c668864089 100644 --- a/config/initializers/attr_encrypted_no_db_connection.rb +++ b/config/initializers/attr_encrypted_no_db_connection.rb @@ -2,27 +2,19 @@ module AttrEncrypted module Adapters module ActiveRecord def attribute_instance_methods_as_symbols_with_no_db_connection - if connection_established? + # Use with_connection so the connection doesn't stay pinned to the thread. + connected = ::ActiveRecord::Base.connection_pool.with_connection(&:active?) rescue false + + if connected # Call version from AttrEncrypted::Adapters::ActiveRecord attribute_instance_methods_as_symbols_without_no_db_connection else - # Call version from AttrEncrypted (`super` with regards to AttrEncrypted::Adapters::ActiveRecord) + # Call version from AttrEncrypted, i.e., `super` with regards to AttrEncrypted::Adapters::ActiveRecord AttrEncrypted.instance_method(:attribute_instance_methods_as_symbols).bind(self).call end end alias_method_chain :attribute_instance_methods_as_symbols, :no_db_connection - - private - - def connection_established? - begin - # Use with_connection so the connection doesn't stay pinned to the thread. - ActiveRecord::Base.connection_pool.with_connection { |con| con.active? } - rescue Exception - false - end - end end end end