Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed returned value of Ransack::Nodes::Condition#format_predicate #692

Merged
merged 1 commit into from
Jul 23, 2016
Merged
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
18 changes: 12 additions & 6 deletions lib/ransack/adapters/active_record/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ def format_predicate(attribute)
arel_pred = arel_predicate_for_attribute(attribute)
arel_values = formatted_values_for_attribute(attribute)
predicate = attribute.attr.public_send(arel_pred, arel_values)
if casted_array_with_in_predicate?(predicate)
predicate.right[0] = format_values_for(predicate.right[0])

if in_predicate?(predicate)
predicate.right = predicate.right.map do |predicate|
casted_array?(predicate) ? format_values_for(predicate) : predicate
end
end

predicate
end

def casted_array_with_in_predicate?(predicate)
def in_predicate?(predicate)
return unless defined?(Arel::Nodes::Casted)
predicate.class == Arel::Nodes::In &&
predicate.right[0].respond_to?(:val) &&
predicate.right[0].val.is_a?(Array)
predicate.class == Arel::Nodes::In
end

def casted_array?(predicate)
predicate.respond_to?(:val) && predicate.val.is_a?(Array)
end

def format_values_for(predicate)
Expand Down
4 changes: 2 additions & 2 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ def self.simple_escaping?

context 'searching on an `in` predicate with a ransacker' do
it 'should function correctly when passing an array of ids' do
s = Person.ransack(array_users_in: true)
expect(s.result.count).to be > 0
s = Person.ransack(array_users_in: [1, 2])
expect(s.result.count).to be 2
end

it 'should function correctly when passing an array of strings' do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Person < ActiveRecord::Base
end

ransacker :array_users,
formatter: proc { |v| Person.first(2).map(&:id) } do |parent|
formatter: proc { |v| Person.where(id: v).map(&:id) } do |parent|
parent.table[:id]
end

Expand Down