Help build up Hash maps to pass into Hoodoo::ActiveRecord::Finder methods Hoodoo::ActiveRecord::Finder::ClassMethods#search_with and Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with. Used also by the default framework search scopes.
The usage pattern is as follows, using “sh” as a local variable just for brevity - it isn't required:
sh = Hoodoo::ActiveRecord::Finder::SearchHelper
class SomeModel < ActiveRecord::Base
search_with(
:colour => sh.cs_match_generic,
:name => sh.ci_match_generic,
:resource_ids => sh.cs_match_csv( :associated_id )
end
end
The helper methods just provide values to pass into the Hash used with the search/filter Hoodoo::ActiveRecord::Finder methods, so they're optional and compatible with calls that write it out “by hand”.
In all cases, in normal use the generated SQL _will not_ match
null
values; if negated for a filter
(“where.not
”), the generated SQL will match
null
values. As a result, passing in explicit searches for
nil
won't work - but that's never expected as a use
case here since search values are coming in via e.g. query string
information from a URI.
- C
Case-insensitive match which should be fairly database independent but will run relatively slowly as a result. If you are using PostgreSQL, consider using the faster ::ci_match_postgres method instead.
Results in a lower(foo) = bar AND foo IS NOT NULL
query with
bar
coerced to a String and converted to lower case by Ruby
first.
model_field_name
-
If the model attribute name differs from the search key you want to use in the URI, give the model attribute name here, else omit.
Returns a value that can be asssigned to a URI query string key in the Hash given to Hoodoo::ActiveRecord::Finder::ClassMethods#search_with or Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 158 def self.ci_match_generic( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr value = ( value || '' ).to_s.downcase [ "lower(#{ column }) = ? AND #{ column } IS NOT NULL", value ] } end
Case-insensitive match which requires PostgreSQL but should run quickly. If you need a database agnostic solution, consider using the slower ::ci_match_generic method instead.
Results in a foo ILIKE bar AND foo IS NOT NULL
query.
model_field_name
-
If the model attribute name differs from the search key you want to use in the URI, give the model attribute name here, else omit.
Returns a value that can be asssigned to a URI query string key in the Hash given to Hoodoo::ActiveRecord::Finder::ClassMethods#search_with or Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 199 def self.ci_match_postgres( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } ILIKE ? AND #{ column } IS NOT NULL", value ] } end
As ::ci_match_generic, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
Results in a foo LIKE %bar% AND foo IS NOT NULL
query.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 173 def self.ciaw_match_generic( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr value = ( value || '' ).to_s.downcase [ "lower(#{ column }) LIKE ? AND #{ column } IS NOT NULL", "%#{ value }%" ] } end
As ::ci_match_postgres, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
Results in a foo ILIKE %bar% AND foo IS NOT NULL
query.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 213 def self.ciaw_match_postgres( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } ILIKE ? AND #{ column } IS NOT NULL", "%#{ value }%" ] } end
As ::cs_lt, but compares with greater-than.
Results in a foo > bar AND foo IS NOT NULL
query.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 267 def self.cs_gt( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } > ? AND #{ column } IS NOT NULL", value ] } end
As ::cs_lt, but compares with greater-than-or-equal-to.
Results in a foo >= bar AND foo IS NOT NULL
query.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 280 def self.cs_gte( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } >= ? AND #{ column } IS NOT NULL", value ] } end
Case-sensitive less-than (default-style comparison). WARNING: This will be case sensitive only if your database is configured for case sensitive matching by default.
If comparing non-string column types be sure to pass in a value of an appropriate matching type (e.g. compare dates with DateTimes), else returned results will be incorrect but errors may not arise depending on database engine in use.
Results in a foo < bar AND foo IS NOT NULL
query.
model_field_name
-
If the model attribute name differs from the search key you want to use in the URI, give the model attribute name here, else omit.
Returns a value that can be asssigned to a URI query string key in the Hash given to Hoodoo::ActiveRecord::Finder::ClassMethods#search_with or Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 241 def self.cs_lt( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } < ? AND #{ column } IS NOT NULL", value ] } end
As ::cs_lt, but compares with less-than-or-equal-to.
Results in a foo <= bar AND foo IS NOT NULL
query.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 254 def self.cs_lte( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } <= ? AND #{ column } IS NOT NULL", value ] } end
Case-sensitive match (default-style matching). WARNING: This will be case sensitive only if your database is configured for case sensitive matching by default.
Results in a foo = bar AND foo IS NOT NULL
query.
model_field_name
-
If the model attribute name differs from the search key you want to use in the URI, give the model attribute name here, else omit.
Returns a value that can be asssigned to a URI query string key in the Hash given to Hoodoo::ActiveRecord::Finder::ClassMethods#search_with or Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 64 def self.cs_match( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr [ "#{ column } = ? AND #{ column } IS NOT NULL", value ] } end
Case-sensitive match of a series of values given as an Array. Normally, query string information comes in as a String so the use cases for this are quite unusual; you probably want to use ::cs_match_csv most of the time.
Results in a foo IN (bar,baz,boo) AND foo IS NOT NULL
query.
model_field_name
-
If the model attribute name differs from the search key you want to use in the URI, give the model attribute name here, else omit.
Returns a value that can be asssigned to a URI query string key in the Hash given to Hoodoo::ActiveRecord::Finder::ClassMethods#search_with or Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 115 def self.cs_match_array( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr value = [ value ].flatten [ "#{ column } IN (?) AND #{ column } IS NOT NULL", value ] } end
Case-sensitive match of a series of values separated by commas, which are split into an array then processed by AREL back to something SQL-safe.
Results in a foo IN (bar,baz,boo) AND foo IS NOT NULL
query.
model_field_name
-
If the model attribute name differs from the search key you want to use in the URI, give the model attribute name here, else omit.
Returns a value that can be asssigned to a URI query string key in the Hash given to Hoodoo::ActiveRecord::Finder::ClassMethods#search_with or Hoodoo::ActiveRecord::Finder::ClassMethods#filter_with.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 88 def self.cs_match_csv( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr value = value.split( ',' ) [ "#{ column } IN (?) AND #{ column } IS NOT NULL", value ] } end
As ::cs_match, but adds wildcards at the front and end of the string for a case-sensitive-all-wildcard match.
Results in a foo LIKE bar AND foo IS NOT NULL
query.
Source: show
# File lib/hoodoo/active/active_record/search_helper.rb, line 130 def self.csaw_match( model_field_name = nil ) Proc.new { | attr, value | column = model_field_name || attr value = ( value || '' ).to_s [ "#{ column } LIKE ? AND #{ column } IS NOT NULL", "%#{ value }%" ] } end