Skip to content

Commit

Permalink
Add tests on passing arrays to ransackers
Browse files Browse the repository at this point in the history
Follow-up to PR #692.
  • Loading branch information
jonatack committed Jul 23, 2016
1 parent e489ca7 commit 98df2c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
20 changes: 16 additions & 4 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,26 @@ 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: [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
Expand Down
14 changes: 12 additions & 2 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 98df2c5

Please sign in to comment.