Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test cases for when 0 and 1 are passed to non-boolean scopes #509

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ module ActiveRecord

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

it "applies true scopes" do
s = Person.ransack('active' => true)
Expand Down Expand Up @@ -68,6 +69,23 @@ module ActiveRecord
s.result.to_sql.should include "age > 18"
s.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