Module: BootstrapAutocompleteInput::Orm::ActiveRecord
- Defined in:
- lib/bootstrap_autocomplete_input/orm/active_record.rb
Instance Method Summary collapse
- #get_autocomplete_items(parameters) ⇒ Object
- #get_autocomplete_order(column_name, options, model = nil) ⇒ Object
- #get_autocomplete_select_clause(model, column_name, extra_columns, options) ⇒ Object
- #get_autocomplete_where_clause(model, term, column_name, options) ⇒ Object
- #postgres?(model) ⇒ Boolean
Instance Method Details
#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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bootstrap_autocomplete_input/orm/active_record.rb', line 11 def get_autocomplete_items(parameters) model = parameters[:model] term = parameters[:q] || parameters[:term] = parameters[:options] extra_columns = ([:extra_columns] || []).map{|c| c.to_sym} #column_name = options[:column_name] column_name = parameters[:method] # items = model.all unless [:full_model] items = items.select(get_autocomplete_select_clause(model, column_name, extra_columns, )) end # basic where items = items.where(get_autocomplete_where_clause(model, term, column_name, )) # additional where scopes = Array([:scopes]) where = [:where] where_method = [:where_method] unless scopes.empty? scopes.each { |scope| items = items.send(scope) } end unless where.blank? items = items.where(where) end unless where_method.nil? if respond_to?(where_method) w = send(where_method) if w items = items.where(w) end end end # order, limit limit = autocomplete_option_limit() order = get_autocomplete_order(column_name, , model) items = items.limit(limit).order(order) # items end |
#get_autocomplete_order(column_name, options, model = nil) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/bootstrap_autocomplete_input/orm/active_record.rb', line 4 def get_autocomplete_order(column_name, , model=nil) order = [:order] table_prefix = model ? "#{model.table_name}." : "" order || "#{table_prefix}#{column_name} ASC" end |
#get_autocomplete_select_clause(model, column_name, extra_columns, options) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/bootstrap_autocomplete_input/orm/active_record.rb', line 66 def get_autocomplete_select_clause(model, column_name, extra_columns, ) table_name = model.table_name #cols = ([model.primary_key, column_name]+extra_columns).reject { |c| c.nil? || c.blank? || c.empty? } cols = ([model.primary_key, column_name]).reject { |c| c.nil? || c.blank? || c.empty? } #(["#{table_name}.#{model.primary_key}", "#{table_name}.#{column_name}"] + (options[:extra_data].blank? ? [] : options[:extra_data])) cols.map{|c| "#{table_name}.#{c}"} + ([:extra_data].blank? ? [] : [:extra_data]) end |
#get_autocomplete_where_clause(model, term, column_name, options) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/bootstrap_autocomplete_input/orm/active_record.rb', line 75 def get_autocomplete_where_clause(model, term, column_name, ) table_name = model.table_name # basic search is_full_search = [:full] like_clause = (postgres?(model) ? 'ILIKE' : 'LIKE') ["LOWER(#{table_name}.#{column_name}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"] end |
#postgres?(model) ⇒ Boolean
84 85 86 87 |
# File 'lib/bootstrap_autocomplete_input/orm/active_record.rb', line 84 def postgres?(model) # Figure out if this particular model uses the PostgreSQL adapter model.connection.class.to_s.match(/PostgreSQLAdapter/) end |