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
|
2015-01-05 17:30:06 +00:00
|
|
|
subject { Person.ransack }
|
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
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(subject.object).to be_an ::ActiveRecord::Relation
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
2014-06-22 09:15:10 +00:00
|
|
|
|
|
|
|
context 'with scopes' do
|
|
|
|
before do
|
2015-07-26 19:24:45 +00:00
|
|
|
Person.stub :ransackable_scopes => [:active, :over_age, :of_age]
|
2014-06-22 09:15:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "applies true scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('active' => true)
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to (include 'active = 1')
|
2014-06-22 09:15:10 +00:00
|
|
|
end
|
|
|
|
|
2014-10-30 17:24:43 +00:00
|
|
|
it "applies stringy true scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('active' => 'true')
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to (include 'active = 1')
|
2014-10-30 17:24:43 +00:00
|
|
|
end
|
|
|
|
|
2014-10-31 15:53:37 +00:00
|
|
|
it "applies stringy boolean scopes with true value in an array" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('of_age' => ['true'])
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to (include 'age >= 18')
|
2014-10-31 15:53:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "applies stringy boolean scopes with false value in an array" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('of_age' => ['false'])
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to (include 'age < 18')
|
2014-10-31 15:53:37 +00:00
|
|
|
end
|
|
|
|
|
2014-06-22 09:15:10 +00:00
|
|
|
it "ignores unlisted scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('restricted' => true)
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to_not (include 'restricted')
|
2014-06-22 09:15:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores false scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('active' => false)
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).not_to (include 'active')
|
2014-06-22 09:15:10 +00:00
|
|
|
end
|
|
|
|
|
2014-10-30 17:24:43 +00:00
|
|
|
it "ignores stringy false scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('active' => 'false')
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to_not (include 'active')
|
2014-10-30 17:24:43 +00:00
|
|
|
end
|
|
|
|
|
2014-06-22 09:15:10 +00:00
|
|
|
it "passes values to scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('over_age' => 18)
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to (include 'age > 18')
|
2014-06-22 09:15:10 +00:00
|
|
|
end
|
|
|
|
|
2015-08-18 08:30:24 +00:00
|
|
|
# TODO: Implement a way to pass true/false values like 0 or 1 to
|
|
|
|
# scopes (e.g. with `in` / `not_in` predicates), without Ransack
|
|
|
|
# converting them to true/false boolean values instead.
|
|
|
|
|
|
|
|
# it 'passes true values to scopes', focus: true do
|
|
|
|
# s = Person.ransack('over_age' => 1)
|
|
|
|
# expect(s.result.to_sql).to (include 'age > 1')
|
|
|
|
# end
|
|
|
|
|
|
|
|
# it 'passes false values to scopes', focus: true do
|
|
|
|
# s = Person.ransack('over_age' => 0)
|
|
|
|
# expect(s.result.to_sql).to (include 'age > 0')
|
|
|
|
# end
|
|
|
|
|
2014-06-22 09:15:10 +00:00
|
|
|
it "chains scopes" do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack('over_age' => 18, 'active' => true)
|
2015-07-26 18:38:28 +00:00
|
|
|
expect(s.result.to_sql).to (include 'age > 18')
|
|
|
|
expect(s.result.to_sql).to (include 'active = 1')
|
2014-06-22 09:15:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 21:12:49 +00:00
|
|
|
it 'does not raise exception for string :params argument' do
|
2015-03-21 10:21:11 +00:00
|
|
|
expect { Person.ransack('') }.to_not raise_error
|
2014-08-26 21:12:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not modify the parameters' do
|
|
|
|
params = { :name_eq => '' }
|
2015-01-05 17:30:06 +00:00
|
|
|
expect { Person.ransack(params) }.not_to change { params }
|
2014-08-26 21:12:49 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
2011-04-10 00:55:28 +00:00
|
|
|
describe '#ransacker' do
|
2013-12-07 00:51:55 +00:00
|
|
|
# For infix tests
|
|
|
|
def self.sane_adapter?
|
|
|
|
case ::ActiveRecord::Base.connection.adapter_name
|
|
|
|
when "SQLite3", "PostgreSQL"
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2011-07-17 23:40:07 +00:00
|
|
|
# in schema.rb, class Person:
|
2013-08-04 13:13:41 +00:00
|
|
|
# ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent|
|
2011-07-17 23:40:07 +00:00
|
|
|
# parent.table[:name]
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# ransacker :doubled_name do |parent|
|
2014-04-27 08:58:09 +00:00
|
|
|
# Arel::Nodes::InfixOperation.new(
|
|
|
|
# '||', parent.table[:name], parent.table[:name]
|
|
|
|
# )
|
2011-07-17 23:40:07 +00:00
|
|
|
# end
|
2013-12-08 20:51:43 +00:00
|
|
|
|
2011-04-10 00:55:28 +00:00
|
|
|
it 'creates ransack attributes' do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:reversed_name_eq => 'htimS cirA')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.size).to eq(1)
|
2014-04-10 03:34:05 +00:00
|
|
|
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
|
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
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:children_reversed_name_eq => 'htimS cirA')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/#{quote_table_name("children_people")}.#{
|
|
|
|
quote_column_name("name")} = 'Aric Smith'/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
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
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:doubled_name_eq => 'Aric SmithAric Smith')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
|
2013-12-07 00:51:55 +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
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:doubled_name_eq => 'Aric SmithAric Smith')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.count).to eq 1
|
2013-12-07 00:51:55 +00:00
|
|
|
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?
|
2013-08-03 18:05:12 +00:00
|
|
|
|
2014-04-27 08:58:09 +00:00
|
|
|
it "should remove empty key value pairs from the params hash" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:children_reversed_name_eq => '')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).not_to match /LEFT OUTER JOIN/
|
2014-04-27 08:58:09 +00:00
|
|
|
end
|
2013-08-03 18:05:12 +00:00
|
|
|
|
2014-04-27 08:58:09 +00:00
|
|
|
it "should keep proper key value pairs in the params hash" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:children_reversed_name_eq => 'Testing')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).to match /LEFT OUTER JOIN/
|
2014-04-27 08:58:09 +00:00
|
|
|
end
|
2013-08-03 18:05:12 +00:00
|
|
|
|
2014-04-27 08:58:09 +00:00
|
|
|
it "should function correctly when nil is passed in" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(nil)
|
2013-08-03 18:05:12 +00:00
|
|
|
end
|
|
|
|
|
2014-05-13 05:06:53 +00:00
|
|
|
it "should function correctly when a blank string is passed in" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack('')
|
2014-05-13 05:06:53 +00:00
|
|
|
end
|
|
|
|
|
2015-01-21 12:03:29 +00:00
|
|
|
it "should function correctly with a multi-parameter attribute" do
|
2015-07-09 22:23:01 +00:00
|
|
|
::ActiveRecord::Base.default_timezone = :utc
|
|
|
|
Time.zone = 'UTC'
|
|
|
|
|
2015-01-21 12:03:29 +00:00
|
|
|
date = Date.current
|
|
|
|
s = Person.ransack(
|
|
|
|
{ "created_at_gteq(1i)" => date.year,
|
|
|
|
"created_at_gteq(2i)" => date.month,
|
|
|
|
"created_at_gteq(3i)" => date.day
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(s.result.to_sql).to match />=/
|
|
|
|
expect(s.result.to_sql).to match date.to_s
|
|
|
|
end
|
|
|
|
|
2013-12-07 00:51:55 +00:00
|
|
|
it "should function correctly when using fields with dots in them" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:email_cont => "example.com")
|
2014-07-24 14:51:18 +00:00
|
|
|
expect(s.result.exists?).to be true
|
2013-12-07 00:51:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should function correctly when using fields with % in them" do
|
2014-10-27 21:14:47 +00:00
|
|
|
p = Person.create!(:name => "110%-er")
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:name_cont => "10%")
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
2013-12-07 00:51:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should function correctly when using fields with backslashes in them" do
|
2014-10-27 21:14:47 +00:00
|
|
|
p = Person.create!(:name => "\\WINNER\\")
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:name_cont => "\\WINNER\\")
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
end
|
|
|
|
|
2015-09-08 13:03:15 +00:00
|
|
|
context 'searching by underscores' do
|
|
|
|
# when escaping is supported right in LIKE expression without adding extra expressions
|
|
|
|
def self.simple_escaping?
|
|
|
|
case ::ActiveRecord::Base.connection.adapter_name
|
|
|
|
when "Mysql2", "PostgreSQL"
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should search correctly if matches exist" do
|
|
|
|
p = Person.create!(:name => "name_with_underscore")
|
|
|
|
s = Person.ransack(:name_cont => "name_")
|
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
end if simple_escaping?
|
|
|
|
|
|
|
|
it "should return empty result if no matches" do
|
|
|
|
Person.create!(:name => "name_with_underscore")
|
|
|
|
s = Person.ransack(:name_cont => "n_")
|
|
|
|
expect(s.result.to_a).to eq []
|
|
|
|
end if simple_escaping?
|
|
|
|
end
|
|
|
|
|
2015-01-30 23:33:28 +00:00
|
|
|
context "searching on an `in` predicate with a ransacker" do
|
2015-01-05 17:25:32 +00:00
|
|
|
it "should function correctly when passing an array of ids" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(array_users_in: true)
|
2015-01-14 11:50:40 +00:00
|
|
|
expect(s.result.count).to be > 0
|
2015-01-05 00:26:09 +00:00
|
|
|
end
|
|
|
|
|
2015-01-05 17:25:32 +00:00
|
|
|
it "should function correctly when passing an array of strings" do
|
2015-02-02 15:54:46 +00:00
|
|
|
Person.create!(name: Person.first.id.to_s)
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(array_names_in: true)
|
2015-01-14 11:50:40 +00:00
|
|
|
expect(s.result.count).to be > 0
|
2015-01-05 00:26:09 +00:00
|
|
|
end
|
2015-01-30 23:33:28 +00:00
|
|
|
|
|
|
|
it 'should function correctly with an Arel SqlLiteral' do
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack(sql_literal_id_in: 1)
|
2015-01-30 23:33:28 +00:00
|
|
|
expect(s.result.count).to be 1
|
2015-02-02 15:54:06 +00:00
|
|
|
s = Person.ransack(sql_literal_id_in: ['2', 4, '5', 8])
|
2015-01-30 23:33:28 +00:00
|
|
|
expect(s.result.count).to be 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "search on an `in` predicate with an array" do
|
|
|
|
it "should function correctly when passing an array of ids" do
|
|
|
|
array = Person.all.map(&:id)
|
|
|
|
s = Person.ransack(id_in: array)
|
|
|
|
expect(s.result.count).to eq array.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-27 21:14:47 +00:00
|
|
|
it "should function correctly when an attribute name ends with '_start'" do
|
|
|
|
p = Person.create!(:new_start => 'Bar and foo', :name => 'Xiang')
|
|
|
|
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:new_start_end => ' and foo')
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:name_or_new_start_start => 'Xia')
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:new_start_or_name_end => 'iang')
|
2014-10-27 21:14:47 +00:00
|
|
|
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')
|
|
|
|
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:stop_end_start => 'Foo and')
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:stop_end_or_name_end => 'anne')
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:name_or_stop_end_end => ' bar')
|
2014-10-27 21:14:47 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should function correctly when an attribute name has 'and' in it" do
|
2014-10-30 19:19:05 +00:00
|
|
|
p = Person.create!(:terms_and_conditions => true)
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:terms_and_conditions_eq => true)
|
2015-07-12 05:58:38 +00:00
|
|
|
expect(s.result.to_a).to eq [p]
|
2013-12-07 00:51:55 +00:00
|
|
|
end
|
|
|
|
|
2015-09-23 14:03:04 +00:00
|
|
|
it "should translate attribute aliased column names",
|
|
|
|
:if => Ransack::SUPPORTS_ATTRIBUTE_ALIAS do
|
|
|
|
s = Person.ransack(:full_name_eq => 'Nicolas Cage')
|
|
|
|
expect(s.result.to_sql).to match(
|
|
|
|
/WHERE #{quote_table_name("people")}.#{quote_column_name("name")}/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-12-07 00:51:55 +00:00
|
|
|
it 'allows sort by "only_sort" field' do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(
|
2013-12-07 00:51:55 +00:00
|
|
|
"s" => { "0" => { "dir" => "asc", "name" => "only_sort" } }
|
|
|
|
)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/ORDER BY #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_sort")} ASC/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sort by 'only_search' field" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(
|
2013-12-07 00:51:55 +00:00
|
|
|
"s" => { "0" => { "dir" => "asc", "name" => "only_search" } }
|
|
|
|
)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).not_to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/ORDER BY #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_search")} ASC/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows search by "only_search" field' do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:only_search_eq => 'htimS cirA')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/WHERE #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_search")} = 'htimS cirA'/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't be searched by 'only_sort'" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:only_sort_eq => 'htimS cirA')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).not_to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/WHERE #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_sort")} = 'htimS cirA'/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows sort by "only_admin" field, if auth_object: :admin' do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(
|
2013-12-07 00:51:55 +00:00
|
|
|
{ "s" => { "0" => { "dir" => "asc", "name" => "only_admin" } } },
|
|
|
|
{ auth_object: :admin }
|
|
|
|
)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/ORDER BY #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_admin")} ASC/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't sort by 'only_admin' field, if auth_object: nil" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(
|
2013-12-07 00:51:55 +00:00
|
|
|
"s" => { "0" => { "dir" => "asc", "name" => "only_admin" } }
|
|
|
|
)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).not_to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/ORDER BY #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_admin")} ASC/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows search by "only_admin" field, if auth_object: :admin' do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(
|
2014-05-01 13:55:39 +00:00
|
|
|
{ :only_admin_eq => 'htimS cirA' },
|
|
|
|
{ :auth_object => :admin }
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/WHERE #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_admin")} = 'htimS cirA'/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't be searched by 'only_admin', if auth_object: nil" do
|
2015-01-05 17:30:06 +00:00
|
|
|
s = Person.ransack(:only_admin_eq => 'htimS cirA')
|
2014-05-15 18:29:46 +00:00
|
|
|
expect(s.result.to_sql).not_to match(
|
2014-04-27 08:58:09 +00:00
|
|
|
/WHERE #{quote_table_name("people")}.#{
|
|
|
|
quote_column_name("only_admin")} = 'htimS cirA'/
|
2013-12-07 00:51:55 +00:00
|
|
|
)
|
|
|
|
end
|
2015-02-17 16:31:38 +00:00
|
|
|
|
2015-03-12 05:56:32 +00:00
|
|
|
it 'should allow passing ransacker arguments to a ransacker' do
|
2015-02-17 16:31:38 +00:00
|
|
|
s = Person.ransack(
|
|
|
|
c: [{
|
|
|
|
a: {
|
|
|
|
'0' => {
|
2015-03-21 10:19:25 +00:00
|
|
|
name: 'with_arguments', ransacker_args: [10, 100]
|
2015-02-17 16:31:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
p: 'cont',
|
2015-03-21 10:19:25 +00:00
|
|
|
v: ['Passing arguments to ransackers!']
|
2015-02-17 16:31:38 +00:00
|
|
|
}]
|
|
|
|
)
|
|
|
|
expect(s.result.to_sql).to match(
|
2015-03-21 10:19:25 +00:00
|
|
|
/LENGTH\(articles.body\) BETWEEN 10 AND 100/
|
2015-02-17 16:31:38 +00:00
|
|
|
)
|
|
|
|
expect(s.result.to_sql).to match(
|
2015-03-21 10:19:25 +00:00
|
|
|
/LIKE \'\%Passing arguments to ransackers!\%\'/
|
|
|
|
)
|
|
|
|
expect { s.result.first }.to_not raise_error
|
2015-02-17 16:31:38 +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-12-07 00:51:55 +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' }
|
2015-09-23 14:03:04 +00:00
|
|
|
|
|
|
|
if Ransack::SUPPORTS_ATTRIBUTE_ALIAS
|
|
|
|
it { should include 'full_name' }
|
|
|
|
end
|
2013-12-07 00:51:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with auth_object :admin' do
|
|
|
|
subject { Person.ransackable_attributes(:admin) }
|
2011-07-17 23:40:07 +00:00
|
|
|
|
2013-12-07 00:51:55 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ransortable_attributes' do
|
|
|
|
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) }
|
|
|
|
|
|
|
|
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
|
2011-07-17 23:40:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ransackable_associations' do
|
|
|
|
subject { Person.ransackable_associations }
|
|
|
|
|
|
|
|
it { should include 'parent' }
|
|
|
|
it { should include 'children' }
|
|
|
|
it { should include 'articles' }
|
|
|
|
end
|
|
|
|
|
2014-06-22 09:15:10 +00:00
|
|
|
describe '#ransackable_scopes' do
|
|
|
|
subject { Person.ransackable_scopes }
|
|
|
|
|
|
|
|
it { should eq [] }
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-24 15:56:45 +00:00
|
|
|
end
|