Skip to content

Commit

Permalink
Use #ransack instead of #search for future deprecation
Browse files Browse the repository at this point in the history
of #search default alias in Ransack 2.0 to fix reported issues of
method name conflicts with other gems.

[skip ci]
  • Loading branch information
Jon Atack committed Jan 5, 2015
1 parent ceefca1 commit 7d68372
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ActiveRecord
it { should respond_to :search }

describe '#search' do
subject { Person.search }
subject { Person.ransack }

it { should be_a Search }
it 'has a Relation as its object' do
Expand All @@ -24,59 +24,59 @@ module ActiveRecord
end

it "applies true scopes" do
search = Person.search('active' => true)
search = Person.ransack('active' => true)
search.result.to_sql.should include "active = 1"
end

it "applies stringy true scopes" do
search = Person.search('active' => 'true')
search = Person.ransack('active' => 'true')
search.result.to_sql.should include "active = 1"
end

it "applies stringy boolean scopes with true value in an array" do
search = Person.search('of_age' => ['true'])
search = Person.ransack('of_age' => ['true'])
search.result.to_sql.should include "age >= 18"
end

it "applies stringy boolean scopes with false value in an array" do
search = Person.search('of_age' => ['false'])
search = Person.ransack('of_age' => ['false'])
search.result.to_sql.should include "age < 18"
end

it "ignores unlisted scopes" do
search = Person.search('restricted' => true)
search = Person.ransack('restricted' => true)
search.result.to_sql.should_not include "restricted"
end

it "ignores false scopes" do
search = Person.search('active' => false)
search = Person.ransack('active' => false)
search.result.to_sql.should_not include "active"
end

it "ignores stringy false scopes" do
search = Person.search('active' => 'false')
search = Person.ransack('active' => 'false')
search.result.to_sql.should_not include "active"
end

it "passes values to scopes" do
search = Person.search('over_age' => 18)
search = Person.ransack('over_age' => 18)
search.result.to_sql.should include "age > 18"
end

it "chains scopes" do
search = Person.search('over_age' => 18, 'active' => true)
search = Person.ransack('over_age' => 18, 'active' => true)
search.result.to_sql.should include "age > 18"
search.result.to_sql.should include "active = 1"
end
end

it 'does not raise exception for string :params argument' do
lambda { Person.search('') }.should_not raise_error
lambda { Person.ransack('') }.should_not raise_error
end

it 'does not modify the parameters' do
params = { :name_eq => '' }
expect { Person.search(params) }.not_to change { params }
expect { Person.ransack(params) }.not_to change { params }
end
end

Expand All @@ -102,108 +102,108 @@ def self.sane_adapter?
# end

it 'creates ransack attributes' do
s = Person.search(:reversed_name_eq => 'htimS cirA')
s = Person.ransack(:reversed_name_eq => 'htimS cirA')
expect(s.result.size).to eq(1)

expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
end

it 'can be accessed through associations' do
s = Person.search(:children_reversed_name_eq => 'htimS cirA')
s = Person.ransack(:children_reversed_name_eq => 'htimS cirA')
expect(s.result.to_sql).to match(
/#{quote_table_name("children_people")}.#{
quote_column_name("name")} = 'Aric Smith'/
)
end

it 'allows an "attribute" to be an InfixOperation' do
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
s = Person.ransack(:doubled_name_eq => 'Aric SmithAric Smith')
expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?

it "doesn't break #count if using InfixOperations" do
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
s = Person.ransack(:doubled_name_eq => 'Aric SmithAric Smith')
expect(s.result.count).to eq 1
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?

it "should remove empty key value pairs from the params hash" do
s = Person.search(:children_reversed_name_eq => '')
s = Person.ransack(:children_reversed_name_eq => '')
expect(s.result.to_sql).not_to match /LEFT OUTER JOIN/
end

it "should keep proper key value pairs in the params hash" do
s = Person.search(:children_reversed_name_eq => 'Testing')
s = Person.ransack(:children_reversed_name_eq => 'Testing')
expect(s.result.to_sql).to match /LEFT OUTER JOIN/
end

it "should function correctly when nil is passed in" do
s = Person.search(nil)
s = Person.ransack(nil)
end

it "should function correctly when a blank string is passed in" do
s = Person.search('')
s = Person.ransack('')
end

it "should function correctly when using fields with dots in them" do
s = Person.search(:email_cont => "example.com")
s = Person.ransack(:email_cont => "example.com")
expect(s.result.exists?).to be true
end

it "should function correctly when using fields with % in them" do
p = Person.create!(:name => "110%-er")
s = Person.search(:name_cont => "10%")
s = Person.ransack(:name_cont => "10%")
expect(s.result.to_a).to eq [p]
end

it "should function correctly when using fields with backslashes in them" do
p = Person.create!(:name => "\\WINNER\\")
s = Person.search(:name_cont => "\\WINNER\\")
s = Person.ransack(:name_cont => "\\WINNER\\")
expect(s.result.to_a).to eq [p]
end

