Module: Octopus::Model::SharedMethods

Included in:
ClassMethods, InstanceMethods
Defined in:
lib/octopus/model.rb

Instance Method Summary collapse

Instance Method Details

#clean_table_nameObject



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_connectionObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/octopus/model.rb', line 49

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_initializerObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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
  
  if !Octopus.rails3?
    def after_initialize
      set_current_shard()
    end
  end
end

#set_current_shardObject



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