Module: Octopus::Model::SharedMethods
- Included in:
- ClassMethods, InstanceMethods
- Defined in:
- lib/octopus/model.rb
Class Method Summary collapse
- .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_proxy ⇒ Object
50 51 52 |
# File 'lib/octopus/model.rb', line 50 def self.connection_proxy Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config()) end |
.connection_with_octopus ⇒ Object
54 55 56 57 58 59 |
# File 'lib/octopus/model.rb', line 54 def self.connection_with_octopus() return connection_without_octopus() if should_use_normal_connection? self.connection_proxy().current_model = self self.connection_proxy() end |
.should_use_normal_connection? ⇒ Boolean
46 47 48 |
# File 'lib/octopus/model.rb', line 46 def self.should_use_normal_connection? defined?(Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s) end |
Instance Method Details
#clean_table_name ⇒ Object
9 10 11 |
# File 'lib/octopus/model.rb', line 9 def clean_table_name self.reset_table_name() if self != ActiveRecord::Base && self.respond_to?(:reset_table_name) end |
#hijack_connection ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/octopus/model.rb', line 44 def hijack_connection() def self.should_use_normal_connection? defined?(Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s) end def self.connection_proxy Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config()) end def self.connection_with_octopus() return connection_without_octopus() if should_use_normal_connection? self.connection_proxy().current_model = self self.connection_proxy() end class << self alias_method_chain :connection, :octopus end end |
#hijack_initializer ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/octopus/model.rb', line 24 def hijack_initializer() attr_accessor :current_shard after_initialize :set_current_shard before_save :reload_connection def set_current_shard 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 end end if !Octopus.rails3? def after_initialize set_current_shard() end end end |
#set_current_shard ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/octopus/model.rb', line 29 def set_current_shard 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 end end |
#using(shard) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/octopus/model.rb', line 13 def using(shard) return self if defined?(::Rails) && !Octopus.enviroments.include?(Rails.env.to_s) clean_table_name() hijack_initializer() self.connection_proxy.using_enabled = true return Octopus::ScopeProxy.new(shard, self) end |