Module: Ransack::Configuration

Included in:
Ransack
Defined in:
lib/ransack/configuration.rb

Instance Method Summary collapse

Instance Method Details

#add_predicate(name, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ransack/configuration.rb', line 19

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



91
92
93
94
95
96
97
# File 'lib/ransack/configuration.rb', line 91

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

Yields:

  • (_self)

Yield Parameters:



15
16
17
# File 'lib/ransack/configuration.rb', line 15

def configure
  yield self
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



87
88
89
# File 'lib/ransack/configuration.rb', line 87

def hide_sort_order_indicators=(boolean)
  self.options[: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



74
75
76
# File 'lib/ransack/configuration.rb', line 74

def ignore_unknown_conditions=(boolean)
  self.options[:ignore_unknown_conditions] = 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 %>



61
62
63
# File 'lib/ransack/configuration.rb', line 61

def search_key=(name)
  self.options[:search_key] = name
end