lib/niceql.rb in niceql-0.1.25 vs lib/niceql.rb in niceql-0.2.0

- old
+ new

@@ -47,11 +47,10 @@ def self.config Niceql.config end - def self.prettify_err(err) prettify_pg_err( err.to_s ) end @@ -227,19 +226,24 @@ end end module ErrorExt def to_s - if Niceql.config.prettify_pg_errors && ActiveRecord::Base.connection_config['adapter'] == 'postgresql' - Prettifier.prettify_err(super) - else - super - end + Niceql.config.prettify_pg_errors ? Prettifier.prettify_err(super) : super end end class NiceQLConfig + def ar_using_pg_adapter? + return false unless defined?(::ActiveRecord::Base) + + adapter = ActiveRecord::Base.try(:connection_db_config).try(:adapter) || + ActiveRecord::Base.try(:connection_config)&.with_indifferent_access&.dig(:adapter) + + adapter == 'postgresql' + end + attr_accessor :pg_adapter_with_nicesql, :indentation_base, :open_bracket_is_newliner, :prettify_active_record_log_output, :prettify_pg_errors @@ -248,28 +252,23 @@ def initialize self.pg_adapter_with_nicesql = false self.indentation_base = 2 self.open_bracket_is_newliner = false self.prettify_active_record_log_output = false - self.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.connection_config['adapter'] == 'postgresql' + self.prettify_pg_errors = ar_using_pg_adapter? end end - def self.configure yield( config ) - if config.pg_adapter_with_nicesql - ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL) - end + return unless defined? ::ActiveRecord::Base - if config.prettify_active_record_log_output - ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier ) - end + ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL) if config.pg_adapter_with_nicesql - if config.prettify_pg_errors - ::ActiveRecord::StatementInvalid.include( Niceql::ErrorExt ) - end + ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier ) if config.prettify_active_record_log_output + + ::ActiveRecord::StatementInvalid.include( Niceql::ErrorExt ) if config.prettify_pg_errors && config.ar_using_pg_adapter? end def self.config @config ||= NiceQLConfig.new end