Class: Hijacker::Database
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Hijacker::Database
- Defined in:
- lib/hijacker/database.rb
Class Method Summary collapse
- .connect_each(sites = all.map(&:database)) ⇒ Object
- .connect_to_each_shared_site(&block) ⇒ Object
- .connect_to_each_sister_site(&block) ⇒ Object
- .current ⇒ Object
- .disabled_databases ⇒ Object
-
.find_master_and_sister_for(client) ⇒ Object
always returns a master, sister can be nil.
-
.find_master_for(client) ⇒ Object
returns a string or nil.
- .find_shared_sites_for(client) ⇒ Object
- .shared_sites ⇒ Object
Instance Method Summary collapse
Class Method Details
.connect_each(sites = all.map(&:database)) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hijacker/database.rb', line 63 def self.connect_each(sites = all.map(&:database)) original_database = Hijacker.current_client begin sites.each do |db| Hijacker.connect_to_master(db) yield db end ensure begin Hijacker.connect_to_master(original_database) rescue Hijacker::InvalidDatabase end end end |
.connect_to_each_shared_site(&block) ⇒ Object
38 39 40 |
# File 'lib/hijacker/database.rb', line 38 def self.connect_to_each_shared_site(&block) connect_each(find_shared_sites_for(Hijacker.current_client), &block) end |
.connect_to_each_sister_site(&block) ⇒ Object
42 43 44 45 46 |
# File 'lib/hijacker/database.rb', line 42 def self.connect_to_each_sister_site(&block) sites = find_shared_sites_for(Hijacker.current_client) sites.delete(Hijacker.current_client) connect_each(sites, &block) end |
.current ⇒ Object
10 11 12 |
# File 'lib/hijacker/database.rb', line 10 def self.current find(:first, :conditions => {:database => Hijacker.current_client}) end |
.disabled_databases ⇒ Object
78 79 80 |
# File 'lib/hijacker/database.rb', line 78 def self.disabled_databases Hijacker::Database.connection.select_values("SELECT `database_name` FROM `disabled_databases`") end |
.find_master_and_sister_for(client) ⇒ Object
always returns a master, sister can be nil
26 27 28 29 30 31 32 |
# File 'lib/hijacker/database.rb', line 26 def self.find_master_and_sister_for(client) master = self.find_master_for(client) sister = master.nil? ? nil : client master ||= client return master, sister end |
.find_master_for(client) ⇒ Object
returns a string or nil
15 16 17 18 19 20 21 22 23 |
# File 'lib/hijacker/database.rb', line 15 def self.find_master_for(client) @masters ||= {} @masters[client] ||= self.connection.select_values( "SELECT master.database FROM `databases` AS master, `databases` AS sister WHERE sister.database = #{ActiveRecord::Base.connection.quote(client)} AND sister.master_id = master.id" ).first end |
.find_shared_sites_for(client) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hijacker/database.rb', line 48 def self.find_shared_sites_for(client) @shared_sites ||= {} return @shared_sites[client] if @shared_sites[client].present? current = self.find(:first, :conditions => {:database => client}) master_id = current.master_id || current.id @shared_sites[client] = self.connection.select_values( "SELECT `database` FROM `databases` WHERE master_id = '#{master_id}' OR id = '#{master_id}' ORDER BY id" ) end |
.shared_sites ⇒ Object
34 35 36 |
# File 'lib/hijacker/database.rb', line 34 def self.shared_sites self.find_shared_sites_for(Hijacker.current_client) end |