Module: Decidim::Map::Autocomplete::FormBuilder

Included in:
FormBuilder
Defined in:
lib/decidim/map/autocomplete.rb

Overview

This module will be included in the main application’s form builder in order to provide the geocoding_field method for the normal form builders. This allows you to include geocoding autocompletion in the forms using the following code:

<%= form_for record do |form| %>
  <%= form.geocoding_field(:address) %>
<% end %>

Instance Method Summary collapse

Instance Method Details

#geocoding_field(attribute, options = {}, geocoding_options = {}) ⇒ Object



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
71
# File 'lib/decidim/map/autocomplete.rb', line 40

def geocoding_field(attribute, options = {}, geocoding_options = {})
  @autocomplete_utility ||= Decidim::Map.autocomplete(
    organization: @template.current_organization
  )
  return text_field(attribute, options) unless @autocomplete_utility

  # Decidim::Map::Autocomplete::Builder
  builder = @autocomplete_utility.create_builder(
    @template,
    geocoding_options
  )

  unless @template.snippets.any?(:geocoding_styles) || @template.snippets.any?(:geocoding_scripts)
    @template.snippets.add(:geocoding_styles, builder.stylesheet_snippets)
    @template.snippets.add(:geocoding_scripts, builder.javascript_snippets)

    # This will display the snippets in the <head> part of the page.
    @template.snippets.add(:head, @template.snippets.for(:geocoding_styles))
    # This will display the snippets in the bottom part of the page.
    @template.snippets.add(:foot, @template.snippets.for(:geocoding_scripts))
  end

  options = merge_geocoding_options(attribute, options)

  field(attribute, options) do |opts|
    builder.geocoding_field(
      @object_name,
      attribute,
      opts
    )
  end
end