context "search using an `in` predicate and an array passed to a ransacker" do
it "should function correctly when passing an array of ids" do
s = Person.search(array_users_in: true)
s = Person.ransack(array_users_in: true)
expect(s.result.length).to be > 0
end

it "should function correctly when passing an array of strings" do
p = Person.create!(name: Person.first.id.to_s)
s = Person.search(array_names_in: true)
s = Person.ransack(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
p = Person.create!(:new_start => 'Bar and foo', :name => 'Xiang')

s = Person.search(:new_start_end => ' and foo')
s = Person.ransack(:new_start_end => ' and foo')
expect(s.result.to_a).to eq [p]

s = Person.search(:name_or_new_start_start => 'Xia')
s = Person.ransack(:name_or_new_start_start => 'Xia')
expect(s.result.to_a).to eq [p]

s = Person.search(:new_start_or_name_end => 'iang')
s = Person.ransack(:new_start_or_name_end => 'iang')
expect(s.result.to_a).to eq [p]
end

it "should function correctly when an attribute name ends with '_end'" do
p = Person.create!(:stop_end => 'Foo and bar', :name => 'Marianne')

s = Person.search(:stop_end_start => 'Foo and')
s = Person.ransack(:stop_end_start => 'Foo and')
expect(s.result.to_a).to eq [p]

s = Person.search(:stop_end_or_name_end => 'anne')
s = Person.ransack(:stop_end_or_name_end => 'anne')
expect(s.result.to_a).to eq [p]

s = Person.search(:name_or_stop_end_end => ' bar')
s = Person.ransack(:name_or_stop_end_end => ' bar')
expect(s.result.to_a).to eq [p]
end

it "should function correctly when an attribute name has 'and' in it" do
# FIXME: this test does not pass!
p = Person.create!(:terms_and_conditions => true)
s = Person.search(:terms_and_conditions_eq => true)
s = Person.ransack(:terms_and_conditions_eq => true)
# search is not detecting the attribute
puts "
FIXME: Search not detecting the `terms_and_conditions` attribute in
Expand All @@ -212,7 +212,7 @@ def self.sane_adapter?
end

it 'allows sort by "only_sort" field' do
s = Person.search(
s = Person.ransack(
"s" => { "0" => { "dir" => "asc", "name" => "only_sort" } }
)
expect(s.result.to_sql).to match(
Expand All @@ -222,7 +222,7 @@ def self.sane_adapter?
end

it "doesn't sort by 'only_search' field" do
s = Person.search(
s = Person.ransack(
"s" => { "0" => { "dir" => "asc", "name" => "only_search" } }
)
expect(s.result.to_sql).not_to match(
Expand All @@ -232,23 +232,23 @@ def self.sane_adapter?
end

it 'allows search by "only_search" field' do
s = Person.search(:only_search_eq => 'htimS cirA')
s = Person.ransack(:only_search_eq => 'htimS cirA')
expect(s.result.to_sql).to match(
/WHERE #{quote_table_name("people")}.#{
quote_column_name("only_search")} = 'htimS cirA'/
)
end

it "can't be searched by 'only_sort'" do
s = Person.search(:only_sort_eq => 'htimS cirA')
s = Person.ransack(:only_sort_eq => 'htimS cirA')
expect(s.result.to_sql).not_to match(
/WHERE #{quote_table_name("people")}.#{
quote_column_name("only_sort")} = 'htimS cirA'/
)
end

it 'allows sort by "only_admin" field, if auth_object: :admin' do
s = Person.search(
s = Person.ransack(
{ "s" => { "0" => { "dir" => "asc", "name" => "only_admin" } } },
{ auth_object: :admin }
)
Expand All @@ -259,7 +259,7 @@ def self.sane_adapter?
end

it "doesn't sort by 'only_admin' field, if auth_object: nil" do
s = Person.search(
s = Person.ransack(
"s" => { "0" => { "dir" => "asc", "name" => "only_admin" } }
)
expect(s.result.to_sql).not_to match(
Expand All @@ -269,7 +269,7 @@ def self.sane_adapter?
end

it 'allows search by "only_admin" field, if auth_object: :admin' do
s = Person.search(
s = Person.ransack(
{ :only_admin_eq => 'htimS cirA' },
{ :auth_object => :admin }
)
Expand All @@ -280,7 +280,7 @@ def self.sane_adapter?
end

it "can't be searched by 'only_admin', if auth_object: nil" do
s = Person.search(:only_admin_eq => 'htimS cirA')
s = Person.ransack(:only_admin_eq => 'htimS cirA')
expect(s.result.to_sql).not_to match(
/WHERE #{quote_table_name("people")}.#{
quote_column_name("only_admin")} = 'htimS cirA'/
Expand Down

1 comment on commit 7d68372

@jonatack
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to issue #369.

Please sign in to comment.