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

Included in:
FormBuilder
Defined in:
decidim-core/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
72
73
# File 'decidim-core/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)
    @template.snippets.add(:geocoding, builder.stylesheet_snippets)
    @template.snippets.add(:geocoding, builder.javascript_snippets)

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

  options[:value] ||= object.send(attribute) if object.respond_to?(attribute)
  if object.respond_to?(:latitude) && object.respond_to?(:longitude)
    point = [object.latitude, object.longitude]
    options["data-coordinates"] ||= point.join(",")
  end

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