Skip to content

Commit 98df2c5

Browse files
committed
Add tests on passing arrays to ransackers
Follow-up to PR #692.
1 parent e489ca7 commit 98df2c5

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

spec/ransack/adapters/active_record/base_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,26 @@ def self.simple_escaping?
314314

315315
context 'searching on an `in` predicate with a ransacker' do
316316
it 'should function correctly when passing an array of ids' do
317-
s = Person.ransack(array_users_in: [1, 2])
318-
expect(s.result.count).to be 2
317+
s = Person.ransack(array_people_ids_in: true)
318+
expect(s.result.count).to be > 0
319+
320+
s = Person.ransack(array_where_people_ids_in: [1, '2', 3])
321+
expect(s.result.count).to be 3
322+
expect(s.result.map(&:id)).to eq [3, 2, 1]
319323
end
320324

321325
it 'should function correctly when passing an array of strings' do
322-
Person.create!(name: Person.first.id.to_s)
323-
s = Person.ransack(array_names_in: true)
326+
a, b = Person.first.id.to_s, Person.second.id.to_s
327+
328+
Person.create!(name: a)
329+
s = Person.ransack(array_people_names_in: true)
324330
expect(s.result.count).to be > 0
331+
s = Person.ransack(array_where_people_names_in: a)
332+
expect(s.result.count).to be 1
333+
334+
Person.create!(name: b)
335+
s = Person.ransack(array_where_people_names_in: [a, b])
336+
expect(s.result.count).to be 2
325337
end
326338

327339
it 'should function correctly with an Arel SqlLiteral' do

spec/support/schema.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,26 @@ class Person < ActiveRecord::Base
6666
parent.table[:name]
6767
end
6868

69-
ransacker :array_users,
69+
ransacker :array_people_ids,
70+
formatter: proc { |v| Person.first(2).map(&:id) } do |parent|
71+
parent.table[:id]
72+
end
73+
74+
ransacker :array_where_people_ids,
7075
formatter: proc { |v| Person.where(id: v).map(&:id) } do |parent|
7176
parent.table[:id]
7277
end
7378

74-
ransacker :array_names,
79+
ransacker :array_people_names,
7580
formatter: proc { |v| Person.first(2).map { |p| p.id.to_s } } do |parent|
7681
parent.table[:name]
7782
end
7883

84+
ransacker :array_where_people_names,
85+
formatter: proc { |v| Person.where(id: v).map { |p| p.id.to_s } } do |parent|
86+
parent.table[:name]
87+
end
88+
7989
ransacker :doubled_name do |parent|
8090
Arel::Nodes::InfixOperation.new(
8191
'||', parent.table[:name], parent.table[:name]

0 commit comments

Comments
 (0)