Skip to content

Commit

Permalink
Merge branch 'association-conditions-rails3' into integration
Browse files Browse the repository at this point in the history
* association-conditions-rails3:
  Avoid overwriting association conditions with default scope
  • Loading branch information
avit committed Jul 10, 2015
2 parents 4e1aace + 9f778e8 commit c31ca3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
20 changes: 7 additions & 13 deletions lib/ransack/adapters/active_record/3.0/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,19 @@ def build_or_find_association(name, parent = @base, klass = nil)
:build, Polyamorous::Join.new(name, @join_type, klass), parent
)
found_association = @join_dependency.join_associations.last
apply_default_conditions(found_association)

default_conditions = found_association.active_record.scoped.arel.constraints
if default_conditions.any?
and_default_conditions = "AND #{default_conditions.reduce(&:and).to_sql}"
end

# Leverage the stashed association functionality in AR
@object = @object.joins(found_association)
@object = @object.joins(found_association).joins(and_default_conditions)
end

found_association
end

def apply_default_conditions(join_association)
reflection = join_association.reflection
return if reflection.instance_variable_get(:@_ransack_applied_default_conditions)
assoc_scope = join_association.active_record.where(reflection.options[:conditions])
assoc_conditions = assoc_scope.arel.constraints
if assoc_conditions.any?
reflection.options[:conditions] = assoc_conditions.reduce(&:and)
end
reflection.instance_variable_set(:@_ransack_applied_default_conditions, true)
end

end
end
end
Expand Down
26 changes: 10 additions & 16 deletions lib/ransack/adapters/active_record/3.1/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def join_associations
#
def join_sources
base = Arel::SelectManager.new(@object.engine, @object.table)
joins = @object.joins_values
joins.each do |assoc|
@object.joins_values.each do |assoc|
next unless assoc.is_a?(JoinDependency::JoinAssociation)
assoc.join_to(base)
end
base.join_sources
Expand Down Expand Up @@ -151,7 +151,7 @@ def build_join_dependency(relation)
Constants::STRING_JOIN
when Hash, Symbol, Array
Constants::ASSOCIATION_JOIN
when ::ActiveRecord::Associations::JoinDependency::JoinAssociation
when JoinDependency::JoinAssociation
Constants::STASHED_JOIN
when Arel::Nodes::Join
Constants::JOIN_NODE
Expand Down Expand Up @@ -196,25 +196,19 @@ def build_or_find_association(name, parent = @base, klass = nil)
:build, Polyamorous::Join.new(name, @join_type, klass), parent
)
found_association = @join_dependency.join_associations.last
apply_default_conditions(found_association)

default_conditions = found_association.active_record.scoped.arel.constraints
if default_conditions.any?
and_default_conditions = "AND #{default_conditions.reduce(&:and).to_sql}"
end

# Leverage the stashed association functionality in AR
@object = @object.joins(found_association)
@object = @object.joins(found_association).joins(and_default_conditions)
end

found_association
end

def apply_default_conditions(join_association)
reflection = join_association.reflection
return if reflection.instance_variable_get(:@_ransack_applied_default_conditions)
assoc_scope = join_association.active_record.where(reflection.options[:conditions])
assoc_conditions = assoc_scope.arel.constraints
if assoc_conditions.any?
reflection.options[:conditions] = assoc_conditions.reduce(&:and)
end
reflection.instance_variable_set(:@_ransack_applied_default_conditions, true)
end

end
end
end
Expand Down

0 comments on commit c31ca3f

Please sign in to comment.