mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
125 lines
3.8 KiB
Ruby
125 lines
3.8 KiB
Ruby
require 'spec_helper'
|
|
|
|
module Ransack
|
|
describe Configuration do
|
|
it 'yields Ransack on configure' do
|
|
Ransack.configure { |config| expect(config).to eq Ransack }
|
|
end
|
|
|
|
it 'adds predicates' do
|
|
Ransack.configure do |config|
|
|
config.add_predicate :test_predicate
|
|
end
|
|
|
|
expect(Ransack.predicates).to have_key 'test_predicate'
|
|
expect(Ransack.predicates).to have_key 'test_predicate_any'
|
|
expect(Ransack.predicates).to have_key 'test_predicate_all'
|
|
end
|
|
|
|
it 'avoids creating compound predicates if compounds: false' do
|
|
Ransack.configure do |config|
|
|
config.add_predicate(
|
|
:test_predicate_without_compound,
|
|
:compounds => false
|
|
)
|
|
end
|
|
expect(Ransack.predicates)
|
|
.to have_key 'test_predicate_without_compound'
|
|
expect(Ransack.predicates)
|
|
.not_to have_key 'test_predicate_without_compound_any'
|
|
expect(Ransack.predicates)
|
|
.not_to have_key 'test_predicate_without_compound_all'
|
|
end
|
|
|
|
it 'should have default value for search key' do
|
|
expect(Ransack.options[:search_key]).to eq :q
|
|
end
|
|
|
|
it 'changes default search key parameter' do
|
|
# store original state so we can restore it later
|
|
before = Ransack.options.clone
|
|
|
|
Ransack.configure do |config|
|
|
config.search_key = :query
|
|
end
|
|
|
|
expect(Ransack.options[:search_key]).to eq :query
|
|
|
|
# restore original state so we don't break other tests
|
|
Ransack.options = before
|
|
end
|
|
|
|
it 'should have default values for arrows' do
|
|
expect(Ransack.options[:up_arrow]).to eq '▼'
|
|
expect(Ransack.options[:down_arrow]).to eq '▲'
|
|
end
|
|
|
|
it 'changes default arrow strings' do
|
|
before = Ransack.options.clone
|
|
up_value, down_value = '<i class="fa fa-long-arrow-up"></i>', 'U+02193'
|
|
|
|
Ransack.configure do |config|
|
|
config.custom_arrows = { up_arrow: up_value, down_arrow: down_value }
|
|
end
|
|
|
|
expect(Ransack.options[:up_arrow]).to eq up_value
|
|
expect(Ransack.options[:down_arrow]).to eq down_value
|
|
|
|
Ransack.options = before
|
|
end
|
|
|
|
it 'adds predicates that take arrays, overriding compounds' do
|
|
Ransack.configure do |config|
|
|
config.add_predicate(
|
|
:test_array_predicate,
|
|
:wants_array => true,
|
|
:compounds => true
|
|
)
|
|
end
|
|
|
|
expect(Ransack.predicates['test_array_predicate'].wants_array).to eq true
|
|
expect(Ransack.predicates).not_to have_key 'test_array_predicate_any'
|
|
expect(Ransack.predicates).not_to have_key 'test_array_predicate_all'
|
|
end
|
|
|
|
describe '`wants_array` option takes precedence over Arel predicate' do
|
|
it 'implicitly wants an array for in/not in predicates' do
|
|
Ransack.configure do |config|
|
|
config.add_predicate(
|
|
:test_in_predicate,
|
|
:arel_predicate => 'in'
|
|
)
|
|
config.add_predicate(
|
|
:test_not_in_predicate,
|
|
:arel_predicate => 'not_in'
|
|
)
|
|
end
|
|
|
|
expect(Ransack.predicates['test_in_predicate'].wants_array)
|
|
.to eq true
|
|
expect(Ransack.predicates['test_not_in_predicate'].wants_array)
|
|
.to eq true
|
|
end
|
|
|
|
it 'explicitly does not want array for in/not_in predicates' do
|
|
Ransack.configure do |config|
|
|
config.add_predicate(
|
|
:test_in_predicate_no_array,
|
|
:arel_predicate => 'in',
|
|
:wants_array => false
|
|
)
|
|
config.add_predicate(
|
|
:test_not_in_predicate_no_array,
|
|
:arel_predicate => 'not_in',
|
|
:wants_array => false
|
|
)
|
|
end
|
|
|
|
expect(Ransack.predicates['test_in_predicate_no_array'].wants_array)
|
|
.to eq false
|
|
expect(Ransack.predicates['test_not_in_predicate_no_array'].wants_array)
|
|
.to eq false
|
|
end
|
|
end
|
|
end
|
|
end
|