Skip to content

Commit

Permalink
Merge pull request #488 from ideanl/master
Browse files Browse the repository at this point in the history
Allow string arrays to be received for custom ransacker.
  • Loading branch information
jonatack committed Jan 5, 2015
2 parents b7710a0 + 6dc6ebd commit 3e11e2b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ henceforth should be documented here.

### Fixed

* Fix passing in arrays for custom ransackers searching by id, to improve
compatibility with Rails 4.2 (Arel 6.0).
* Fix passing in arrays for custom ransackers, to improve compatibility
with Rails 4.2 (Arel 6.0).
([pull request](https://github.com/activerecord-hackery/ransack/pull/486))
([pull request](https://github.com/activerecord-hackery/ransack/pull/488))

*Idean Labib*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def arel_predicate
def return_predicate(predicate)
if casted_array_with_in_predicate?(predicate)
predicate.right[0] = predicate.right[0].val
predicate.right[0].map! { |v| v.is_a?(String) ? Arel::Nodes.build_quoted(v) : v }
end
predicate
end
Expand Down
14 changes: 11 additions & 3 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ def self.sane_adapter?
expect(s.result.to_a).to eq [p]
end

it "should function correctly when an array is passed into custom ransacker with _in" do
s = Person.search(array_users_in: true)
expect(s.result.length).to be > 0
context "when an array is passed into a custom ransacker with _in" do
it "should function correctly with ids." do
s = Person.search(array_users_in: true)
expect(s.result.length).to be > 0
end

it "should function correctly with strings." do
p = Person.create!(name: Person.first.id.to_s)
s = Person.search(array_names_in: true)
expect(s.result.length).to be > 0
end
end

it "should function correctly when an attribute name ends with '_start'" do
Expand Down
4 changes: 4 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Person < ActiveRecord::Base
parent.table[:id]
end

ransacker :array_names, formatter: proc { |v| Person.first(2).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 3e11e2b

Please sign in to comment.