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 ⇒ Object
- #save! ⇒ Object
- #shard ⇒ Object
- #shard=(new_shard) ⇒ Object
- #to_param ⇒ Object
- #transaction(**kwargs, &block) ⇒ Object
- #update_columns ⇒ Object
Class Method Details
.included(klass) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/switchman/active_record/base.rb', line 82 def self.included(klass) klass.singleton_class.prepend(ClassMethods) klass.set_callback(:initialize, :before) do @shard ||= if self.class.sharded_primary_key? Shard.shard_for(self[self.class.primary_key], Shard.current(self.class.connection_classes)) else Shard.current(self.class.connection_classes) end end end |
Instance Method Details
#clone ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/switchman/active_record/base.rb', line 122 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, shard) result end |
#destroy ⇒ Object
118 119 120 |
# File 'lib/switchman/active_record/base.rb', line 118 def destroy shard.activate(self.class.connection_classes) { super } end |
#hash ⇒ Object
137 138 139 |
# File 'lib/switchman/active_record/base.rb', line 137 def hash self.class.sharded_primary_key? ? self.class.hash ^ global_id.hash : super end |
#initialize_dup(*args) ⇒ Object
146 147 148 149 150 |
# File 'lib/switchman/active_record/base.rb', line 146 def initialize_dup(*args) copy = super @shard_set_in_stone = false copy end |
#quoted_id ⇒ Object
152 153 154 155 156 157 |
# 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) end |
#save ⇒ Object
108 109 110 111 |
# File 'lib/switchman/active_record/base.rb', line 108 def save(*, **) @shard_set_in_stone = true (self.class.current_scope || self.class.default_scoped).shard(shard, :implicit).scoping { super } end |
#save! ⇒ Object
113 114 115 116 |
# File 'lib/switchman/active_record/base.rb', line 113 def save!(*, **) @shard_set_in_stone = true (self.class.current_scope || self.class.default_scoped).shard(shard, :implicit).scoping { super } end |
#shard ⇒ Object
93 94 95 |
# File 'lib/switchman/active_record/base.rb', line 93 def shard @shard || Shard.current(self.class.connection_classes) || Shard.default end |
#shard=(new_shard) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/switchman/active_record/base.rb', line 97 def shard=(new_shard) raise ::ActiveRecord::ReadOnlyRecord if !new_record? || @shard_set_in_stone return 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 |
#to_param ⇒ Object
141 142 143 144 |
# File 'lib/switchman/active_record/base.rb', line 141 def to_param short_id = Shard.short_id_for(id) short_id&.to_s end |
#transaction(**kwargs, &block) ⇒ Object
131 132 133 134 135 |
# File 'lib/switchman/active_record/base.rb', line 131 def transaction(**kwargs, &block) shard.activate(self.class.connection_classes) do self.class.transaction(**kwargs, &block) end end |