Class: Hoodoo::ActiveRecord::Finder::SearchHelper
- Inherits:
-
Object
- Object
- Hoodoo::ActiveRecord::Finder::SearchHelper
- Defined in:
- lib/hoodoo/active/active_record/search_helper.rb
Overview
Help build up Hash maps to pass into Hoodoo::ActiveRecord::Finder methods Hoodoo::ActiveRecord::Finder#search_with and Hoodoo::ActiveRecord::Finder#filter_with.
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/fitler 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.
Class Method Summary collapse
-
.ci_match_generic(model_field_name = nil) ⇒ Object
Case-insensitive match which should be fairly database independent but will run relatively slowly as a result.
-
.ci_match_postgres(model_field_name = nil) ⇒ Object
Case-insensitive match which requires PostgreSQL but should run quickly.
-
.ciaw_match_generic(model_field_name = nil) ⇒ Object
As #ci_match_generic, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
-
.ciaw_match_postgres(model_field_name = nil) ⇒ Object
As #ci_match_postgres, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
-
.cs_match(model_field_name = nil) ⇒ Object
Case-sensitive match (default-style matching).
-
.cs_match_array(model_field_name = nil) ⇒ Object
Case-sensitive match of a series of values given as an Array.
-
.cs_match_csv(model_field_name = nil) ⇒ Object
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.
Class Method Details
.ci_match_generic(model_field_name = nil) ⇒ Object
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
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#search_with or Hoodoo::ActiveRecord::Finder#filter_with.
132 133 134 135 136 137 138 139 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 132 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 |
.ci_match_postgres(model_field_name = nil) ⇒ Object
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
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#search_with or Hoodoo::ActiveRecord::Finder#filter_with.
167 168 169 170 171 172 173 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 167 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 |
.ciaw_match_generic(model_field_name = nil) ⇒ Object
As #ci_match_generic, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
144 145 146 147 148 149 150 151 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 144 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 |
.ciaw_match_postgres(model_field_name = nil) ⇒ Object
As #ci_match_postgres, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
178 179 180 181 182 183 184 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 178 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 |
.cs_match(model_field_name = nil) ⇒ Object
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
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#search_with or Hoodoo::ActiveRecord::Finder#filter_with.
61 62 63 64 65 66 67 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 61 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 |
.cs_match_array(model_field_name = nil) ⇒ Object
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
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#search_with or Hoodoo::ActiveRecord::Finder#filter_with.
107 108 109 110 111 112 113 114 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 107 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 |
.cs_match_csv(model_field_name = nil) ⇒ Object
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
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#search_with or Hoodoo::ActiveRecord::Finder#filter_with.
83 84 85 86 87 88 89 90 |
# File 'lib/hoodoo/active/active_record/search_helper.rb', line 83 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 |