Add tests on passing arrays to ransackers

Follow-up to PR #692.
This commit is contained in:
Jon Atack 2016-07-23 14:27:31 +02:00
parent e489ca7789
commit 98df2c5b52
2 changed files with 28 additions and 6 deletions

View File

@ -314,14 +314,26 @@ module Ransack
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: [1, 2])
expect(s.result.count).to be 2
s = Person.ransack(array_people_ids_in: true)
expect(s.result.count).to be > 0
s = Person.ransack(array_where_people_ids_in: [1, '2', 3])
expect(s.result.count).to be 3
expect(s.result.map(&:id)).to eq [3, 2, 1]
end
it 'should function correctly when passing an array of strings' do
Person.create!(name: Person.first.id.to_s)
s = Person.ransack(array_names_in: true)
a, b = Person.first.id.to_s, Person.second.id.to_s
Person.create!(name: a)
s = Person.ransack(array_people_names_in: true)
expect(s.result.count).to be > 0
s = Person.ransack(array_where_people_names_in: a)
expect(s.result.count).to be 1
Person.create!(name: b)
s = Person.ransack(array_where_people_names_in: [a, b])
expect(s.result.count).to be 2
end
it 'should function correctly with an Arel SqlLiteral' do

View File

@ -66,16 +66,26 @@ class Person < ActiveRecord::Base
parent.table[:name]
end
ransacker :array_users,
ransacker :array_people_ids,
formatter: proc { |v| Person.first(2).map(&:id) } do |parent|
parent.table[:id]
end
ransacker :array_where_people_ids,
formatter: proc { |v| Person.where(id: v).map(&:id) } do |parent|
parent.table[:id]
end
ransacker :array_names,
ransacker :array_people_names,
formatter: proc { |v| Person.first(2).map { |p| p.id.to_s } } do |parent|
parent.table[:name]
end
ransacker :array_where_people_names,
formatter: proc { |v| Person.where(id: v).map { |p| p.id.to_s } } do |parent|
parent.table[:name]
end
ransacker :doubled_name do |parent|
Arel::Nodes::InfixOperation.new(
'||', parent.table[:name], parent.table[:name]