Skip to content

Commit

Permalink
wrap with on_load(:active_record) hook
Browse files Browse the repository at this point in the history
  • Loading branch information
iberianpig committed May 1, 2024
1 parent 9a19e72 commit fe7594b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
34 changes: 10 additions & 24 deletions lib/octoball.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@

require 'active_record'
require 'octoball/version'
require 'octoball/relation_proxy'
require 'octoball/connection_adapters'
require 'octoball/connection_handling'
require 'octoball/current_shard_tracker'
require 'octoball/association'
require 'octoball/association_shard_check'
require 'octoball/persistence'
require 'octoball/log_subscriber'

class Octoball
def self.using(shard, &block)
ActiveRecord::Base.connected_to(role: current_role, shard: shard&.to_sym, &block)
end

def self.current_role
ActiveRecord::Base.current_role || ActiveRecord::Base.writing_role
end

module UsingShard
def using(shard)
Octoball::RelationProxy.new(all, shard&.to_sym)
end
end

::ActiveRecord::Base.singleton_class.prepend(UsingShard)
ActiveSupport.on_load(:active_record) do
require 'octoball/relation_proxy'
require 'octoball/connection_adapters'
require 'octoball/connection_handling'
require 'octoball/current_shard_tracker'
require 'octoball/association_shard_check'
require 'octoball/persistence'
require 'octoball/association'
require 'octoball/log_subscriber'
require 'octoball/using_shard'
end
17 changes: 17 additions & 0 deletions lib/octoball/using_shard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Octoball
def self.using(shard, &block)
ActiveRecord::Base.connected_to(role: current_role, shard: shard&.to_sym, &block)
end

def self.current_role
ActiveRecord::Base.current_role || ActiveRecord::Base.writing_role
end

module UsingShard
def using(shard)
Octoball::RelationProxy.new(all, shard&.to_sym)
end
end

::ActiveRecord::Base.singleton_class.prepend(UsingShard)
end

0 comments on commit fe7594b

Please sign in to comment.