Skip to content

Commit

Permalink
Follow Association behavior changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gussan committed Dec 29, 2014
1 parent 56fcb98 commit 0b5fb90
Showing 1 changed file with 77 additions and 27 deletions.
104 changes: 77 additions & 27 deletions lib/active_record/turntable/active_record_ext/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,53 @@ module SingularAssociationExt
extend ActiveSupport::Concern

included do
alias_method_chain :find_target, :turntable
if ActiveRecord::Turntable.rails42_later?
alias_method_chain :get_records, :turntable
else
alias_method_chain :find_target, :turntable
end
end

private

def turntable_scope(scope)
if should_use_shard_key?
scope = scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
end
scope
end

# @note Override to add sharding condition for singular association
if ActiveRecord::Turntable.rails41_later?
def find_target_with_turntable
current_scope = scope
if should_use_shard_key?
current_scope = current_scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
if ActiveRecord::Turntable.rails42_later?
def get_records_with_turntable
if reflection.scope_chain.any?(&:any?) ||
scope.eager_loading? ||
klass.current_scope ||
klass.default_scopes.any?

return turntable_scope(scope).limit(1).to_a
end
if record = current_scope.take

conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
ActiveRecord::StatementCache.create(conn) { |params|
as = ActiveRecord::Associations::AssociationScope.create { params.bind }
target_scope.merge(as.scope(self, conn)).limit(1)
}
end

binds = ActiveRecord::Associations::AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection
end
elsif ActiveRecord::Turntable.rails41_later?
def find_target_with_turntable
if record = turntable_scope(scope).take
set_inverse_instance record
end
end
else
def find_target_with_turntable
current_scope = scope
if should_use_shard_key?
current_scope = current_scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
end
current_scope.take.tap { |record| set_inverse_instance(record) }
turntable_scope(scope).take.tap { |record| set_inverse_instance(record) }
end
end
end
Expand All @@ -46,27 +70,53 @@ module CollectionAssociationExt
extend ActiveSupport::Concern

included do
alias_method_chain :find_target, :turntable
if ActiveRecord::Turntable.rails42_later?
alias_method_chain :get_records, :turntable
else
alias_method_chain :find_target, :turntable
end
end

private

# @note Override to add sharding condition for collection association
def find_target_with_turntable
records =
if options[:finder_sql]
reflection.klass.find_by_sql(custom_finder_sql)
else
current_scope = scope
if should_use_shard_key?
current_scope = current_scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
end
current_scope.to_a
if ActiveRecord::Turntable.rails42_later?
def get_records_with_turntable
if reflection.scope_chain.any?(&:any?) ||
scope.eager_loading? ||
klass.current_scope ||
klass.default_scopes.any?

return scope.to_a
end

conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
ActiveRecord::StatementCache.create(conn) { |params|
as = ActiveRecord::Associations::AssociationScope.create { params.bind }
target_scope.merge as.scope(self, conn)
}
end
records.each { |record| set_inverse_instance(record) }
records
end

binds = ActiveRecord::Associations::AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection
end
else
# @note Override to add sharding condition for collection association
def find_target_with_turntable
records =
if options[:finder_sql]
reflection.klass.find_by_sql(custom_finder_sql)
else
current_scope = scope
if should_use_shard_key?
current_scope = current_scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
end
current_scope.to_a
end
records.each { |record| set_inverse_instance(record) }
records
end
end
end

private
Expand Down

0 comments on commit 0b5fb90

Please sign in to comment.