Module: SeamlessDatabasePool::ControllerFilter

Defined in:
lib/seamless_database_pool/controller_filter.rb

Overview

This module provides a simple method of declaring which read pool connection type should be used for various ActionController actions. To use it, you must first mix it into you controller and then call use_database_pool to configure the connection types. Generally you should just do this in ApplicationController and call use_database_pool in your controllers when you need different connection types.

Example:

ApplicationController < ActionController::Base
  include SeamlessDatabasePool::ControllerFilter
  use_database_pool :all => :persistent, [:save, :delete] => :master
  ...

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/seamless_database_pool/controller_filter.rb', line 16

def self.included(base)
  unless base.respond_to?(:use_database_pool)
    base.extend(ClassMethods)
    base.class_eval do
      if base.method_defined?(:perform_action) || base.private_method_defined?(:perform_action)
        alias_method_chain :perform_action, :seamless_database_pool
      else
        alias_method_chain :process, :seamless_database_pool
      end
      alias_method_chain :redirect_to, :seamless_database_pool
    end
  end
end

Instance Method Details

#process_with_seamless_database_pool(action, *args) ⇒ Object

Rails 3.x hook for setting the read connection for the request.


77
78
79
80
81
# File 'lib/seamless_database_pool/controller_filter.rb', line 77

def process_with_seamless_database_pool(action, *args)
  set_read_only_connection_for_block(action) do
    process_without_seamless_database_pool(action, *args)
  end
end

#redirect_to_with_seamless_database_pool(options = {}, response_status = {}) ⇒ Object


83
84
85
86
87
88
# File 'lib/seamless_database_pool/controller_filter.rb', line 83

def redirect_to_with_seamless_database_pool(options = {}, response_status = {})
  if SeamlessDatabasePool.read_only_connection_type(nil) == :master
    use_master_db_connection_on_next_request
  end
  redirect_to_without_seamless_database_pool(options, response_status)
end

#seamless_database_pool_optionsObject


72
73
74
# File 'lib/seamless_database_pool/controller_filter.rb', line 72

def seamless_database_pool_options
  self.class.seamless_database_pool_options
end

#use_master_db_connection_on_next_requestObject

Force the master connection to be used on the next request. This is very useful for the Post-Redirect pattern where you post a request to your save action and then redirect the user back to the edit action. By calling this method, you won't have to worry if the replication engine is slower than the redirect. Normally you won't need to call this method yourself as it is automatically called when you perform a redirect from within a master connection block. It is made available just in case you have special needs that don't quite fit into this module's default logic.


68
69
70
# File 'lib/seamless_database_pool/controller_filter.rb', line 68

def use_master_db_connection_on_next_request
  session[:next_request_db_connection] = :master if session
end