Module: Octopus::Model::ClassMethods

Includes:
SharedMethods
Defined in:
lib/octopus/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

#clean_table_name, #using

Class Method Details

.extended(base) ⇒ Object



74
75
76
77
78
# File 'lib/octopus/model.rb', line 74

def self.extended(base)
  base.class_attribute(:replicated)
  base.class_attribute(:sharded)
  base.hijack_methods
end

Instance Method Details

#clear_active_connections_with_octopus!Object



144
145
146
147
148
149
150
# File 'lib/octopus/model.rb', line 144

def clear_active_connections_with_octopus!
  if should_use_normal_connection?
    clear_active_connections_without_octopus!
  else
    connection_proxy.clear_active_connections!
  end
end

#clear_all_connections_with_octopus!Object



152
153
154
155
156
157
158
# File 'lib/octopus/model.rb', line 152

def clear_all_connections_with_octopus!
  if should_use_normal_connection?
    clear_all_connections_without_octopus!
  else
    connection_proxy.clear_all_connections!
  end
end

#connected_with_octopus?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
# File 'lib/octopus/model.rb', line 160

def connected_with_octopus?
  if should_use_normal_connection?
    connected_without_octopus?
  else
    connection_proxy.connected?
  end
end

#connection_pool_with_octopusObject



136
137
138
139
140
141
142
# File 'lib/octopus/model.rb', line 136

def connection_pool_with_octopus
  if should_use_normal_connection?
    connection_pool_without_octopus
  else
    connection_proxy.connection_pool
  end
end

#connection_proxyObject



113
114
115
116
117
118
119
120
121
# File 'lib/octopus/model.rb', line 113

def connection_proxy
  cached = ActiveRecord::Base.class_variable_get :@@connection_proxy rescue nil
  cached ||
    begin
      p = Octopus::Proxy.new
      ActiveRecord::Base.class_variable_set :@@connection_proxy, p
      p
    end
end

#connection_with_octopusObject



127
128
129
130
131
132
133
134
# File 'lib/octopus/model.rb', line 127

def connection_with_octopus
  if should_use_normal_connection?
    connection_without_octopus
  else
    connection_proxy.current_model = self
    connection_proxy
  end
end

#hijack_methodsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/octopus/model.rb', line 88

def hijack_methods
  around_save :run_on_shard
  after_initialize :set_current_shard

  class << self
    attr_accessor :custom_octopus_connection
    attr_accessor :custom_octopus_table_name

    alias_method_chain :connection, :octopus
    alias_method_chain :connection_pool, :octopus
    alias_method_chain :clear_all_connections!, :octopus
    alias_method_chain :clear_active_connections!, :octopus
    alias_method_chain :connected?, :octopus

    if Octopus.rails3?
      alias_method_chain(:set_table_name, :octopus)
    end

    def table_name=(value = nil)
      self.custom_octopus_table_name = true
      super
    end
  end
end

#octopus_establish_connection(spec = ENV['DATABASE_URL']) ⇒ Object



173
174
175
176
# File 'lib/octopus/model.rb', line 173

def octopus_establish_connection(spec = ENV['DATABASE_URL'])
  self.custom_octopus_connection = true if spec
  establish_connection(spec)
end

#octopus_set_table_name(value = nil) ⇒ Object



178
179
180
181
# File 'lib/octopus/model.rb', line 178

def octopus_set_table_name(value = nil)
  ActiveSupport::Deprecation.warn "Calling `octopus_set_table_name` is deprecated and will be removed in Octopus 1.0.", caller
  set_table_name(value)
end

#replicated_modelObject



80
81
82
# File 'lib/octopus/model.rb', line 80

def replicated_model
  self.replicated = true
end

#set_table_name_with_octopus(value = nil, &block) ⇒ Object



168
169
170
171
# File 'lib/octopus/model.rb', line 168

def set_table_name_with_octopus(value = nil, &block)
  self.custom_octopus_table_name = true
  set_table_name_without_octopus(value, &block)
end

#sharded_modelObject



84
85
86
# File 'lib/octopus/model.rb', line 84

def sharded_model
  self.sharded = true
end

#should_use_normal_connection?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/octopus/model.rb', line 123

def should_use_normal_connection?
  !Octopus.enabled? || custom_octopus_connection
end