Skip to content

Commit

Permalink
Merge pull request #10 from iberianpig/ar_on_load
Browse files Browse the repository at this point in the history
Wrap ActiveRecord::Base with on_load(:active_record) hook
  • Loading branch information
fumihumi authored May 7, 2024
2 parents 9a19e72 + fe7594b commit c15eca9
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 c15eca9

Please sign in to comment.