Module: Octopus::Model::SharedMethods
- Included in:
- ClassMethods, InstanceMethods
- Defined in:
- lib/octopus/model.rb
Instance Method Summary collapse
- #clean_table_name ⇒ Object
- #hijack_connection ⇒ Object
- #hijack_initializer ⇒ Object
- #set_current_shard ⇒ Object
- #using(shard, &block) ⇒ Object
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
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/octopus/model.rb', line 43 def hijack_connection() class << self def connection_proxy Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config()) end def connection self.connection_proxy().current_model = self self.connection_proxy() end end end |
#hijack_initializer ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/octopus/model.rb', line 29 def hijack_initializer() attr_accessor :current_shard after_initialize :set_current_shard before_save :set_connection def set_current_shard if new_record? || self.connection.block self.current_shard = self.class.connection_proxy.current_shard else self.current_shard = self.class.connection_proxy.last_current_shard end end end |
#set_current_shard ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/octopus/model.rb', line 34 def set_current_shard if new_record? || self.connection.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, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/octopus/model.rb', line 13 def using(shard, &block) return self if defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s) hijack_connection() clean_table_name() if block_given? self.connection.run_queries_on_shard(shard, &block) else hijack_initializer() self.connection.using_enabled = true return Octopus::ScopeProxy.new(shard, self) end end |