module Ransack::Configuration
def add_predicate(name, opts = {})
def add_predicate(name, opts = {}) name = name.to_s opts[:name] = name compounds = opts.delete(:compounds) compounds = true if compounds.nil? compounds = false if opts[:wants_array] self.predicates[name] = Predicate.new(opts) Constants::SUFFIXES.each do |suffix| compound_name = name + suffix self.predicates[compound_name] = Predicate.new( opts.merge( name: compound_name, arel_predicate: arel_predicate_with_suffix( opts[:arel_predicate], suffix ), compound: true ) ) end if compounds end
def arel_predicate_with_suffix(arel_predicate, suffix)
def arel_predicate_with_suffix(arel_predicate, suffix) if arel_predicate === Proc proc { |v| "#{arel_predicate.call(v)}#{suffix}" } else "#{arel_predicate}#{suffix}" end end
def configure
def configure yield self end
def custom_arrows=(opts = {})
end
}
default_arrow: 'U+11047'
down_arrow: 'U+02193',
up_arrow: '',
config.custom_arrows = {
# Globally set the up arrow to an icon, and the down and default arrows to unicode.
Ransack.configure do |config|
like `config/initializers/ransack.rb` as follows:
Any of the defaults may be globally overridden in an initializer file
By default this is nil so nothing will be displayed.
There is also a default arrow which is displayed if a column is not sorted.
down_arrow: '▲'
up_arrow: '▼'
By default, Ransack displays sort order indicator arrows with HTML codes:
def custom_arrows=(opts = {}) self.options[:up_arrow] = opts[:up_arrow].freeze if opts[:up_arrow] self.options[:down_arrow] = opts[:down_arrow].freeze if opts[:down_arrow] self.options[:default_arrow] = opts[:default_arrow].freeze if opts[:default_arrow] end
def default_predicate=(name)
end
config.default_predicate = 'eq'
# Use the 'eq' predicate if an unknown predicate is passed
Ransack.configure do |config|
like `config/initializers/ransack.rb` as follows:
a default predicate by setting it in an initializer file
By default Ransack ignores empty predicates. Ransack can also fallback to
def default_predicate=(name) self.options[:default_predicate] = name end
def hide_sort_order_indicators=(boolean)
end
config.hide_sort_order_indicators = true
# Hide sort link order indicators globally across the application
Ransack.configure do |config|
`config/initializers/ransack.rb` as follows:
The default may be globally overridden in an initializer file like
By default, Ransack displays sort order indicator arrows in sort links.
def hide_sort_order_indicators=(boolean) self.options[:hide_sort_order_indicators] = boolean end
def ignore_unknown_conditions=(boolean)
end
config.ignore_unknown_conditions = false
# Raise if an unknown predicate, condition or attribute is passed
Ransack.configure do |config|
initializer file like `config/initializers/ransack.rb` as follows:
attribute is passed into a search. The default may be overridden in an
By default Ransack ignores errors if an unknown predicate, condition or
def ignore_unknown_conditions=(boolean) self.options[:ignore_unknown_conditions] = boolean end
def postgres_fields_sort_option=(setting)
See this feature: https://www.postgresql.org/docs/13/queries-order.html
end
c.postgres_fields_sort_option = :nulls_first # or e.g. :nulls_always_last
Ransack.configure do |c|
User may want to configure it like this:
whether nulls appear before or after non-null values in the sort ordering.
The `NULLS FIRST` and `NULLS LAST` options can be used to determine
def postgres_fields_sort_option=(setting) self.options[:postgres_fields_sort_option] = setting end
def sanitize_custom_scope_booleans=(boolean)
end
config.sanitize_custom_scope_booleans = false
# Accept my custom scope values as what they are.
Ransack.configure do |config|
`config/initializers/ransack.rb` as follows:
This default may be globally overridden in an initializer file like
[0, '0', 'f', 'F', 'false', 'FALSE'] all evaluate to false.
[1, '1', 't', 'T', 'true', 'TRUE'] all evaluate to true.
Ransack sanitizes many values in your custom scopes into booleans.
def sanitize_custom_scope_booleans=(boolean) self.options[:sanitize_scope_args] = boolean end
def search_key=(name)
<%= f.search_form_for @search, as: :log_search %>
In the view:
@search = Log.ransack(params[:log_search], search_key: :log_search)
In the controller:
`ransack`, `search` and `@search_form_for` methods in controllers & views.
Another name can be set using the `search_key` option with Ransack
cannot be used, for instance if there were two searches on one page.
Sometimes there are situations when the default search parameter name
end
config.search_key = :query
# Name the search_key `:query` instead of the default `:q`
Ransack.configure do |config|
in an initializer file like `config/initializers/ransack.rb` as follows:
The default `search_key` name is `:q`. The default key may be overridden
def search_key=(name) self.options[:search_key] = name end
def strip_whitespace=(boolean)
end
config.strip_whitespace = true
# Enable whitespace stripping for string searches
Ransack.configure do |config|
`config/initializers/ransack.rb` as follows:
The default may be globally changed in an initializer file like
By default, Ransack displays strips all whitespace when searching for a string.
def strip_whitespace=(boolean) self.options[:strip_whitespace] = boolean end