Sha256: 9b41367a7c92b8bf3e0dd0912c10afcf7fb01f00fd1e27807155ac1e3b5e9871

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require "active_record/ssh_tunnel/version"
require "net/ssh/gateway"
require "rails/railtie"

module ActiveRecord
  module SshTunnel
    class Railtie < ::Rails::Railtie
      ActiveSupport.on_load(:active_record) do
        ActiveRecord::Base.singleton_class.prepend ::ActiveRecord::SshTunnel
      end
    end

    def establish_connection(spec = nil)
      spec     ||= ConnectionHandling::DEFAULT_ENV.call.to_sym
      resolver =   ConnectionAdapters::ConnectionSpecification::Resolver.new configurations
      spec     =   resolver.spec(spec)

      if spec.config[:ssh_tunnel_hostname]
        ssh_options = Hash[spec.config.keys.select { |key|
          key.to_s.start_with? "ssh_tunnel_"
        }.map{ |key|
          [key.to_s.gsub("ssh_tunnel_", "").to_sym, spec.config[key]]
        }]

        port = Net::SSH::Gateway.new(
          ssh_options.delete(:hostname),
          ssh_options.delete(:user),
          ssh_options,
        ).open(spec.config[:host], spec.config[:port])

        spec.config[:host] = "127.0.0.1"
        spec.config[:port] = port
      end

      remove_connection
      connection_handler.establish_connection self, spec
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record-ssh_tunnel-0.1.0 lib/active_record/ssh_tunnel.rb