Sha256: 39b97b78621a99610b8e5c1b6cac6e9d083783e93dca8825e2aafd72352f3f2d

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

module Bootstrap3AutocompleteInput
  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[:q] || parameters[:term]
        method  = parameters[:method]
        options = parameters[:options]
        scopes  = Array(options[:scopes])
        where   = options[:where]
        limit   = autocomplete_option_limit(options)
        order   = get_autocomplete_order(method, options, model)

        items = model.all

        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)
        items = items.where(where) unless where.blank?

        items
      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?(model) ? 'ILIKE' : 'LIKE')
        ["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
      end

      def postgres?(model)
        # Figure out if this particular model uses the PostgreSQL adapter
        model.connection.class.to_s.match(/PostgreSQLAdapter/)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bootstrap3_autocomplete_input-0.1.9 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.8 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.7 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.6 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.5 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.4 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.3 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.2 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.1 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.1.0 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.0.6 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.0.5 lib/bootstrap3_autocomplete_input/orm/active_record.rb
bootstrap3_autocomplete_input-0.0.4 lib/bootstrap3_autocomplete_input/orm/active_record.rb