Commit ba07c9f7 authored by Douwe Maan's avatar Douwe Maan

Improve fix.

parent b77e1ae6
module AttrEncrypted module AttrEncrypted
module Adapters module Adapters
module ActiveRecord module ActiveRecord
protected def attribute_instance_methods_as_symbols_with_no_db_connection
if connection_established?
def attribute_instance_methods_as_symbols # Call version from AttrEncrypted::Adapters::ActiveRecord
# We add accessor methods of the db columns to the list of instance attribute_instance_methods_as_symbols_without_no_db_connection
# 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 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
end end
alias_method_chain :attribute_instance_methods_as_symbols, :no_db_connection
private
def connection_established? def connection_established?
begin begin
# use with_connection so the connection doesn't stay pinned to the thread. # Use with_connection so the connection doesn't stay pinned to the thread.
ActiveRecord::Base.connection_pool.with_connection { ActiveRecord::Base.connection_pool.with_connection { |con| con.active? }
ActiveRecord::Base.connection.active?
}
rescue Exception rescue Exception
false false
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment