2011-03-31 00:31:39 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
module Adapters
|
|
|
|
module ActiveRecord
|
|
|
|
describe Base do
|
|
|
|
|
2011-07-17 23:40:07 +00:00
|
|
|
subject { ::ActiveRecord::Base }
|
2011-03-31 00:31:39 +00:00
|
|
|
|
2011-07-17 23:40:07 +00:00
|
|
|
it { should respond_to :ransack }
|
|
|
|
it { should respond_to :search }
|
2011-03-31 00:31:39 +00:00
|
|
|
|
|
|
|
describe '#search' do
|
2011-07-17 23:40:07 +00:00
|
|
|
subject { Person.search }
|
2011-03-31 00:31:39 +00:00
|
|
|
|
2011-07-17 23:40:07 +00:00
|
|
|
it { should be_a Search }
|
|
|
|
it 'has a Relation as its object' do
|
|
|
|
subject.object.should be_an ::ActiveRecord::Relation
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-10 00:55:28 +00:00
|
|
|
describe '#ransacker' do
|
2013-11-06 06:20:51 +00:00
|
|
|
# For infix tests
|
|
|
|
def self.sane_adapter?
|
|
|
|
case ::ActiveRecord::Base.connection.adapter_name
|
2013-11-13 00:51:12 +00:00
|
|
|
when "SQLite3", "PostgreSQL"
|
2013-11-06 06:20:51 +00:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2011-07-17 23:40:07 +00:00
|
|
|
# in schema.rb, class Person:
|
|
|
|
# ransacker :reversed_name, :formatter => proc {|v| v.reverse} do |parent|
|
|
|
|
# parent.table[:name]
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# ransacker :doubled_name do |parent|
|
|
|
|
# Arel::Nodes::InfixOperation.new('||', parent.table[:name], parent.table[:name])
|
|
|
|
# end
|
2011-04-10 00:55:28 +00:00
|
|
|
it 'creates ransack attributes' do
|
2011-04-11 16:04:31 +00:00
|
|
|
s = Person.search(:reversed_name_eq => 'htimS cirA')
|
|
|
|
s.result.should have(1).person
|
|
|
|
s.result.first.should eq Person.find_by_name('Aric Smith')
|
2011-04-10 00:55:28 +00:00
|
|
|
end
|
2011-04-11 16:59:26 +00:00
|
|
|
|
2011-07-17 23:40:07 +00:00
|
|
|
it 'can be accessed through associations' do
|
2011-04-11 16:59:26 +00:00
|
|
|
s = Person.search(:children_reversed_name_eq => 'htimS cirA')
|
2013-11-06 06:20:51 +00:00
|
|
|
|
|
|
|
s.result.to_sql.should match /#{quote_table_name("children_people")}.#{quote_column_name("name")} = 'Aric Smith'/
|
2011-04-11 16:59:26 +00:00
|
|
|
end
|
2011-04-11 18:39:40 +00:00
|
|
|
|
|
|
|
it 'allows an "attribute" to be an InfixOperation' do
|
|
|
|
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
|
|
|
|
s.result.first.should eq Person.find_by_name('Aric Smith')
|
2013-11-06 06:20:51 +00:00
|
|
|
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?
|
2012-08-22 23:17:45 +00:00
|
|
|
|
|
|
|
it "doesn't break #count if using InfixOperations" do
|
|
|
|
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
|
|
|
|
s.result.count.should eq 1
|
2013-11-06 06:20:51 +00:00
|
|
|
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?
|
|
|
|
|
|
|
|
it "should function correctly when using fields with dots in them" do
|
|
|
|
s = Person.search(email_cont: "example.com")
|
|
|
|
s.result.exists?.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should function correctly when using fields with % in them" do
|
|
|
|
Person.create!(name: "110%-er")
|
|
|
|
s = Person.search(name_cont: "10%")
|
|
|
|
s.result.exists?.should be_true
|
|
|
|
end
|
|
|
|
|
2013-12-03 13:28:58 +00:00
|
|
|
it "should function correctly when using fields with backslashes in them" do
|
2013-11-06 06:20:51 +00:00
|
|
|
Person.create!(name: "\\WINNER\\")
|
|
|
|
s = Person.search(name_cont: "\\WINNER\\")
|
|
|
|
s.result.exists?.should be_true
|
|
|
|
end
|
2013-10-24 15:56:45 +00:00
|
|
|
|
|
|
|
it 'allows sort by "only_sort" field' do
|
|
|
|
s = Person.search("s"=>{"0"=>{"dir"=>"asc", "name"=>"only_sort"}})
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should match /ORDER BY #{quote_table_name("people")}.#{quote_column_name("only_sort")} ASC/
|
2013-10-24 15:56:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sort by 'only_search' field" do
|
|
|
|
s = Person.search("s"=>{"0"=>{"dir"=>"asc", "name"=>"only_search"}})
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should_not match /ORDER BY #{quote_table_name("people")}.#{quote_column_name("only_search")} ASC/
|
2013-10-24 15:56:45 +00:00
|
|
|
end
|
|
|
|
|
2013-10-29 23:16:49 +00:00
|
|
|
it 'allows search by "only_search" field' do
|
|
|
|
s = Person.search(:only_search_eq => 'htimS cirA')
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should match /WHERE #{quote_table_name("people")}.#{quote_column_name("only_search")} = 'htimS cirA'/
|
2013-10-29 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2013-10-24 15:56:45 +00:00
|
|
|
it "can't be searched by 'only_sort'" do
|
|
|
|
s = Person.search(:only_sort_eq => 'htimS cirA')
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should_not match /WHERE #{quote_table_name("people")}.#{quote_column_name("only_sort")} = 'htimS cirA'/
|
2013-10-24 15:56:45 +00:00
|
|
|
end
|
2013-11-22 12:44:27 +00:00
|
|
|
|
|
|
|
it 'allows sort by "only_admin" field, if auth_object: :admin' do
|
|
|
|
s = Person.search({"s"=>{"0"=>{"dir"=>"asc", "name"=>"only_admin"}}}, {auth_object: :admin})
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should match /ORDER BY #{quote_table_name("people")}.#{quote_column_name("only_admin")} ASC/
|
2013-11-22 12:44:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sort by 'only_admin' field, if auth_object: nil" do
|
|
|
|
s = Person.search("s"=>{"0"=>{"dir"=>"asc", "name"=>"only_admin"}})
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should_not match /ORDER BY #{quote_table_name("people")}.#{quote_column_name("only_admin")} ASC/
|
2013-11-22 12:44:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows search by "only_admin" field, if auth_object: :admin' do
|
|
|
|
s = Person.search({:only_admin_eq => 'htimS cirA'}, {auth_object: :admin})
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should match /WHERE #{quote_table_name("people")}.#{quote_column_name("only_admin")} = 'htimS cirA'/
|
2013-11-22 12:44:27 +00:00
|
|
|
end
|
|
|
|
|
2013-11-26 13:15:18 +00:00
|
|
|
it "can't be searched by 'only_admin', if auth_object: nil" do
|
2013-11-22 12:44:27 +00:00
|
|
|
s = Person.search(:only_admin_eq => 'htimS cirA')
|
2013-11-26 13:15:18 +00:00
|
|
|
s.result.to_sql.should_not match /WHERE #{quote_table_name("people")}.#{quote_column_name("only_admin")} = 'htimS cirA'/
|
2013-11-22 12:44:27 +00:00
|
|
|
end
|
2011-04-10 00:55:28 +00:00
|
|
|
end
|
|
|
|
|
2011-07-17 23:40:07 +00:00
|
|
|
describe '#ransackable_attributes' do
|
2013-11-22 12:44:27 +00:00
|
|
|
context 'when auth_object is nil' do
|
|
|
|
subject { Person.ransackable_attributes }
|
|
|
|
|
|
|
|
it { should include 'name' }
|
|
|
|
it { should include 'reversed_name' }
|
|
|
|
it { should include 'doubled_name' }
|
|
|
|
it { should include 'only_search' }
|
|
|
|
it { should_not include 'only_sort' }
|
|
|
|
it { should_not include 'only_admin' }
|
|
|
|
end
|
2011-07-17 23:40:07 +00:00
|
|
|
|
2013-11-22 12:44:27 +00:00
|
|
|
context 'with auth_object :admin' do
|
|
|
|
subject { Person.ransackable_attributes(:admin) }
|
|
|
|
|
|
|
|
it { should include 'name' }
|
|
|
|
it { should include 'reversed_name' }
|
|
|
|
it { should include 'doubled_name' }
|
|
|
|
it { should include 'only_search' }
|
|
|
|
it { should_not include 'only_sort' }
|
|
|
|
it { should include 'only_admin' }
|
|
|
|
end
|
2011-07-17 23:40:07 +00:00
|
|
|
end
|
|
|
|
|
2013-02-20 03:54:55 +00:00
|
|
|
describe '#ransortable_attributes' do
|
2013-11-22 12:44:27 +00:00
|
|
|
context 'when auth_object is nil' do
|
|
|
|
subject { Person.ransortable_attributes }
|
|
|
|
|
|
|
|
it { should include 'name' }
|
|
|
|
it { should include 'reversed_name' }
|
|
|
|
it { should include 'doubled_name' }
|
|
|
|
it { should include 'only_sort' }
|
|
|
|
it { should_not include 'only_search' }
|
|
|
|
it { should_not include 'only_admin' }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with auth_object :admin' do
|
|
|
|
subject { Person.ransortable_attributes(:admin) }
|
2013-02-20 03:54:55 +00:00
|
|
|
|
2013-11-22 12:44:27 +00:00
|
|
|
it { should include 'name' }
|
|
|
|
it { should include 'reversed_name' }
|
|
|
|
it { should include 'doubled_name' }
|
|
|
|
it { should include 'only_sort' }
|
|
|
|
it { should_not include 'only_search' }
|
|
|
|
it { should include 'only_admin' }
|
|
|
|
end
|
2013-02-20 03:54:55 +00:00
|
|
|
end
|
|
|
|
|
2011-07-17 23:40:07 +00:00
|
|
|
describe '#ransackable_associations' do
|
|
|
|
subject { Person.ransackable_associations }
|
|
|
|
|
|
|
|
it { should include 'parent' }
|
|
|
|
it { should include 'children' }
|
|
|
|
it { should include 'articles' }
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-24 15:56:45 +00:00
|
|
|
end
|