Skip to content

Commit

Permalink
Cache the is_sharded? status of the model
Browse files Browse the repository at this point in the history
Sets self.sharded depending on the superclass, if it is not otherwise
set.
  • Loading branch information
KJ Tsanaktsidis committed Apr 5, 2023
1 parent 8f39606 commit fe8627a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/active_record_shards/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def not_sharded
end

def is_sharded? # rubocop:disable Naming/PredicateName
if self == ActiveRecord::Base
# "sharded" here means self.sharded, but actually writing "self.sharded"
# doesn't work until Ruby 2.7 (and this gem currently supports 2.6).
return sharded unless sharded.nil?

# Despite self.sharded not working, self.sharded= _DOES_ work.
self.sharded = if self == ActiveRecord::Base
sharded != false && supports_sharding?
elsif self == base_class
if sharded.nil?
Expand Down

0 comments on commit fe8627a

Please sign in to comment.