Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Fix where_unscoping on 4.0, 4.1, 4.2 #421

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def where(opts = :chain, *rest)

def where_unscoping(target_value)
target_value = target_value.to_s
where_values.flatten!

where_values.reject! do |rel|
case rel
Expand Down
6 changes: 2 additions & 4 deletions lib/squeel/adapters/active_record/4.1/relation_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,18 @@ def visited

def where_unscoping(target_value)
target_value = target_value.to_s
where_values.flatten!

where_values.reject! do |rel|
case rel
when Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual
subrelation = (rel.left.kind_of?(Arel::Attributes::Attribute) ? rel.left : rel.right)
subrelation.name == target_value
when Hash
rel.stringify_keys.has_key?(target_value)
when Squeel::Nodes::Predicate
rel.expr.symbol.to_s == target_value if rel.expr.respond_to?(:symbol)
end
end

bind_values.reject! { |col,_| col.name == target_value }
super
end

def reverse_sql_order(order_query)
Expand Down
15 changes: 15 additions & 0 deletions lib/squeel/adapters/active_record/4.2/relation_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ module RelationExtensions

undef_method 'to_sql_with_binding_params'

def where_unscoping(target_value)
target_value = target_value.to_s

self.where_values = where_values.flatten.reject do |rel|
case rel
when Hash
rel.stringify_keys.has_key?(target_value)
when Squeel::Nodes::Predicate
rel.expr.symbol.to_s == target_value if rel.expr.respond_to?(:symbol)
end
end

super
end

attr_accessor :reverse_order_value
private :reverse_order_value, :reverse_order_value=

Expand Down
18 changes: 9 additions & 9 deletions spec/squeel/adapters/active_record/relation_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1067,17 +1067,17 @@ module ActiveRecord

describe '#where_unscoping' do

it "isn't broken" do
OrderItem.where(:quantity => 0).unscope(:where => :quantity).where_values.should be_empty
end

it "doesn't ruin everything when predicate expression in where_values doesn't respond to :symbol method" do
unless activerecord_version_at_least '4.2.0'
if activerecord_version_at_least '4.0.0'
order_items = OrderItem.where{quantity == 0}.where{unit_price / 2 == 5}
expect { order_items.unscope(where: :quantity) }.should_not raise_error
order_items.to_sql.should_not match /#{Q}order_items#{Q}.#{Q}quantity#{Q} = 0/
else
pending 'Unsupported on AR versions < 4.0.0'
end
if activerecord_version_at_least '4.0.0'
order_items = OrderItem.where{quantity == 0}.where{unit_price / 2 == 5}
expect { order_items.unscope!(where: :quantity) }.should_not raise_error
order_items.to_sql.should_not match /#{Q}order_items#{Q}.#{Q}quantity#{Q} = 0/
else
pending 'Not required in AR versions > 4.2.0'
pending 'Unsupported on AR versions < 4.0.0'
end
end

Expand Down