Module: Ransack::Configuration
- Included in:
- Ransack
- Defined in:
- lib/ransack/configuration.rb
Defined Under Namespace
Classes: PredicateCollection
Instance Method Summary collapse
- #add_predicate(name, opts = {}) ⇒ Object
- #arel_predicate_with_suffix(arel_predicate, suffix) ⇒ Object
- #configure {|_self| ... } ⇒ Object
-
#custom_arrows=(opts = {}) ⇒ Object
By default, Ransack displays sort order indicator arrows with HTML codes:.
-
#hide_sort_order_indicators=(boolean) ⇒ Object
By default, Ransack displays sort order indicator arrows in sort links.
-
#ignore_unknown_conditions=(boolean) ⇒ Object
By default Ransack ignores errors if an unknown predicate, condition or attribute is passed into a search.
-
#postgres_fields_sort_option=(setting) ⇒ Object
The ‘NULLS FIRST` and `NULLS LAST` options can be used to determine whether nulls appear before or after non-null values in the sort ordering.
-
#sanitize_custom_scope_booleans=(boolean) ⇒ Object
Ransack sanitizes many values in your custom scopes into booleans.
-
#search_key=(name) ⇒ Object
The default ‘search_key` name is `:q`.
-
#strip_whitespace=(boolean) ⇒ Object
By default, Ransack displays strips all whitespace when searching for a string.
Instance Method Details
#add_predicate(name, opts = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ransack/configuration.rb', line 45 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 |
#arel_predicate_with_suffix(arel_predicate, suffix) ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/ransack/configuration.rb', line 187 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 |
#configure {|_self| ... } ⇒ Object
41 42 43 |
# File 'lib/ransack/configuration.rb', line 41 def configure yield self end |
#custom_arrows=(opts = {}) ⇒ Object
By default, Ransack displays sort order indicator arrows with HTML codes:
up_arrow: '▼'
down_arrow: '▲'
There is also a default arrow which is displayed if a column is not sorted. By default this is nil so nothing will be displayed.
Any of the defaults may be globally overridden in an initializer file like ‘config/initializers/ransack.rb` as follows:
Ransack.configure do |config|
# Globally set the up arrow to an icon, and the down and default arrows to unicode.
config.custom_arrows = {
up_arrow: '<i class="fa fa-long-arrow-up"></i>',
down_arrow: 'U+02193',
default_arrow: 'U+11047'
}
end
124 125 126 127 128 |
# File 'lib/ransack/configuration.rb', line 124 def custom_arrows=(opts = {}) self.[:up_arrow] = opts[:up_arrow].freeze if opts[:up_arrow] self.[:down_arrow] = opts[:down_arrow].freeze if opts[:down_arrow] self.[:default_arrow] = opts[:default_arrow].freeze if opts[:default_arrow] end |
#hide_sort_order_indicators=(boolean) ⇒ Object
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:
Ransack.configure do |config|
# Hide sort link order indicators globally across the application
config.hide_sort_order_indicators = true
end
170 171 172 |
# File 'lib/ransack/configuration.rb', line 170 def hide_sort_order_indicators=(boolean) self.[:hide_sort_order_indicators] = boolean end |
#ignore_unknown_conditions=(boolean) ⇒ Object
By default Ransack ignores errors if an unknown predicate, condition or attribute is passed into a search. The default may be overridden in an initializer file like ‘config/initializers/ransack.rb` as follows:
Ransack.configure do |config|
# Raise if an unknown predicate, condition or attribute is passed
config.ignore_unknown_conditions = false
end
100 101 102 |
# File 'lib/ransack/configuration.rb', line 100 def ignore_unknown_conditions=(boolean) self.[:ignore_unknown_conditions] = boolean end |
#postgres_fields_sort_option=(setting) ⇒ Object
The ‘NULLS FIRST` and `NULLS LAST` options can be used to determine whether nulls appear before or after non-null values in the sort ordering.
User may want to configure it like this:
Ransack.configure do |c|
c.postgres_fields_sort_option = :nulls_first # or e.g. :nulls_always_last
end
See this feature: www.postgresql.org/docs/13/queries-order.html
157 158 159 |
# File 'lib/ransack/configuration.rb', line 157 def postgres_fields_sort_option=(setting) self.[:postgres_fields_sort_option] = setting end |
#sanitize_custom_scope_booleans=(boolean) ⇒ Object
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
142 143 144 |
# File 'lib/ransack/configuration.rb', line 142 def sanitize_custom_scope_booleans=(boolean) self.[:sanitize_scope_args] = boolean end |
#search_key=(name) ⇒ Object
The default ‘search_key` name is `:q`. The default key may be overridden in an initializer file like `config/initializers/ransack.rb` as follows:
Ransack.configure do |config|
# Name the search_key `:query` instead of the default `:q`
config.search_key = :query
end
Sometimes there are situations when the default search parameter name cannot be used, for instance if there were two searches on one page. Another name can be set using the ‘search_key` option with Ransack `ransack`, `search` and `@search_form_for` methods in controllers & views.
In the controller: In the view: <%= f.search_form_for @search, as: :log_search %>
87 88 89 |
# File 'lib/ransack/configuration.rb', line 87 def search_key=(name) self.[:search_key] = name end |
#strip_whitespace=(boolean) ⇒ Object
By default, Ransack displays strips all whitespace when searching for a string. The default may be globally changed in an initializer file like ‘config/initializers/ransack.rb` as follows:
Ransack.configure do |config|
# Enable whitespace stripping for string searches
config.strip_whitespace = true
end
183 184 185 |
# File 'lib/ransack/configuration.rb', line 183 def strip_whitespace=(boolean) self.[:strip_whitespace] = boolean end |