module Postamt
Constants
- VERSION
Public Class Methods
configurations()
click to toggle source
# 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_stack()
click to toggle source
# File lib/postamt.rb, line 36 def self.connection_stack Thread.current[:postamt_connection_stack] ||= [] end
hook!()
click to toggle source
Called by Postamt::Railtie
# 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) { || ... }
click to toggle source
# 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_connections()
click to toggle source
Used by use_db_connection. Cleared in an after_filter.
# File lib/postamt.rb, line 41 def self.overwritten_default_connections Thread.current[:postamt_overwritten_default_connections] ||= {} end
Public Instance Methods
establish_connection(*args)
click to toggle source
disable Model.establish_connection
# File lib/postamt.rb, line 57 def establish_connection(*args) # This would be the only place Model.connection_handler.establish_connection is called. nil end
transaction(options = {}, &block)
click to toggle source
a transaction runs on Postamt.transaction_connection or on the :on option
Calls superclass method
# File lib/postamt.rb, line 63 def transaction(options = {}, &block) if connection = (options.delete(:on) || Postamt.transaction_connection) Postamt.on(connection) { super } else super end end
use_db_connection(connection, args) { || ... }
click to toggle source
# File lib/postamt.rb, line 73 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