Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/core_ext/core_ext.rb
Class Method Summary collapse
-
.search_conditions(valid_search_criteria, pairs, operator = 'OR') ⇒ Object
Returns an array of SQL conditions suitable for use with ActiveRecord’s finder.
- .to_option_values ⇒ Object
Instance Method Summary collapse
-
#strip(attribute) ⇒ Object
Strips the specified attribute’s value.
Class Method Details
.search_conditions(valid_search_criteria, pairs, operator = 'OR') ⇒ Object
Returns an array of SQL conditions suitable for use with ActiveRecord’s finder. valid_criteria is an array of valid search fields. pairs is a hash of field names and values.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/core_ext/core_ext.rb', line 260 def self.search_conditions( valid_search_criteria, pairs, operator = 'OR' ) if valid_search_criteria.detect{ |_c| ! pairs[_c].blank? } || ! pairs[:query].blank? _conditions = [] _or_clause = '' _or_clause_values = [] _int_terms = {} _text_terms = {} # build or clause for keyword search unless pairs[:query].blank? || ! self.respond_to?(:flattened_content) pairs[:query].split(' ').each do |keyword| _or_clause += 'flattened_content LIKE ? OR ' _or_clause_values << "%#{keyword}%" end _or_clause.gsub!( / OR $/, '') end # iterate across each valid search field valid_search_criteria.each do |_field| # build or clause for keyword search unless pairs[:query].blank? || self.respond_to?(:flattened_content) pairs[:query].split(' ').each do |keyword| _or_clause += "#{_field} LIKE ? OR " _or_clause_values << "%#{keyword}%" end end # build hashes of integer and/or text search fields and values for each non-blank param if ! pairs[_field].blank? _field.to_s =~ /^id$|_id$|\?$/ ? _int_terms[_field.to_s.gsub('?', '')] = pairs[_field] : _text_terms[_field] = pairs[_field] end end _or_clause.gsub!( / OR $/, '') # convert the hash to parametric SQL if _or_clause.blank? _conditions = sql_conditions_for( _int_terms, _text_terms, nil, operator ) elsif _int_terms.keys.empty? && _text_terms.keys.empty? _conditions = [ _or_clause ] else _conditions = sql_conditions_for( _int_terms, _text_terms, _or_clause, operator ) end # add integer values _int_terms.keys.each{ |key| _conditions << _int_terms[key] } # add wildcard-padded values _text_terms.keys.each{ |key| _conditions << "%#{_text_terms[key]}%" } unless _or_clause_values.empty? # add keywords _conditions += _or_clause_values end return _conditions else return nil end end |
.to_option_values ⇒ Object
321 322 323 |
# File 'lib/core_ext/core_ext.rb', line 321 def self.to_option_values self.all.map{ |_x| [_x.name, _x.id] } end |
Instance Method Details
#strip(attribute) ⇒ Object
Strips the specified attribute’s value.
326 327 328 329 |
# File 'lib/core_ext/core_ext.rb', line 326 def strip(attribute) value = self[attribute] self.send("#{attribute}=", value && value.strip) end |