Module: Switchman::ActiveRecord::Base::ClassMethods

Defined in:
lib/switchman/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#integral_id?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/switchman/active_record/base.rb', line 34

def integral_id?
  if @integral_id == nil
    @integral_id = columns_hash[primary_key].try(:type) == :integer
  end
  @integral_id
end

#reset_column_informationObject



61
62
63
64
# File 'lib/switchman/active_record/base.rb', line 61

def reset_column_information
  @sharded_column_values = {}
  super
end

#shard_categoryObject



8
9
10
# File 'lib/switchman/active_record/base.rb', line 8

def shard_category
  @shard_category || (self.superclass < ::ActiveRecord::Base && self.superclass.shard_category) || :primary
end

#shard_category=(category) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/switchman/active_record/base.rb', line 17

def shard_category=(category)
  categories = Shard.const_get(:CATEGORIES)
  if categories[shard_category]
    categories[shard_category].delete(self)
    categories.delete(shard_category) if categories[shard_category].empty?
  end
  categories[category] ||= []
  categories[category] << self
  if ::Rails.version < '5'
    connection_handler.uninitialize_ar(self)
    @shard_category = category
    connection_handler.initialize_categories(superclass)
  else
    self.connection_specification_name = category.to_s
  end
end

#transaction(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/switchman/active_record/base.rb', line 41

def transaction(*args)
  if self != ::ActiveRecord::Base && current_scope
    current_scope.activate do
      db = Shard.current(shard_category).database_server
      if ::Shackles.environment != db.shackles_environment
        db.unshackle { super }
      else
        super
      end
    end
  else
    db = Shard.current(shard_category).database_server
    if ::Shackles.environment != db.shackles_environment
      db.unshackle { super }
    else
      super
    end
  end
end

#unscopedObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/switchman/active_record/base.rb', line 66

def unscoped
  if block_given?
    super do
      current_scope.shard_value = nil
      yield
    end
  else
    result = super
    result.shard_value = nil
    result
  end
end