Skip to content

Commit

Permalink
Don't replace 0 and 1 with false and true, fixes #502
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Kopley committed Jan 28, 2015
1 parent 76b4e38 commit 9f4e48e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

* When sanitizing scope arguments, do not replace 0 and 1 with false and true
cf. https://github.com/activerecord-hackery/ransack/issues/502

*Gabe Kopley*


## Version 1.6.3 - 2015-01-21

* Fix a regression
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ module Constants
STASHED_JOIN = 'stashed_join'.freeze
JOIN_NODE = 'join_node'.freeze

TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
TRUE_VALUES = [true, 't', 'T', 'true', 'TRUE'].to_set
FALSE_VALUES = [false, 'f', 'F', 'false', 'FALSE'].to_set
BOOLEAN_VALUES = (TRUE_VALUES + FALSE_VALUES).freeze

S_SORTS = %w(s sorts).freeze
Expand Down
20 changes: 19 additions & 1 deletion spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module ActiveRecord

context 'with scopes' do
before do
Person.stub :ransackable_scopes => [:active, :over_age, :of_age]
Person.stub :ransackable_scopes =>
[:active, :over_age, :of_age, :article_count_equals]
end

it "applies true scopes" do
Expand Down Expand Up @@ -68,6 +69,23 @@ module ActiveRecord
search.result.to_sql.should include "age > 18"
search.result.to_sql.should include "active = 1"
end

context "applying joins/group/having scope" do
it "applies scope correctly when input is 0" do
search = Person.ransack('article_count_equals' => 0)
search.result.to_sql.should include "HAVING count(articles.id) = 0"
end

it "applies scope correctly when input is 1" do
search = Person.ransack('article_count_equals' => 1)
search.result.to_sql.should include "HAVING count(articles.id) = 1"
end

it "applies scope correctly when input is 1337" do
search = Person.ransack('article_count_equals' => 1337)
search.result.to_sql.should include "HAVING count(articles.id) = 1337"
end
end
end

it 'does not raise exception for string :params argument' do
Expand Down
3 changes: 3 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Person < ActiveRecord::Base
scope :of_age, lambda { |of_age|
of_age ? where("age >= ?", 18) : where("age < ?", 18)
}
scope :article_count_equals, lambda { |n|
joins(:articles).group("people.id").having("count(articles.id) = ?", n)
}

ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent|
parent.table[:name]
Expand Down

0 comments on commit 9f4e48e

Please sign in to comment.