module Decidim::Map::Autocomplete::FormBuilder

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 %>

Public Instance Methods

geocoding_field(attribute, options = {}, geocoding_options = {}) click to toggle source
# 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)
    @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