Module: RailsJQueryAutocomplete::Orm::ActiveRecord
- Defined in:
- lib/rails-jquery-autocomplete/orm/active_record.rb
Instance Method Summary collapse
- #active_record_get_autocomplete_items(parameters) ⇒ Object
- #active_record_get_autocomplete_order(method, options, model = nil) ⇒ Object
- #get_autocomplete_select_clause(model, method, options) ⇒ Object
- #get_autocomplete_where_clause(model, term, method, options) ⇒ Object
- #postgres?(model) ⇒ Boolean
Instance Method Details
#active_record_get_autocomplete_items(parameters) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rails-jquery-autocomplete/orm/active_record.rb', line 11 def active_record_get_autocomplete_items(parameters) model = parameters[:model] term = parameters[:term] = parameters[:options] method = [:hstore] ? [:hstore][:method] : parameters[:method] scopes = Array([:scopes]) where = [:where] limit = get_autocomplete_limit() order = active_record_get_autocomplete_order(method, , model) items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped scopes.each { |scope| items = items.send(scope) } unless scopes.empty? items = items.select(get_autocomplete_select_clause(model, method, )) unless [:full_model] items = items.where(get_autocomplete_where_clause(model, term, method, )). limit(limit).order(order) items = items.where(where) unless where.blank? items end |
#active_record_get_autocomplete_order(method, options, model = nil) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/rails-jquery-autocomplete/orm/active_record.rb', line 4 def active_record_get_autocomplete_order(method, , model=nil) order = [:order] table_prefix = model ? "#{model.table_name}." : "" order || "LOWER(#{table_prefix}#{method}) ASC" end |
#get_autocomplete_select_clause(model, method, options) ⇒ Object
34 35 36 37 |
# File 'lib/rails-jquery-autocomplete/orm/active_record.rb', line 34 def get_autocomplete_select_clause(model, method, ) table_name = model.table_name (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + ([:extra_data].blank? ? [] : [:extra_data])) end |
#get_autocomplete_where_clause(model, term, method, options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rails-jquery-autocomplete/orm/active_record.rb', line 39 def get_autocomplete_where_clause(model, term, method, ) table_name = model.table_name is_full_search = [:full] like_clause = (postgres?(model) ? 'ILIKE' : 'LIKE') if [:hstore] ["LOWER(#{table_name}.#{method} -> '#{[:hstore][:key]}') LIKE ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"] else ["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"] end end |
#postgres?(model) ⇒ Boolean
50 51 52 53 |
# File 'lib/rails-jquery-autocomplete/orm/active_record.rb', line 50 def postgres?(model) # Figure out if this particular model uses the PostgreSQL adapter model.connection.class.to_s.match(/PostgreSQLAdapter/) end |