Skip to content

Commit

Permalink
Simplify is_sharded? calculation further
Browse files Browse the repository at this point in the history
Instead of checking class inheritance we can just call a class method. This way
we can define it to be false for ActiveRecord::Base and override it in sharded
subclasses. This also gives us the possibility of using a module (or just plain
defining the method in any random class).
  • Loading branch information
jacobat committed Oct 23, 2017
1 parent 529386f commit edf44fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_record_shards/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def not_sharded
end

def is_sharded? # rubocop:disable Naming/PredicateName
self <= ActiveRecordShards::ShardedModel
false
end

def on_slave_by_default?
Expand Down
10 changes: 10 additions & 0 deletions lib/active_record_shards/sharded_model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
module ActiveRecordShards
module Ext
module ShardedModel
def is_sharded?
true
end
end
end

class ShardedModel < ActiveRecord::Base
self.abstract_class = true

extend ActiveRecordShards::Ext::ShardedModel
end
end

0 comments on commit edf44fd

Please sign in to comment.