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
- #clone ⇒ Object
- #destroy ⇒ Object
- #hash ⇒ Object
- #initialize_dup(*args) ⇒ Object
- #quoted_id ⇒ Object
- #save(*args) ⇒ Object
- #save!(*args) ⇒ Object
- #shard ⇒ Object
- #shard=(new_shard) ⇒ Object
- #to_param ⇒ Object
- #transaction(options = {}, &block) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/switchman/active_record/base.rb', line 73 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
#clone ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/switchman/active_record/base.rb', line 114 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 |
#destroy ⇒ Object
110 111 112 |
# File 'lib/switchman/active_record/base.rb', line 110 def destroy self.class.shard(shard, :implicit).scoping { super } end |
#hash ⇒ Object
129 130 131 |
# File 'lib/switchman/active_record/base.rb', line 129 def hash self.class.sharded_primary_key? ? self.class.hash ^ Shard.global_id_for(id).hash : super end |
#initialize_dup(*args) ⇒ Object
138 139 140 141 142 |
# File 'lib/switchman/active_record/base.rb', line 138 def initialize_dup(*args) copy = super @shard_set_in_stone = false copy end |
#quoted_id ⇒ Object
144 145 146 147 148 |
# File 'lib/switchman/active_record/base.rb', line 144 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) end |
#save(*args) ⇒ Object
100 101 102 103 |
# File 'lib/switchman/active_record/base.rb', line 100 def save(*args) @shard_set_in_stone = true self.class.shard(shard, :implicit).scoping { super } end |
#save!(*args) ⇒ Object
105 106 107 108 |
# File 'lib/switchman/active_record/base.rb', line 105 def save!(*args) @shard_set_in_stone = true self.class.shard(shard, :implicit).scoping { super } end |
#shard ⇒ Object
86 87 88 |
# File 'lib/switchman/active_record/base.rb', line 86 def shard @shard || Shard.current(self.class.shard_category) || Shard.default end |
#shard=(new_shard) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/switchman/active_record/base.rb', line 90 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_param ⇒ Object
133 134 135 136 |
# File 'lib/switchman/active_record/base.rb', line 133 def to_param short_id = Shard.short_id_for(id) short_id && short_id.to_s end |
#transaction(options = {}, &block) ⇒ Object
123 124 125 126 127 |
# File 'lib/switchman/active_record/base.rb', line 123 def transaction(={}, &block) shard.activate(self.class.shard_category) do self.class.transaction(, &block) end end |