Module: Postamt

Defined in:
lib/postamt.rb,
lib/postamt/railtie.rb,
lib/postamt/version.rb,
lib/postamt/connection_handler.rb

Defined Under Namespace

Modules: LogSubscriber, Relation Classes: ConnectionHandler, Railtie

Constant Summary collapse

VERSION =
"0.9.7"

Class Method Summary collapse

Class Method Details

.configurationsObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/postamt.rb', line 25

def self.configurations
  @configurations ||= begin
    input = ActiveRecord::Base.configurations[Rails.env]
    configs = input.select { |k, v| v.is_a? Hash }
    master_config = input.reject { |k, v| v.is_a? Hash }
    configs.each { |k, v| v.reverse_merge!(master_config) }
    configs['master'] = master_config
    configs
  end
end

.connection_stackObject



36
37
38
# File 'lib/postamt.rb', line 36

def self.connection_stack
  Thread.current[:postamt_connection_stack] ||= []
end

.hook!Object

Called by Postamt::Railtie



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/postamt.rb', line 46

def self.hook!
  if Rails::VERSION::MAJOR == 4 and Rails::VERSION::MINOR <= 2
    ActiveRecord::Base.default_connection_handler = Postamt::ConnectionHandler.new
  elsif Rails::VERSION::MAJOR == 3 and Rails::VERSION::MINOR == 2
    ActiveRecord::Base.connection_handler = Postamt::ConnectionHandler.new
  end

  ActiveRecord::Base.instance_eval do
    class_attribute :default_connection

    # disable Model.establish_connection
    def establish_connection(*args)
      # This would be the only place Model.connection_handler.establish_connection is called.
      nil
    end

    # a transaction runs on Postamt.transaction_connection or on the :on option
    def transaction(options = {}, &block)
      if connection = (options.delete(:on) || Postamt.transaction_connection)
        Postamt.on(connection) { super }
      else
        super
      end
    end
  end

  ActionController::Base.instance_eval do
    def use_db_connection(connection, args)
      klass_names = args.delete(:for)
      if klass_names == :all
        around_filter(args) do |controller|
          Postamt.on(connection) { yield }
        end
      else
        default_connections = {}
        klass_names.each do |klass_name|
          default_connections[klass_name] = connection
        end

        before_filter(args) do |controller|
          Postamt.overwritten_default_connections.merge!(default_connections)
        end
      end
    end

    after_filter do
      Postamt.overwritten_default_connections.clear
    end
  end

  ActiveRecord::LogSubscriber.prepend Postamt::LogSubscriber
  ActiveRecord::Relation.prepend Postamt::Relation
end

.on(connection) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/postamt.rb', line 16

def self.on(connection)
  self.connection_stack << connection
  begin
    yield
  ensure
    self.connection_stack.pop
  end
end

.overwritten_default_connectionsObject

Used by use_db_connection. Cleared in an after_filter.



41
42
43
# File 'lib/postamt.rb', line 41

def self.overwritten_default_connections
  Thread.current[:postamt_overwritten_default_connections] ||= {}
end