Add config to turn off boolean conversions

This commit is contained in:
Garett Arrowood 2016-12-11 09:50:56 -05:00
parent 92e5f31ee3
commit 9d666f815f
2 changed files with 26 additions and 3 deletions

View File

@ -11,7 +11,8 @@ module Ransack
:ignore_unknown_conditions => true,
:hide_sort_order_indicators => false,
:up_arrow => '▼'.freeze,
:down_arrow => '▲'.freeze
:down_arrow => '▲'.freeze,
:sanitize_scope_args => true
}
def configure
@ -98,6 +99,22 @@ module Ransack
self.options[:down_arrow] = opts[:down_arrow].freeze if opts[:down_arrow]
end
# Ransack sanitizes many values in your custom scopes into booleans.
# [1, '1', 't', 'T', 'true', 'TRUE'] all evaluate to true.
# [0, '0', 'f', 'F', 'false', 'FALSE'] all evaluate to false.
#
# This default may be globally overridden in an initializer file like
# `config/initializers/ransack.rb` as follows:
#
# Ransack.configure do |config|
# # Accept my custom scope values as what they are.
# config.sanitize_custom_scope_booleans = false
# end
#
def sanitize_custom_scope_booleans=(boolean)
self.options[:sanitize_scope_args] = boolean
end
# By default, Ransack displays sort order indicator arrows in sort links.
# The default may be globally overridden in an initializer file like
# `config/initializers/ransack.rb` as follows:

View File

@ -123,12 +123,18 @@ module Ransack
private
def add_scope(key, args)
sanitized_args = if Ransack.options[:sanitize_scope_args]
sanitized_scope_args(args)
else
args
end
if @context.scope_arity(key) == 1
@scope_args[key] = args.is_a?(Array) ? args[0] : args
else
@scope_args[key] = args.is_a?(Array) ? sanitized_scope_args(args) : args
@scope_args[key] = args.is_a?(Array) ? sanitized_args : args
end
@context.chain_scope(key, sanitized_scope_args(args))
@context.chain_scope(key, sanitized_args)
end
def sanitized_scope_args(args)