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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/switchman/active_record/base.rb', line 80

def self.included(klass)
  klass.extend(ClassMethods)
  klass.set_callback(:initialize, :before) do
    unless @shard
      if self.class.sharded_primary_key?
        @shard = Shard.shard_for(self[self.class.primary_key], Shard.current(self.class.shard_category))
      else
        @shard = Shard.current(self.class.shard_category)
      end
    end
  end
end

Instance Method Details

#cloneObject



121
122
123
124
125
126
127
128
# File 'lib/switchman/active_record/base.rb', line 121

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



117
118
119
# File 'lib/switchman/active_record/base.rb', line 117

def destroy
  self.class.shard(shard, :implicit).scoping { super }
end

#hashObject



136
137
138
# File 'lib/switchman/active_record/base.rb', line 136

def hash
  self.class.sharded_primary_key? ? Shard.global_id_for(id).hash : super
end

#initialize_dup(*args) ⇒ Object



145
146
147
148
149
# File 'lib/switchman/active_record/base.rb', line 145

def initialize_dup(*args)
  copy = super
  @shard_set_in_stone = false
  copy
end

#quoted_idObject



152
153
154
155
156
# File 'lib/switchman/active_record/base.rb', line 152

def quoted_id
  return super unless self.class.sharded_primary_key?
  # do this the Rails 4.2 way, so that if Shard.current != self.shard, the id gets transposed
  self.class.connection.quote(id, column_for_attribute(self.class.primary_key))
end

#save(*args) ⇒ Object



107
108
109
110
# File 'lib/switchman/active_record/base.rb', line 107

def save(*args)
  @shard_set_in_stone = true
  self.class.shard(shard, :implicit).scoping { super }
end

#save!(*args) ⇒ Object



112
113
114
115
# File 'lib/switchman/active_record/base.rb', line 112

def save!(*args)
  @shard_set_in_stone = true
  self.class.shard(shard, :implicit).scoping { super }
end

#shardObject



93
94
95
# File 'lib/switchman/active_record/base.rb', line 93

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

#shard=(new_shard) ⇒ Object

Raises:

  • (::ActiveRecord::ReadOnlyRecord)


97
98
99
100
101
102
103
104
105
# File 'lib/switchman/active_record/base.rb', line 97

def shard=(new_shard)
  raise ::ActiveRecord::ReadOnlyRecord if !self.new_record? || @shard_set_in_stone
  if shard != new_shard
    attributes.each do |attr, value|
      self[attr] = Shard.relative_id_for(value, shard, new_shard) if self.class.sharded_column?(attr)
    end
    @shard = new_shard
  end
end

#to_paramObject



140
141
142
143
# File 'lib/switchman/active_record/base.rb', line 140

def to_param
  short_id = Shard.short_id_for(id)
  short_id && short_id.to_s
end

#transaction(options = {}, &block) ⇒ Object



130
131
132
133
134
# File 'lib/switchman/active_record/base.rb', line 130

def transaction(options={}, &block)
  shard.activate(self.class.shard_category) do
    self.class.transaction(options, &block)
  end
end