Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Includes:
MirUtility
Defined in:
lib/mir_utility.rb

Constant Summary

Constants included from MirUtility

MirUtility::MONTHS, MirUtility::STATE_CODES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MirUtility

canonical_url, destroy_old_sessions, normalize, normalize_slug, state_name_for

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.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mir_utility.rb', line 142

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_valuesObject



203
204
205
# File 'lib/mir_utility.rb', line 203

def self.to_option_values
  self.all.sort_by{ |x| x.name }.map{ |x| [x.name, x.id] }
end

Instance Method Details

#strip(attribute) ⇒ Object

Strips the specified attribute’s value.



208
209
210
211
# File 'lib/mir_utility.rb', line 208

def strip(attribute)
  value = self[attribute]
  self.send("#{attribute}=", value && value.strip)
end