config/application.rb
# filter
#
# Use the input active record query, and append a filter to it based on the filters present in params
filter
Use the input active record query, and append a filter to it based on the filters present in params
filter Use the input active record query and append a filter to it based on the filters present in params
def filter(filters)
      return self if filters.nil?
      filtered_query = self
      filters.each do |field, value|
        parsed_value = value == '' ? nil : value
        field_s = field.to_s
        min_max_match =  /(min_)|(max_)/.match(field_s)
        if min_max_match
          mf = field_s.sub('min_','').sub('max_','').strip
          next if mf.empty?
          min = min_max_match[1]
          model = self.klass
          mfield = model.arel_table[mf]
          filtered_query = min ? filtered_query.where(mfield.gteq(value)) : filtered_query.where(mfield.lteq(value))
        else
          if(parsed_value.is_a? Hash)
            parsed_value.each do |join_field, join_value|
              filtered_query = filtered_query.includes(field).where(field.to_s.pluralize => {join_field => join_value})
            end
          else
            filtered_query = filtered_query.where(field_s => parsed_value)
          end
        end
      end
      @filters = filters
      return filtered_query
    end
def filter ( filters ) return self if filters . nil? filtered_query = self filters . each do | field , value | parsed_value = value == '' ? nil : value field_s = field . to_s min_max_match = / (min_)|(max_) / . match ( field_s ) if min_max_match mf = field_s . sub ( 'min_' , '' ) . sub ( 'max_' , '' ) . strip next if mf . empty? min = min_max_match [ 1 ] model = self . klass mfield = model . arel_table [ mf ] filtered_query = min ? filtered_query . where ( mfield . gteq ( value ) ) : filtered_query . where ( mfield . lteq ( value ) ) else if ( parsed_value . is_a? Hash ) parsed_value . each do | join_field , join_value | filtered_query = filtered_query . includes ( field ) . where ( field . to_s . pluralize => { join_field => join_value } ) end else filtered_query = filtered_query . where ( field_s => parsed_value ) end end end @filters = filters return filtered_query end
filter
Use the input active record query, and append a filter to it based on the filters present in params
filter Use the input active record query and append a filter to it based on the filters present in params
# Handle ranges of values # Make sure there's a valid field here # Figure out if this is a min or max # Handle associations/joins # Assumes that table is association pluralized #filtered_query = filtered_query.where(field => {join_field => join_value}) # Handle equality
filters