Module: ActiveRecordShards::Model
- Defined in:
- lib/active_record_shards/model.rb
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#is_sharded? ⇒ Boolean
rubocop:disable Naming/PredicateName.
- #not_sharded ⇒ Object
- #on_replica_by_default=(value) ⇒ Object
- #on_replica_by_default? ⇒ Boolean
Class Method Details
.extended(base) ⇒ Object
70 71 72 73 |
# File 'lib/active_record_shards/model.rb', line 70 def self.extended(base) base.send(:include, InstanceMethods) base.after_initialize :initialize_shard_and_replica end |
Instance Method Details
#is_sharded? ⇒ Boolean
rubocop:disable Naming/PredicateName
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_record_shards/model.rb', line 13 def is_sharded? # rubocop:disable Naming/PredicateName # "sharded" here means self.sharded, but actually writing "self.sharded" # doesn't work until Ruby 2.7 (and this gem currently supports 2.6) because # the sharded attr_accessor is private. Private methods must be called without # a receiver, but Ruby 2.7+ does allow an explicit "self" as a receiver. return sharded unless sharded.nil? # Despite self.sharded not working, self.sharded= _DOES_ work. That's an exception # to the "private methods must be called with no receiver" rule (presumably # because it would otherwise be ambiguous with local variable assignment). self.sharded = if self == ActiveRecord::Base sharded != false && supports_sharding? elsif self == base_class if sharded.nil? ActiveRecord::Base.is_sharded? else sharded != false end else base_class.is_sharded? end end |
#not_sharded ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/active_record_shards/model.rb', line 5 def not_sharded if self != ActiveRecord::Base && self != base_class raise "You should only call not_sharded on direct descendants of ActiveRecord::Base" end self.sharded = false end |
#on_replica_by_default=(value) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/active_record_shards/model.rb', line 47 def on_replica_by_default=(value) if self == ActiveRecord::Base raise ArgumentError, "Cannot set on_replica_by_default on ActiveRecord::Base" else base_class.instance_variable_set(:@on_replica_by_default, value) end end |
#on_replica_by_default? ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_record_shards/model.rb', line 36 def on_replica_by_default? if self == ActiveRecord::Base false else base = base_class if base.instance_variable_defined?(:@on_replica_by_default) base.instance_variable_get(:@on_replica_by_default) end end end |