Module: Switchman::ActiveRecord::Base

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



40
41
42
43
# File 'lib/switchman/active_record/base.rb', line 40

def self.included(klass)
  klass.extend(ClassMethods)
  klass.set_callback(:initialize, :before) { @shard = Shard.current(self.class.shard_category) }
end

Instance Method Details

#cloneObject



71
72
73
74
75
76
77
78
# File 'lib/switchman/active_record/base.rb', line 71

def clone
  result = super
  # TODO: adjust foreign keys
  # don't use the setter, cause the foreign keys are already
  # relative to this shard
  result.instance_variable_set(:@shard, self.shard)
  result
end

#destroyObject



67
68
69
# File 'lib/switchman/active_record/base.rb', line 67

def destroy
  shard.activate(self.class.shard_category) { super }
end

#hashObject



86
87
88
# File 'lib/switchman/active_record/base.rb', line 86

def hash
  global_id.hash
end

#save(*args) ⇒ Object



57
58
59
60
# File 'lib/switchman/active_record/base.rb', line 57

def save(*args)
  @shard_set_in_stone = true
  shard.activate(self.class.shard_category) { super }
end

#save!(*args) ⇒ Object



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

def save!(*args)
  @shard_set_in_stone = true
  shard.activate(self.class.shard_category) { super }
end

#shardObject



45
46
47
# File 'lib/switchman/active_record/base.rb', line 45

def shard
  @shard || Shard.current(self.class.shard_category) || Shard.default
end

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


49
50
51
52
53
54
55
# File 'lib/switchman/active_record/base.rb', line 49

def shard=(new_shard)
  raise ::ActiveRecord::ReadOnlyRecord if !self.new_record? || @shard_set_in_stone
  if shard != new_shard
    # TODO: adjust foreign keys
    @shard = new_shard
  end
end

#to_paramObject



90
91
92
# File 'lib/switchman/active_record/base.rb', line 90

def to_param
  Shard.short_id_for(self.id) if persisted?
end

#transaction(&block) ⇒ Object



80
81
82
83
84
# File 'lib/switchman/active_record/base.rb', line 80

def transaction(&block)
  shard.activate do
    super
  end
end