2011-03-30 20:31:39 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
describe Configuration do
|
2011-06-04 18:01:12 -04:00
|
|
|
it 'yields Ransack on configure' do
|
2016-09-29 10:05:58 -04:00
|
|
|
Ransack.configure { |config| expect(config).to eq Ransack }
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2011-07-17 19:04:00 -04:00
|
|
|
|
|
|
|
it 'adds predicates' do
|
|
|
|
Ransack.configure do |config|
|
|
|
|
config.add_predicate :test_predicate
|
|
|
|
end
|
|
|
|
|
2014-05-15 14:29:46 -04:00
|
|
|
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'
|
2011-07-17 19:04:00 -04:00
|
|
|
end
|
|
|
|
|
2013-08-04 09:13:41 -04:00
|
|
|
it 'avoids creating compound predicates if compounds: false' do
|
2011-07-17 19:04:00 -04:00
|
|
|
Ransack.configure do |config|
|
2013-12-15 13:27:50 -05:00
|
|
|
config.add_predicate(
|
|
|
|
:test_predicate_without_compound,
|
2014-05-01 09:55:39 -04:00
|
|
|
:compounds => false
|
2013-12-15 13:27:50 -05:00
|
|
|
)
|
2011-07-17 19:04:00 -04:00
|
|
|
end
|
2014-05-15 14:29:46 -04:00
|
|
|
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'
|
2011-07-17 19:04:00 -04:00
|
|
|
end
|
2012-03-07 14:31:13 -05:00
|
|
|
|
|
|
|
it 'should have default value for search key' do
|
2014-05-15 14:29:46 -04:00
|
|
|
expect(Ransack.options[:search_key]).to eq :q
|
2012-03-07 14:31:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes default search key parameter' do
|
2016-09-30 13:40:12 -04:00
|
|
|
default = Ransack.options.clone
|
2012-03-07 14:31:13 -05:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
Ransack.configure { |c| c.search_key = :query }
|
2012-03-07 14:31:13 -05:00
|
|
|
|
2014-05-15 14:29:46 -04:00
|
|
|
expect(Ransack.options[:search_key]).to eq :query
|
2012-03-07 14:31:13 -05:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
Ransack.options = default
|
2012-03-07 14:31:13 -05:00
|
|
|
end
|
2012-05-02 02:54:12 -04:00
|
|
|
|
2016-09-27 15:01:06 -04:00
|
|
|
it 'should have default values for arrows' do
|
2016-09-29 09:45:25 -04:00
|
|
|
expect(Ransack.options[:up_arrow]).to eq '▼'
|
|
|
|
expect(Ransack.options[:down_arrow]).to eq '▲'
|
2017-07-12 06:57:11 -04:00
|
|
|
expect(Ransack.options[:default_arrow]).to eq nil
|
2016-09-27 15:01:06 -04:00
|
|
|
end
|
|
|
|
|
2016-09-29 10:32:51 -04:00
|
|
|
it 'changes the default value for the up arrow only' do
|
2016-09-30 13:40:12 -04:00
|
|
|
default, new_up_arrow = Ransack.options.clone, 'U+02191'
|
2016-09-29 10:32:51 -04:00
|
|
|
|
2016-09-30 13:14:21 -04:00
|
|
|
Ransack.configure { |c| c.custom_arrows = { up_arrow: new_up_arrow } }
|
2016-09-29 10:32:51 -04:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
expect(Ransack.options[:down_arrow]).to eq default[:down_arrow]
|
2016-09-30 13:14:21 -04:00
|
|
|
expect(Ransack.options[:up_arrow]).to eq new_up_arrow
|
2016-09-29 10:32:51 -04:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
Ransack.options = default
|
2016-09-29 10:32:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes the default value for the down arrow only' do
|
2016-09-30 13:40:12 -04:00
|
|
|
default, new_down_arrow = Ransack.options.clone, '<i class="down"></i>'
|
2016-09-29 10:32:51 -04:00
|
|
|
|
2016-09-30 13:14:21 -04:00
|
|
|
Ransack.configure { |c| c.custom_arrows = { down_arrow: new_down_arrow } }
|
2016-09-29 10:32:51 -04:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
expect(Ransack.options[:up_arrow]).to eq default[:up_arrow]
|
2016-09-30 13:14:21 -04:00
|
|
|
expect(Ransack.options[:down_arrow]).to eq new_down_arrow
|
2016-09-29 10:32:51 -04:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
Ransack.options = default
|
2016-09-29 10:32:51 -04:00
|
|
|
end
|
|
|
|
|
2017-07-12 06:57:11 -04:00
|
|
|
it 'changes the default value for the default arrow only' do
|
|
|
|
default, new_default_arrow = Ransack.options.clone, '<i class="default"></i>'
|
|
|
|
|
|
|
|
Ransack.configure { |c| c.custom_arrows = { default_arrow: new_default_arrow } }
|
|
|
|
|
|
|
|
expect(Ransack.options[:up_arrow]).to eq default[:up_arrow]
|
|
|
|
expect(Ransack.options[:down_arrow]).to eq default[:down_arrow]
|
|
|
|
expect(Ransack.options[:default_arrow]).to eq new_default_arrow
|
|
|
|
|
|
|
|
Ransack.options = default
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes the default value for all arrows' do
|
2016-09-30 13:40:12 -04:00
|
|
|
default = Ransack.options.clone
|
|
|
|
new_up_arrow = '<i class="fa fa-long-arrow-up"></i>'
|
|
|
|
new_down_arrow = 'U+02193'
|
2017-07-12 06:57:11 -04:00
|
|
|
new_default_arrow = 'defaultarrow'
|
2016-09-27 15:01:06 -04:00
|
|
|
|
2016-09-30 13:14:21 -04:00
|
|
|
Ransack.configure do |c|
|
2017-07-12 06:57:11 -04:00
|
|
|
c.custom_arrows = { up_arrow: new_up_arrow, down_arrow: new_down_arrow, default_arrow: new_default_arrow }
|
2016-09-27 15:01:06 -04:00
|
|
|
end
|
|
|
|
|
2016-09-30 13:14:21 -04:00
|
|
|
expect(Ransack.options[:up_arrow]).to eq new_up_arrow
|
|
|
|
expect(Ransack.options[:down_arrow]).to eq new_down_arrow
|
2017-07-12 06:57:11 -04:00
|
|
|
expect(Ransack.options[:default_arrow]).to eq new_default_arrow
|
2016-09-27 15:01:06 -04:00
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
Ransack.options = default
|
2016-09-27 15:01:06 -04:00
|
|
|
end
|
|
|
|
|
2016-09-30 13:14:21 -04:00
|
|
|
it 'consecutive arrow customizations respect previous customizations' do
|
2016-09-30 13:40:12 -04:00
|
|
|
default = Ransack.options.clone
|
2016-09-30 12:44:24 -04:00
|
|
|
|
|
|
|
Ransack.configure { |c| c.custom_arrows = { up_arrow: 'up' } }
|
2016-09-30 13:40:12 -04:00
|
|
|
expect(Ransack.options[:down_arrow]).to eq default[:down_arrow]
|
2016-09-30 12:44:24 -04:00
|
|
|
|
|
|
|
Ransack.configure { |c| c.custom_arrows = { down_arrow: 'DOWN' } }
|
|
|
|
expect(Ransack.options[:up_arrow]).to eq 'up'
|
|
|
|
|
|
|
|
Ransack.configure { |c| c.custom_arrows = { up_arrow: '<i>U-Arrow</i>' } }
|
|
|
|
expect(Ransack.options[:down_arrow]).to eq 'DOWN'
|
|
|
|
|
|
|
|
Ransack.configure { |c| c.custom_arrows = { down_arrow: 'down arrow-2' } }
|
|
|
|
expect(Ransack.options[:up_arrow]).to eq '<i>U-Arrow</i>'
|
|
|
|
|
2016-09-30 13:40:12 -04:00
|
|
|
Ransack.options = default
|
2016-09-30 12:44:24 -04:00
|
|
|
end
|
|
|
|
|
2012-05-02 02:54:12 -04:00
|
|
|
it 'adds predicates that take arrays, overriding compounds' do
|
|
|
|
Ransack.configure do |config|
|
2013-12-15 13:27:50 -05:00
|
|
|
config.add_predicate(
|
|
|
|
:test_array_predicate,
|
2014-05-01 09:55:39 -04:00
|
|
|
:wants_array => true,
|
|
|
|
:compounds => true
|
2013-12-15 13:27:50 -05:00
|
|
|
)
|
2012-05-02 02:54:12 -04:00
|
|
|
end
|
|
|
|
|
2014-05-15 14:29:46 -04:00
|
|
|
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'
|
2012-05-02 02:54:12 -04:00
|
|
|
end
|
2015-03-19 17:01:45 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-09-29 09:17:30 -04:00
|
|
|
expect(Ransack.predicates['test_in_predicate'].wants_array)
|
|
|
|
.to eq true
|
|
|
|
expect(Ransack.predicates['test_not_in_predicate'].wants_array)
|
|
|
|
.to eq true
|
2015-03-19 17:01:45 -04:00
|
|
|
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
|
|
|
|
|
2016-09-29 09:17:30 -04:00
|
|
|
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
|
2015-03-19 17:01:45 -04:00
|
|
|
end
|
|
|
|
end
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2014-04-09 23:28:29 -04:00
|
|
|
end
|