diff --git a/lib/octopus/association_shard_tracking.rb b/lib/octopus/association_shard_tracking.rb index 36d4622f..dfb2f151 100644 --- a/lib/octopus/association_shard_tracking.rb +++ b/lib/octopus/association_shard_tracking.rb @@ -35,13 +35,48 @@ def connection_on_association=(record) end end - def has_many(name, scope = nil, **options, &extension) - if options == {} && scope.is_a?(Hash) - default_octopus_opts(scope) + + # def has_many(name, scope = nil, **options, &extension) + # puts "has_many called with: name=#{name.inspect}, scope=#{scope.inspect}, options=#{options.inspect}" + # if options == {} && scope.is_a?(Hash) + # default_octopus_opts(scope) + # else + # default_octopus_opts(options) + # end + # # Ensure compatibility with Rails' expected argument count + # if scope.nil? || scope.is_a?(Hash) + # super(name, **options, &extension) + # elsif scope.is_a?(Proc) + # if extension + # super(name, scope, **options, &extension) + # else + # p "bpk2" + # # super(name, **options, &scope) + # super(name, scope, **options, &extension) + # # super(name, scope, **options, &extension) + # # super(name, **options, &scope.to_proc) + # # super(name, **options) { scope.call } + # # super(name, scope, **options) + # end + # else + # super(name, scope, **options, &extension) + # end + # end + + + def has_many(name, *args, &extension) + # Extract scope and options carefully + scope = args[0].is_a?(Proc) ? args.shift : nil + options = args.last.is_a?(Hash) ? args.pop : {} + + # puts "has_many called with: name=#{name.inspect}, scope=#{scope.inspect}, options=#{options.inspect}" + + # Pass correct arguments to ActiveRecord's has_many + if scope + super(name, scope, **options, &extension) else - default_octopus_opts(options) + super(name, **options, &extension) end - super end def has_and_belongs_to_many(association_id, scope = nil, options = {}, &extension) diff --git a/lib/octopus/collection_association.rb b/lib/octopus/collection_association.rb index 6713271a..a1b44c3d 100644 --- a/lib/octopus/collection_association.rb +++ b/lib/octopus/collection_association.rb @@ -1,7 +1,7 @@ module Octopus module CollectionAssociation def self.included(base) - if Octopus.rails51? || Octopus.rails52? + if Octopus.atleast_rails51? base.sharded_methods :reader, :writer, :ids_reader, :ids_writer, :create, :create!, :build, :include?, :load_target, :reload, :size, :select @@ -14,4 +14,4 @@ def self.included(base) end end -ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::CollectionAssociation) +ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::CollectionAssociation) \ No newline at end of file diff --git a/lib/octopus/proxy_config.rb b/lib/octopus/proxy_config.rb index 93a2e5bc..2cc5eb4d 100644 --- a/lib/octopus/proxy_config.rb +++ b/lib/octopus/proxy_config.rb @@ -138,7 +138,8 @@ def initialize_shards(config) self.shards_slave_groups = HashWithIndifferentAccess.new self.slave_groups = HashWithIndifferentAccess.new self.groups = {} - self.config = ActiveRecord::Base.connection_pool_without_octopus.spec.config + # self.config = ActiveRecord::Base.connection_pool_without_octopus.spec.config + self.config = ActiveRecord::Base.connection_pool_without_octopus.db_config.configuration_hash unless config.nil? self.entire_sharded = config['entire_sharded'] @@ -216,15 +217,22 @@ def reinitialize_shards private - def connection_pool_for(config, adapter) - if Octopus.rails4? - spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config.dup, adapter ) - else - name = adapter["octopus_shard"] - spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(name, config.dup, adapter) - end + # def connection_pool_for(config, adapter) + # if Octopus.rails4? + # spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config.dup, adapter ) + # else + # name = adapter["octopus_shard"] + # spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(name, config.dup, adapter) + # end + # + # ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec) + # end - ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec) + def connection_pool_for(config, adapter) + # resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(config) + # spec = resolver.spec(config) + # ActiveRecord::Base.connection_handler.establish_connection(spec) + ActiveRecord::Base.establish_connection(config) end def resolve_string_connection(spec)