Sha256: 1ff14268be1cb6ebdc1366c0eb8de94960b76c842820dbab321b7e085f18c825

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

module Rails3JQueryAutocomplete
  module Orm
    module ActiveRecord
      def get_autocomplete_order(method, options, model=nil)
        order = options[:order]

        table_prefix = model ? "#{model.table_name}." : ""
        order || "#{table_prefix}#{method} ASC"
      end

      def get_autocomplete_items(parameters)
        model   = parameters[:model]
        term    = parameters[:term]
        method  = parameters[:method]
        options = parameters[:options]
        scopes  = Array(options[:scopes])
        limit   = get_autocomplete_limit(options)
        order   = get_autocomplete_order(method, options, model)


        items = model.scoped

        scopes.each { |scope| items = items.send(scope) } unless scopes.empty?

        items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
        items = items.where(get_autocomplete_where_clause(model, term, method, options)).
            limit(limit).order(order)
      end

      def get_autocomplete_select_clause(model, method, options)
        table_name = model.table_name
        (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
      end

      def get_autocomplete_where_clause(model, term, method, options)
        table_name = model.table_name
        is_full_search = options[:full]
        like_clause = (postgres? ? 'ILIKE' : 'LIKE')
        ["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
      end

      def postgres?
        defined?(PGconn)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails3-jquery-autocomplete-1.0.5 lib/rails3-jquery-autocomplete/orm/active_record.rb
rails3-jquery-autocomplete-1.0.4 lib/rails3-jquery-autocomplete/orm/active_record.rb
rails3-jquery-autocomplete-1.0.3 lib/rails3-jquery-autocomplete/orm/active_record.rb
rails3-jquery-autocomplete-1.0.2 lib/rails3-jquery-autocomplete/orm/active_record.rb