Module: GeoTools::FormHelpers

Defined in:
lib/geo_tools/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#latitude_field(method, options = {}) ⇒ Object

Options:

:latitude
  :degrees
    :symbol
  :minutes
    :symbol
  :decimal_minutes
    :symbol
    :maxlength

Assumes the latitude field is called ‘latitude’.

The ‘method’ argument is for consistency with other field helpers. We don’t use it when using the normal Rails form builder.

1/100th of a minute of latitude (or equitorial longitude) is approximately 20m.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/geo_tools/form_helpers.rb', line 21

def latitude_field(method, options = {})
  opts = {
    :degrees         => { :symbol => '°' },
    :minutes         => { :symbol => '.'     },
    :decimal_minutes => { :symbol => '′', :maxlength => 2 },
  }
  lat_options = options.delete :latitude
  opts.merge! lat_options if lat_options
  
  output = []

  # Degrees
  width = 2
  output << text_field("latitude_degrees",
                       options.merge(:maxlength => width,
                                     :value     => "%0#{width}d" % (@object.send("latitude_degrees") || 0)))
  output << opts[:degrees][:symbol]

  # Minutes
  width = 2
  output << text_field("latitude_minutes",
                       options.merge(:maxlength => width,
                                     :value     => "%0#{width}d" % (@object.send("latitude_minutes") || 0)))
  output << opts[:minutes][:symbol]

  # Decimal minutes
  width = opts[:decimal_minutes][:maxlength]
  output << text_field("latitude_decimal_minutes",
                       options.merge(:maxlength => width,
                                     :value     => @object.send("latitude_decimal_minutes_as_string").ljust(width, '0')))
  output << opts[:decimal_minutes][:symbol]

  # Hemisphere.
  # Hmm, we pass the options in the html_options position.
  output << select("latitude_hemisphere", %w( N S ), {}, options)

  output.join "\n"
end

#longitude_field(method, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/geo_tools/form_helpers.rb', line 60

def longitude_field(method, options = {})
  opts = {
    :degrees         => { :symbol => '&deg;' },
    :minutes         => { :symbol => '.'     },
    :decimal_minutes => { :symbol => '&prime;', :maxlength => 2 },
  }
  long_options = options.delete :longitude
  opts.merge! long_options if long_options

  output = []
  
  # Degrees
  width = 3
  output << text_field("longitude_degrees",
                       options.merge(:maxlength => width,
                                     :value     => "%0#{width}d" % (@object.send("longitude_degrees") || 0)))
  output << opts[:degrees][:symbol]

  # Minutes
  width = 2
  output << text_field("longitude_minutes",
                       options.merge(:maxlength => width,
                                     :value     => "%0#{width}d" % (@object.send("longitude_minutes") || 0)))
  output << opts[:minutes][:symbol]

  # Decimal minutes
  width = opts[:decimal_minutes][:maxlength]
  output << text_field("longitude_decimal_minutes",
                       options.merge(:maxlength => width,
                                     :value     => @object.send("longitude_decimal_minutes_as_string").ljust(width, '0')))
  output << opts[:decimal_minutes][:symbol]

  # Hemisphere.
  # Hmm, we pass the options in the html_options position.
  output << select("longitude_hemisphere", %w( E W ), {}, options)

  output.join "\n"
end