Sha256: cbc6a33b28c095a1bd920ff4a55a0247d6fd34e7b619fd9cccdaf18023ee1b51

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

class SearchPage < Page
  OPERATOR_METHODS = {
    'Equals' => nil,
    'Not Equal' => :ne,
    'Greater Than' => :gt,
    'Less Than' => :lt,
    'Greater Than or Equal To' => :gte,
    'Less Than or Equal To' => :lte,
    'In' => :in
  }
  
  def query
    q = (type || site.records).where(show_in_search: true)
    
    # constant constraints
    conditions.each do |condition|
      q = add_condition(q, type, condition.name, condition.operator, condition.value)
    end
    
    # user conditions
    user_conditions.each do |condition|
      param_name = condition.as || condition.name
      q = add_condition(q, type, condition.name, condition.operator, params[param_name])
    end
    
    # add other optional search parameters
    q = q.sort(sort || params['sort']) if sort || params['sort']
    q = q.limit(limit || params['limit'].to_i) if limit || params['limit']
    q = q.skip(skip || params['skip'].to_i) if skip || params['skip']
    q
  end
  
  def add_condition(q, type, field, operator, value)
    return q if value.blank?
    operator = OPERATOR_METHODS[operator]
    field = field.to_sym
    
    if type && type.all_record_fields[field.to_s]
      value = type.all_record_fields[field.to_s].from_json(value, type)
    end
    
    # the in operator works on arrays
    #if operator == :in
    #  value = value.to_s.split(' ').reject(&:blank?).collect(&:downcase)
    #end
    
    if operator
      q.where(field.send(operator) => value)
    else
      q.where(field => value)
    end
  end
  
  # True if the request contains any of the parameters 
  def user_query?
    user_conditions.any? {|condition| params[condition.as ? condition.as : condition.name].present?}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yodel-0.0.1 lib/yodel/models/search/search_page.rb