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::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.
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 Hoodoo::ActiveRecord::Finder::SearchHelper::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 Hoodoo::ActiveRecord::Finder::SearchHelper::ci_match_postgres, but adds wildcards at the front and end of the string for a case-insensitive-all-wildcard match.
-
.cs_gt(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_lt, but compares with greater-than.
-
.cs_gte(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_lt, but compares with greater-than-or-equal-to.
-
.cs_lt(model_field_name = nil) ⇒ Object
Case-sensitive less-than (default-style comparison).
-
.cs_lte(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_lt, but compares with less-than-or-equal-to.
-
.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.
-
.csaw_match(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_match, but adds wildcards at the front and end of the string for a case-sensitive-all-wildcard match.
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 Hoodoo::ActiveRecord::Finder::SearchHelper::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.
158 159 160 161 162 163 164 165 |
# 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 |
.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 Hoodoo::ActiveRecord::Finder::SearchHelper::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.
199 200 201 202 203 204 205 |
# 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 |
.ciaw_match_generic(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::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.
173 174 175 176 177 178 179 180 |
# 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 |
.ciaw_match_postgres(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::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.
213 214 215 216 217 218 219 |
# 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 |
.cs_gt(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_lt, but compares with greater-than.
Results in a foo > bar AND foo IS NOT NULL
query.
267 268 269 270 271 272 273 |
# 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 |
.cs_gte(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_lt, but compares with greater-than-or-equal-to.
Results in a foo >= bar AND foo IS NOT NULL
query.
280 281 282 283 284 285 286 |
# 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 |
.cs_lt(model_field_name = nil) ⇒ Object
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.
241 242 243 244 245 246 247 |
# 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 |
.cs_lte(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::cs_lt, but compares with less-than-or-equal-to.
Results in a foo <= bar AND foo IS NOT NULL
query.
254 255 256 257 258 259 260 |
# 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 |
.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 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.
64 65 66 67 68 69 70 |
# 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 |
.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 Hoodoo::ActiveRecord::Finder::SearchHelper::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.
115 116 117 118 119 120 121 122 |
# 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 |
.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) 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.
88 89 90 91 92 93 94 95 |
# 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 |
.csaw_match(model_field_name = nil) ⇒ Object
As Hoodoo::ActiveRecord::Finder::SearchHelper::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.
130 131 132 133 134 135 136 137 |
# 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 |