Module: Octopus::Model::SharedMethods

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connection_proxyObject



45
46
47
# File 'lib/octopus/model.rb', line 45

def self.connection_proxy
  Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
end

.connection_with_octopusObject



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

def self.connection_with_octopus()
  if defined?(Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s)
    return connection_without_octopus() 
  end

  self.connection_proxy().current_model = self
  self.connection_proxy()
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/octopus/model.rb', line 44

def hijack_connection()
  def self.connection_proxy
    Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
  end

  def self.connection_with_octopus()
    if defined?(Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s)
      return connection_without_octopus() 
    end

    self.connection_proxy().current_model = self
    self.connection_proxy()
  end

  class << self
    alias_method_chain :connection, :octopus
  end
end

#hijack_initializerObject



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_shardObject



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