Class: AutocompleteInput

Inherits:
SimpleForm::Inputs::TextInput
  • Object
show all
Defined in:
app/inputs/autocomplete_input.rb

Instance Method Summary collapse

Instance Method Details

#add_field_id?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/inputs/autocomplete_input.rb', line 72

def add_field_id?
  !options[:source_array] && (options[:field_id].nil? || options[:field_id])
end

#input(wrapper_options = nil) ⇒ Object

enable :placeholder, :maxlength



4
5
6
7
8
9
10
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
# File 'app/inputs/autocomplete_input.rb', line 4

def input(wrapper_options = nil)
  out = ActiveSupport::SafeBuffer.new

  # prepare options
  new_html_options = merge_wrapper_options(prepare_html_options, wrapper_options)


  # hidden field
  if add_field_id?
    hidden_name = "#{attribute_name}_id"
    hidden_id = "#{object_name}_#{attribute_name}_id"
    new_html_options["data-field-id"] ||= hidden_id

    hidden_options = {}
    hidden_options[:id] = hidden_id

    # value
    value_method = "#{attribute_name}_id"
    if new_html_options.has_key?(:value)
      v = new_html_options[:value]
    elsif object.respond_to?(value_method)
      v = object.send(value_method)
    end
    hidden_options[:value] = v unless v.nil?

    out << @builder.hidden_field(hidden_name, hidden_options)
  end

  #
  new_html_options[:value] = options[:value_text] unless options[:value_text].nil?

  out << @builder.text_field(attribute_name, new_html_options)
  out
end

#prepare_html_optionsObject



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
65
66
67
68
69
70
# File 'app/inputs/autocomplete_input.rb', line 39

def prepare_html_options
  new_options = {}

  new_options[:class] = [input_html_options[:class], options[:class]].compact

  #
  new_options["data-provide"] ||= "typeahead"
  new_options["autocomplete"] ||= "off"

  # source
  if options[:source]
    new_options["data-source"] = options[:source]
  elsif options[:source_query]
    new_options["data-source-query"] = options[:source_query]
  elsif options[:source_array]
    #new_options["data-source-array"] = options[:source_array].inspect.to_s # problem with quotes "
    new_options["data-source-array"] = '['+options[:source_array].map{|r| "&quot;#{r}&quot;"}.join(',')+']'
  end


  # data options
  new_options["data-items"] = options[:items] || 8
  new_options["data-min-length"] = options[:minLength] || 1
  new_options["data-afterSelect"] = options[:afterSelect] || false

  # value
  new_options[:value] = options[:value] unless options[:value].nil?
  new_options[:value_text] = options[:value_text] unless options[:value_text].nil?


  input_html_options.merge new_options
end