Module: Octopus::Model::SharedMethods
- Included in:
- ClassMethods, InstanceMethods
- Defined in:
- lib/octopus/model.rb
Class Method Summary collapse
- .connection_pool_with_octopus ⇒ Object
- .connection_proxy ⇒ Object
- .connection_with_octopus ⇒ Object
- .should_use_normal_connection? ⇒ Boolean
Instance Method Summary collapse
- #clean_table_name ⇒ Object
- #hijack_connection ⇒ Object
- #hijack_initializer ⇒ Object
- #set_current_shard ⇒ Object
- #using(shard) ⇒ Object
Class Method Details
.connection_pool_with_octopus ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/octopus/model.rb', line 67 def self.connection_pool_with_octopus if should_use_normal_connection? connection_pool_without_octopus else connection_proxy.connection_pool end end |
.connection_proxy ⇒ Object
54 55 56 |
# File 'lib/octopus/model.rb', line 54 def self.connection_proxy @@connection_proxy ||= Octopus::Proxy.new end |
.connection_with_octopus ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/octopus/model.rb', line 58 def self.connection_with_octopus if should_use_normal_connection? connection_without_octopus else self.connection_proxy.current_model = self self.connection_proxy end end |
Instance Method Details
#clean_table_name ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/octopus/model.rb', line 12 def clean_table_name return unless self.connection_proxy.should_clean_table_name? if self != ActiveRecord::Base && self.respond_to?(:reset_table_name) && !self.custom_octopus_table_name self.reset_table_name() end self.reset_column_information self.instance_variable_set(:@quoted_table_name, nil) end |
#hijack_connection ⇒ Object
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 |
# File 'lib/octopus/model.rb', line 49 def hijack_connection() def self.should_use_normal_connection? !Octopus.enabled? || self.custom_octopus_connection end def self.connection_proxy @@connection_proxy ||= Octopus::Proxy.new end def self.connection_with_octopus if should_use_normal_connection? connection_without_octopus else self.connection_proxy.current_model = self self.connection_proxy end end def self.connection_pool_with_octopus if should_use_normal_connection? connection_pool_without_octopus else connection_proxy.connection_pool end end class << self alias_method_chain :connection, :octopus alias_method_chain :connection_pool, :octopus end end |
#hijack_initializer ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/octopus/model.rb', line 32 def hijack_initializer() attr_accessor :current_shard before_save :reload_connection def set_current_shard return unless Octopus.enabled? if new_record? || self.class.connection_proxy.block self.current_shard = self.class.connection_proxy.current_shard else self.current_shard = self.class.connection_proxy.last_current_shard || self.class.connection_proxy.current_shard end end after_initialize :set_current_shard end |
#set_current_shard ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/octopus/model.rb', line 36 def set_current_shard return unless Octopus.enabled? if new_record? || self.class.connection_proxy.block self.current_shard = self.class.connection_proxy.current_shard else self.current_shard = self.class.connection_proxy.last_current_shard || self.class.connection_proxy.current_shard end end |
#using(shard) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/octopus/model.rb', line 23 def using(shard) if Octopus.enabled? clean_table_name Octopus::ScopeProxy.new(shard, self) else self end end |