Module: AutoCompleteFormBuilderHelper

Defined in:
lib/auto_complete_form_builder_helper.rb

Instance Method Summary collapse

Instance Method Details

#class_nameObject



3
4
5
6
7
8
9
# File 'lib/auto_complete_form_builder_helper.rb', line 3

def class_name
  if @object
    "#{@object.class.to_s.underscore}"
  else
    "#{@object_name.to_s.underscore}"
  end
end

#is_used_as_nested_attribute?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/auto_complete_form_builder_helper.rb', line 15

def is_used_as_nested_attribute?
  /\[#{class_name.pluralize}_attributes\]\[[0-9]+\]/.match @object_name.to_s
end

#sanitized_object_nameObject



11
12
13
# File 'lib/auto_complete_form_builder_helper.rb', line 11

def sanitized_object_name
  @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
end

#text_field_with_auto_complete(method, tag_options = {}, completion_options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/auto_complete_form_builder_helper.rb', line 19

def text_field_with_auto_complete(method, tag_options = {}, completion_options = {})
  if completion_options[:child_index]
    unique_object_name = "#{class_name}_#{completion_options[:child_index]}"
  elsif @options[:child_index]
    unique_object_name = "#{class_name}_#{@options[:child_index]}"
  elsif is_used_as_nested_attribute?
    unique_object_name = sanitized_object_name
  elsif !(@object_name.to_s =~ /\[\]$/)
    unique_object_name = sanitized_object_name
  else
    unique_object_name = "#{class_name}_#{Object.new.object_id.abs}"
  end
  completion_options_for_class_name = {
    :url => { :action => "auto_complete_for_#{class_name}_#{method}" },
    :param_name => "#{class_name}[#{method}]"
  }.update(completion_options)
  @template.auto_complete_field_with_style_and_script(unique_object_name,
                                                      method,
                                                      tag_options,
                                                      completion_options_for_class_name
                                                     ) do
    text_field(method, { :id => "#{unique_object_name}_#{method}" }.update(tag_options))
  end
end