Sha256: 2388a5b716f1597b2d97b506036248f7b7b53ac2944413d9d14ebca0ddbd5b6d

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# -*- encoding : utf-8 -*-
require 'search/conditions/abstract'

module RademadeAdmin
  module Search
    module Conditions
      class Autocomplete < Abstract

        def initialize(params, origin_fields, filter_fields)
          super(params, origin_fields)
          @filter_fields = filter_fields
        end

        protected

        def where
          @where_conditions = Where.new(:and)
          append_query_condition
          append_search_params
          @where_conditions
        end

        def limit
          10
        end

        private

        def append_query_condition
          if @params[:q].present?
            query_where = Where.new(:or)
            @filter_fields.each do |field|
              query_where.add(field, /#{@params[:q]}/i)
            end
            @where_conditions.sub_add(query_where)
          end
        end

        def append_search_params
          if @params[:search].present?
            @params[:search].each do |key, value|
              @where_conditions.add(key.to_sym, value) if @origin_fields.include? key.to_s
            end
          end
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rademade_admin-0.0.3 app/services/search/conditions/autocomplete.rb
rademade_admin-0.0.2 app/services/search/conditions/autocomplete.rb