Module: Hijacker::ControllerMethods::Instance

Defined in:
lib/hijacker/controller_methods.rb

Instance Method Summary collapse

Instance Method Details

#determine_databases(host) ⇒ Object

Returns 2-member array of the main database to connect to, and the sister (sister will be nil if no master is found, which means we are on the master).



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hijacker/controller_methods.rb', line 23

def determine_databases(host)
  if Hijacker.do_hijacking?
    Hijacker.config[:domain_patterns].find {|pattern| host =~ pattern}
    client = $1
  else # development, test, etc
    client = ActiveRecord::Base.configurations[Rails.env]['database']
  end

  raise Hijacker::UnparseableURL, "cannot parse '#{host}'" if client.nil?

  master, sister = Hijacker::Database.find_master_and_sister_for(client)
  
  return [master, sister]
end

#hijack_connectionObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hijacker/controller_methods.rb', line 3

def hijack_connection
  host = request.host
  
  master, sister = determine_databases(host)

  Hijacker.connect(master, sister)
  
  return true
rescue Hijacker::InvalidDatabase => e
  render_invalid_db
  
  # If we've encountered a bad database connection, we don't want
  # to continue rendering the rest of the before_filters on this, which it will
  # try to do even when just rendering the bit of text above. If any filters
  # return false, though, it will halt the filter chain.
  return false
end

#render_invalid_dbObject



38
39
40
# File 'lib/hijacker/controller_methods.rb', line 38

def render_invalid_db
  render :text => "You do not appear to have an account with us (#{request.host})"
end