geo_tools 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/.rbenv-version +1 -0
- data/Gemfile +2 -0
- data/README.md +36 -36
- data/Rakefile +1 -37
- data/geo_tools.gemspec +25 -0
- data/lib/geo_tools.rb +4 -3
- data/lib/geo_tools/form_helpers.rb +103 -0
- data/lib/geo_tools/location.rb +259 -0
- data/lib/geo_tools/validations.rb +79 -0
- data/lib/geo_tools/version.rb +3 -0
- data/test/geo_tools_test.rb +39 -29
- data/test/test_helper.rb +17 -4
- metadata +72 -47
- data/VERSION +0 -1
- data/init.rb +0 -1
- data/install.rb +0 -1
- data/lib/air_blade/geo_tools/form_helpers.rb +0 -166
- data/lib/air_blade/geo_tools/location.rb +0 -266
- data/lib/air_blade/geo_tools/validations.rb +0 -75
- data/tasks/geo_tools_tasks.rake +0 -4
- data/uninstall.rb +0 -1
@@ -0,0 +1,79 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module GeoTools
|
3
|
+
module Validations
|
4
|
+
|
5
|
+
# ActiveRecord::Validations::ClassMethods::ALL_NUMERICALITY_CHECKS
|
6
|
+
ALL_NUMERICALITY_CHECKS = { :greater_than => '>', :greater_than_or_equal_to => '>=',
|
7
|
+
:equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=',
|
8
|
+
:odd => 'odd?', :even => 'even?' }.freeze
|
9
|
+
|
10
|
+
# Sames as validates_numericality_of but additionally supports :for option
|
11
|
+
# which lets you attach an error to a different attribute.
|
12
|
+
def validates_inclusion_of_for(*attr_names)
|
13
|
+
configuration = { :on => :save }
|
14
|
+
configuration.update(attr_names.extract_options!)
|
15
|
+
|
16
|
+
enum = configuration[:in] || configuration[:within]
|
17
|
+
|
18
|
+
raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?(:include?)
|
19
|
+
|
20
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
21
|
+
unless enum.include?(value)
|
22
|
+
attr_for = configuration[:for] || attr_name
|
23
|
+
record.errors.add(attr_for, :inclusion, :default => configuration[:message], :value => value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sames as validates_numericality_of but additionally supports :for option
|
29
|
+
# which lets you attach an error to a different attribute.
|
30
|
+
def validates_numericality_of_for(*attr_names)
|
31
|
+
configuration = { :on => :save, :only_integer => false, :allow_nil => false }
|
32
|
+
configuration.update(attr_names.extract_options!)
|
33
|
+
|
34
|
+
numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys
|
35
|
+
|
36
|
+
(numericality_options - [ :odd, :even ]).each do |option|
|
37
|
+
raise ArgumentError, ":#{option} must be a number" unless configuration[option].is_a?(Numeric)
|
38
|
+
end
|
39
|
+
|
40
|
+
validates_each(attr_names,configuration) do |record, attr_name, value|
|
41
|
+
raw_value = record.send("#{attr_name}_before_type_cast") || value
|
42
|
+
|
43
|
+
next if configuration[:allow_nil] and raw_value.nil?
|
44
|
+
|
45
|
+
attr_for = configuration[:for] || attr_name
|
46
|
+
|
47
|
+
if configuration[:only_integer]
|
48
|
+
unless raw_value.to_s =~ /\A[+-]?\d+\Z/
|
49
|
+
record.errors.add(attr_for, :not_a_number, :value => raw_value, :default => configuration[:message])
|
50
|
+
next
|
51
|
+
end
|
52
|
+
raw_value = raw_value.to_i
|
53
|
+
else
|
54
|
+
begin
|
55
|
+
raw_value = Kernel.Float(raw_value)
|
56
|
+
rescue ArgumentError, TypeError
|
57
|
+
record.errors.add(attr_for, :not_a_number, :value => raw_value, :default => configuration[:message])
|
58
|
+
next
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
numericality_options.each do |option|
|
63
|
+
case option
|
64
|
+
when :odd, :even
|
65
|
+
unless raw_value.to_i.method( ALL_NUMERICALITY_CHECKS[option])[]
|
66
|
+
record.errors.add(attr_for, option, :value => raw_value, :default => configuration[:message])
|
67
|
+
end
|
68
|
+
else
|
69
|
+
record.errors.add(attr_for, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless raw_value.method( ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
ActiveRecord::Base.send :extend, GeoTools::Validations
|
data/test/geo_tools_test.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'test_helper'
|
3
|
+
require 'geo_tools'
|
2
4
|
|
3
5
|
class Treasure < ActiveRecord::Base
|
4
6
|
acts_as_location
|
@@ -6,9 +8,19 @@ end
|
|
6
8
|
|
7
9
|
class GeoToolsTest < ActiveSupport::TestCase
|
8
10
|
|
9
|
-
context 'A location
|
11
|
+
context 'A new location' do
|
10
12
|
setup { @treasure = Treasure.new }
|
11
13
|
|
14
|
+
should 'display a sensible #to_s' do
|
15
|
+
assert_equal "00°00.00′, 00°00.00′", @treasure.to_s
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
context 'A location model' do
|
21
|
+
setup { @treasure = Treasure.new }
|
22
|
+
teardown { Treasure.destroy_all }
|
23
|
+
|
12
24
|
should 'convert northern hemisphere latitude fields to a positive float' do
|
13
25
|
@treasure.update_attributes location
|
14
26
|
assert_in_delta 42.95583, @treasure.latitude, 0.0001
|
@@ -33,19 +45,17 @@ class GeoToolsTest < ActiveSupport::TestCase
|
|
33
45
|
@treasure.update_attributes location
|
34
46
|
assert_equal "42°57.35′N, 153°22.27′E", @treasure.to_s
|
35
47
|
end
|
36
|
-
|
37
|
-
teardown { Treasure.destroy_all }
|
38
48
|
end
|
39
49
|
|
40
50
|
context 'Location#within' do
|
41
|
-
# TODO: use Factory Girl.
|
42
|
-
|
43
51
|
context 'NE quadrant' do
|
44
52
|
setup do
|
45
|
-
@a = Treasure.create :latitude_degrees =>
|
46
|
-
@b = Treasure.create :latitude_degrees =>
|
47
|
-
@c = Treasure.create :latitude_degrees =>
|
53
|
+
@a = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'N', :longitude_degrees => 153, :longitude_hemisphere => 'E', :latitude_minutes => 12, :longitude_minutes => 47
|
54
|
+
@b = Treasure.create :latitude_degrees => 43, :latitude_hemisphere => 'N', :longitude_degrees => 153, :longitude_hemisphere => 'E'
|
55
|
+
@c = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'N', :longitude_degrees => 154, :longitude_hemisphere => 'E'
|
48
56
|
end
|
57
|
+
teardown { Treasure.destroy_all }
|
58
|
+
|
49
59
|
should 'return locations to nearest minute' do
|
50
60
|
assert_same_elements [], Treasure.within(1, 1, 42, 153)
|
51
61
|
assert_same_elements [@a, @b, @c], Treasure.within(1, 1, 43, 154)
|
@@ -54,15 +64,16 @@ class GeoToolsTest < ActiveSupport::TestCase
|
|
54
64
|
assert_same_elements [@b], Treasure.within(1, 1, 43, f(153, 46))
|
55
65
|
assert_same_elements [@a, @b], Treasure.within(1, 1, 43, f(153, 47))
|
56
66
|
end
|
57
|
-
teardown { Treasure.destroy_all }
|
58
67
|
end
|
59
68
|
|
60
69
|
context 'NW quadrant' do
|
61
70
|
setup do
|
62
|
-
@a = Treasure.create :latitude_degrees =>
|
63
|
-
@b = Treasure.create :latitude_degrees =>
|
64
|
-
@c = Treasure.create :latitude_degrees =>
|
71
|
+
@a = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'N', :longitude_degrees => 153, :longitude_hemisphere => 'W', :latitude_minutes => 12, :longitude_minutes => 47
|
72
|
+
@b = Treasure.create :latitude_degrees => 43, :latitude_hemisphere => 'N', :longitude_degrees => 153, :longitude_hemisphere => 'W'
|
73
|
+
@c = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'N', :longitude_degrees => 154, :longitude_hemisphere => 'W'
|
65
74
|
end
|
75
|
+
teardown { Treasure.destroy_all }
|
76
|
+
|
66
77
|
should 'return locations to nearest minute' do
|
67
78
|
assert_same_elements [], Treasure.within(1, -153, 42, -1)
|
68
79
|
assert_same_elements [@a, @b, @c], Treasure.within(1, -154, 43, -1)
|
@@ -71,15 +82,16 @@ class GeoToolsTest < ActiveSupport::TestCase
|
|
71
82
|
assert_same_elements [@b], Treasure.within(1, f(-153, 46), 43, -1)
|
72
83
|
assert_same_elements [@a, @b], Treasure.within(1, f(-153, 47), 43, -1)
|
73
84
|
end
|
74
|
-
teardown { Treasure.destroy_all }
|
75
85
|
end
|
76
86
|
|
77
87
|
context 'SE quadrant' do
|
78
88
|
setup do
|
79
|
-
@a = Treasure.create :latitude_degrees =>
|
80
|
-
@b = Treasure.create :latitude_degrees =>
|
81
|
-
@c = Treasure.create :latitude_degrees =>
|
89
|
+
@a = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'S', :longitude_degrees => 153, :longitude_hemisphere => 'E', :latitude_minutes => 12, :longitude_minutes => 47
|
90
|
+
@b = Treasure.create :latitude_degrees => 43, :latitude_hemisphere => 'S', :longitude_degrees => 153, :longitude_hemisphere => 'E'
|
91
|
+
@c = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'S', :longitude_degrees => 154, :longitude_hemisphere => 'E'
|
82
92
|
end
|
93
|
+
teardown { Treasure.destroy_all }
|
94
|
+
|
83
95
|
should 'return locations to nearest minute' do
|
84
96
|
assert_same_elements [], Treasure.within(-42, 1, -1, 153)
|
85
97
|
assert_same_elements [@a, @b, @c], Treasure.within(-43, 1, -1, 154)
|
@@ -88,15 +100,16 @@ class GeoToolsTest < ActiveSupport::TestCase
|
|
88
100
|
assert_same_elements [@b], Treasure.within(-43, 1, -1, f(153, 46))
|
89
101
|
assert_same_elements [@a, @b], Treasure.within(-43, 1, -1, f(153, 47))
|
90
102
|
end
|
91
|
-
teardown { Treasure.destroy_all }
|
92
103
|
end
|
93
104
|
|
94
105
|
context 'SW quadrant' do
|
95
106
|
setup do
|
96
|
-
@a = Treasure.create :latitude_degrees =>
|
97
|
-
@b = Treasure.create :latitude_degrees =>
|
98
|
-
@c = Treasure.create :latitude_degrees =>
|
107
|
+
@a = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'S', :longitude_degrees => 153, :longitude_hemisphere => 'W', :latitude_minutes => 12, :longitude_minutes => 47
|
108
|
+
@b = Treasure.create :latitude_degrees => 43, :latitude_hemisphere => 'S', :longitude_degrees => 153, :longitude_hemisphere => 'W'
|
109
|
+
@c = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'S', :longitude_degrees => 154, :longitude_hemisphere => 'W'
|
99
110
|
end
|
111
|
+
teardown { Treasure.destroy_all }
|
112
|
+
|
100
113
|
should 'return locations to nearest minute' do
|
101
114
|
assert_same_elements [], Treasure.within(-42, -153, -1, -1)
|
102
115
|
assert_same_elements [@a, @b, @c], Treasure.within(-43, -154, -1, -1)
|
@@ -105,16 +118,17 @@ class GeoToolsTest < ActiveSupport::TestCase
|
|
105
118
|
assert_same_elements [@b], Treasure.within(-43, f(-153, 46), -1, -1)
|
106
119
|
assert_same_elements [@a, @b], Treasure.within(-43, f(-153, 47), -1, -1)
|
107
120
|
end
|
108
|
-
teardown { Treasure.destroy_all }
|
109
121
|
end
|
110
122
|
|
111
123
|
context 'straddling equator and prime meridian' do
|
112
124
|
setup do
|
113
|
-
@a = Treasure.create :latitude_degrees =>
|
114
|
-
@b = Treasure.create :latitude_degrees =>
|
115
|
-
@c = Treasure.create :latitude_degrees =>
|
116
|
-
@d = Treasure.create :latitude_degrees =>
|
125
|
+
@a = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'N', :longitude_degrees => 153, :longitude_hemisphere => 'E', :latitude_minutes => 12, :longitude_minutes => 47
|
126
|
+
@b = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'N', :longitude_degrees => 153, :longitude_hemisphere => 'W', :latitude_minutes => 12, :longitude_minutes => 47
|
127
|
+
@c = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'S', :longitude_degrees => 153, :longitude_hemisphere => 'E', :latitude_minutes => 12, :longitude_minutes => 47
|
128
|
+
@d = Treasure.create :latitude_degrees => 42, :latitude_hemisphere => 'S', :longitude_degrees => 153, :longitude_hemisphere => 'W', :latitude_minutes => 12, :longitude_minutes => 47
|
117
129
|
end
|
130
|
+
teardown { Treasure.destroy_all }
|
131
|
+
|
118
132
|
should 'return locations to nearest degree' do
|
119
133
|
assert_same_elements [], Treasure.within(-42, -153, 42, 153)
|
120
134
|
assert_same_elements [@a, @b, @c, @d], Treasure.within(-43, -154, 43, 154)
|
@@ -131,15 +145,11 @@ class GeoToolsTest < ActiveSupport::TestCase
|
|
131
145
|
assert_same_elements [@b, @d], Treasure.within(-43, -154, 43, f(153, 46))
|
132
146
|
assert_same_elements [@a, @b, @c, @d], Treasure.within(-43, -154, 43, f(153, 47))
|
133
147
|
end
|
134
|
-
teardown { Treasure.destroy_all }
|
135
148
|
end
|
136
149
|
end
|
137
150
|
|
138
|
-
|
139
151
|
private
|
140
152
|
|
141
|
-
# TODO: use FactoryGirl instead.
|
142
|
-
|
143
153
|
def location(params = {})
|
144
154
|
{ :latitude_degrees => 42,
|
145
155
|
:latitude_minutes => 57,
|
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
|
3
1
|
require 'test/unit'
|
4
|
-
require 'shoulda'
|
2
|
+
require 'shoulda-context'
|
5
3
|
|
6
4
|
require 'active_record'
|
7
5
|
require 'action_view'
|
8
6
|
require 'active_support'
|
9
7
|
require 'active_support/test_case'
|
10
8
|
|
11
|
-
require 'lib/geo_tools'
|
12
9
|
|
13
10
|
ActiveRecord::Base.establish_connection(
|
14
11
|
:adapter => "sqlite3",
|
@@ -20,3 +17,19 @@ class ActiveSupport::TestCase
|
|
20
17
|
# FIXME: why won't this work?
|
21
18
|
#self.use_transactional_fixtures = true
|
22
19
|
end
|
20
|
+
|
21
|
+
class Test::Unit::TestCase
|
22
|
+
|
23
|
+
# Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
|
24
|
+
def assert_same_elements(a1, a2, msg=nil)
|
25
|
+
[:select, :inject, :size].each do |m|
|
26
|
+
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
|
27
|
+
end
|
28
|
+
|
29
|
+
assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
|
30
|
+
assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
|
31
|
+
|
32
|
+
assert_equal(a1h, a2h, msg)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
CHANGED
@@ -1,71 +1,96 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo_tools
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- Andy Stewart
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
date: 2012-02-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &2153836380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2153836380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3-ruby
|
27
|
+
requirement: &2153835940 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2153835940
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: shoulda-context
|
38
|
+
requirement: &2153830760 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2153830760
|
47
|
+
description: Makes using latitudes and longitudes on forms easier.
|
48
|
+
email:
|
49
|
+
- boss@airbladesoftware.com
|
18
50
|
executables: []
|
19
|
-
|
20
51
|
extensions: []
|
21
|
-
|
22
|
-
|
23
|
-
-
|
24
|
-
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rbenv-version
|
56
|
+
- Gemfile
|
25
57
|
- MIT-LICENSE
|
26
58
|
- README.md
|
27
59
|
- Rakefile
|
28
|
-
-
|
29
|
-
- init.rb
|
30
|
-
- install.rb
|
31
|
-
- lib/air_blade/geo_tools/form_helpers.rb
|
32
|
-
- lib/air_blade/geo_tools/location.rb
|
33
|
-
- lib/air_blade/geo_tools/validations.rb
|
60
|
+
- geo_tools.gemspec
|
34
61
|
- lib/geo_tools.rb
|
35
|
-
-
|
62
|
+
- lib/geo_tools/form_helpers.rb
|
63
|
+
- lib/geo_tools/location.rb
|
64
|
+
- lib/geo_tools/validations.rb
|
65
|
+
- lib/geo_tools/version.rb
|
36
66
|
- test/geo_tools_test.rb
|
37
67
|
- test/schema.rb
|
38
68
|
- test/test_helper.rb
|
39
|
-
|
40
|
-
has_rdoc: true
|
41
|
-
homepage: http://github.com/airblade/geo_tools
|
69
|
+
homepage: https://github.com/airblade/geo_tools
|
42
70
|
licenses: []
|
43
|
-
|
44
71
|
post_install_message:
|
45
|
-
rdoc_options:
|
46
|
-
|
47
|
-
require_paths:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
48
74
|
- lib
|
49
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
61
87
|
requirements: []
|
62
|
-
|
63
|
-
|
64
|
-
rubygems_version: 1.3.5
|
88
|
+
rubyforge_project: geo_tools
|
89
|
+
rubygems_version: 1.8.11
|
65
90
|
signing_key:
|
66
91
|
specification_version: 3
|
67
|
-
summary:
|
68
|
-
test_files:
|
92
|
+
summary: Makes using latitudes and longitudes on forms easier.
|
93
|
+
test_files:
|
69
94
|
- test/geo_tools_test.rb
|
70
95
|
- test/schema.rb
|
71
96
|
- test/test_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.1
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'geo_tools'
|
data/install.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Install hook code here
|
@@ -1,166 +0,0 @@
|
|
1
|
-
module AirBlade
|
2
|
-
module GeoTools
|
3
|
-
|
4
|
-
module FormHelpers
|
5
|
-
|
6
|
-
# Options:
|
7
|
-
# :latitude
|
8
|
-
# :degrees
|
9
|
-
# :symbol
|
10
|
-
# :minutes
|
11
|
-
# :symbol
|
12
|
-
# :decimal_minutes
|
13
|
-
# :symbol
|
14
|
-
# :maxlength
|
15
|
-
#
|
16
|
-
# Assumes the latitude field is called 'latitude'.
|
17
|
-
#
|
18
|
-
# The 'method' argument is for consistency with other field helpers. We don't use it
|
19
|
-
# when using the normal Rails form builder.
|
20
|
-
#
|
21
|
-
# 1/100th of a minute of latitude (or equitorial longitude) is approximately 20m.
|
22
|
-
def latitude_field(method, options = {})
|
23
|
-
opts = {
|
24
|
-
:degrees => { :symbol => '°' },
|
25
|
-
:minutes => { :symbol => '.' },
|
26
|
-
:decimal_minutes => { :symbol => '′', :maxlength => 2 },
|
27
|
-
}
|
28
|
-
lat_options = options.delete :latitude
|
29
|
-
opts.merge! lat_options if lat_options
|
30
|
-
|
31
|
-
output = []
|
32
|
-
|
33
|
-
# Degrees
|
34
|
-
width = 2
|
35
|
-
output << plain_text_field("latitude_degrees",
|
36
|
-
options.merge(:maxlength => width,
|
37
|
-
:value => "%0#{width}d" % @object.send("latitude_degrees")))
|
38
|
-
output << opts[:degrees][:symbol]
|
39
|
-
|
40
|
-
# Minutes
|
41
|
-
width = 2
|
42
|
-
output << plain_text_field("latitude_minutes",
|
43
|
-
options.merge(:maxlength => width,
|
44
|
-
:value => "%0#{width}d" % @object.send("latitude_minutes")))
|
45
|
-
output << opts[:minutes][:symbol]
|
46
|
-
|
47
|
-
# Decimal minutes
|
48
|
-
width = opts[:decimal_minutes][:maxlength]
|
49
|
-
output << plain_text_field("latitude_decimal_minutes",
|
50
|
-
options.merge(:maxlength => width,
|
51
|
-
:value => @object.send("latitude_decimal_minutes_as_string").ljust(width, '0')))
|
52
|
-
output << opts[:decimal_minutes][:symbol]
|
53
|
-
|
54
|
-
# Hemisphere.
|
55
|
-
# Hmm, we pass the options in the html_options position.
|
56
|
-
output << plain_select("latitude_hemisphere", %w( N S ), {}, options)
|
57
|
-
|
58
|
-
output.join "\n"
|
59
|
-
end
|
60
|
-
|
61
|
-
def longitude_field(method, options = {})
|
62
|
-
opts = {
|
63
|
-
:degrees => { :symbol => '°' },
|
64
|
-
:minutes => { :symbol => '.' },
|
65
|
-
:decimal_minutes => { :symbol => '′', :maxlength => 2 },
|
66
|
-
}
|
67
|
-
long_options = options.delete :longitude
|
68
|
-
opts.merge! long_options if long_options
|
69
|
-
|
70
|
-
output = []
|
71
|
-
|
72
|
-
# Degrees
|
73
|
-
width = 3
|
74
|
-
output << plain_text_field("longitude_degrees",
|
75
|
-
options.merge(:maxlength => width,
|
76
|
-
:value => "%0#{width}d" % @object.send("longitude_degrees")))
|
77
|
-
output << opts[:degrees][:symbol]
|
78
|
-
|
79
|
-
# Minutes
|
80
|
-
width = 2
|
81
|
-
output << plain_text_field("longitude_minutes",
|
82
|
-
options.merge(:maxlength => width,
|
83
|
-
:value => "%0#{width}d" % @object.send("longitude_minutes")))
|
84
|
-
output << opts[:minutes][:symbol]
|
85
|
-
|
86
|
-
# Decimal minutes
|
87
|
-
width = opts[:decimal_minutes][:maxlength]
|
88
|
-
output << plain_text_field("longitude_decimal_minutes",
|
89
|
-
options.merge(:maxlength => width,
|
90
|
-
:value => @object.send("longitude_decimal_minutes_as_string").ljust(width, '0')))
|
91
|
-
output << opts[:decimal_minutes][:symbol]
|
92
|
-
|
93
|
-
# Hemisphere.
|
94
|
-
# Hmm, we pass the options in the html_options position.
|
95
|
-
output << plain_select("longitude_hemisphere", %w( E W ), {}, options)
|
96
|
-
|
97
|
-
output.join "\n"
|
98
|
-
end
|
99
|
-
|
100
|
-
# A layer of indirection to allow us always to use a plain field helpers,
|
101
|
-
# regardless of the form builder being used.
|
102
|
-
|
103
|
-
def plain_text_field(*a, &b)
|
104
|
-
text_field(*a, &b)
|
105
|
-
end
|
106
|
-
|
107
|
-
def plain_select(*a, &b)
|
108
|
-
select(*a, &b)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
|
113
|
-
module AirBuddFormHelpers
|
114
|
-
include AirBlade::GeoTools::FormHelpers
|
115
|
-
alias_method :plain_latitude_field, :latitude_field
|
116
|
-
alias_method :plain_longitude_field, :longitude_field
|
117
|
-
|
118
|
-
# Override latitude_field to wrap it with the custom form builder gubbins.
|
119
|
-
# http://github.com/airblade/air_budd_form_builder/tree/master/lib/air_blade/air_budd/form_builder.rb
|
120
|
-
def latitude_field(method, options = {}, html_options = {})
|
121
|
-
@template.content_tag('p',
|
122
|
-
label_element(method, options, html_options) +
|
123
|
-
(
|
124
|
-
plain_latitude_field method, options
|
125
|
-
) +
|
126
|
-
hint_element(options),
|
127
|
-
(errors_for?(method) ? {:class => 'error'} : {})
|
128
|
-
)
|
129
|
-
end
|
130
|
-
|
131
|
-
# Override longitude_field to wrap it with the custom form builder gubbins.
|
132
|
-
# http://github.com/airblade/air_budd_form_builder/tree/master/lib/air_blade/air_budd/form_builder.rb
|
133
|
-
def longitude_field(method, options = {}, html_options = {})
|
134
|
-
@template.content_tag('p',
|
135
|
-
label_element(method, options, html_options) +
|
136
|
-
(
|
137
|
-
plain_longitude_field method, options
|
138
|
-
) +
|
139
|
-
hint_element(options),
|
140
|
-
(errors_for?(method) ? {:class => 'error'} : {})
|
141
|
-
)
|
142
|
-
end
|
143
|
-
|
144
|
-
# Use the standard Rails helpers for text fields and selects.
|
145
|
-
# These are overridden by the AirBudd form builder, so we define
|
146
|
-
# them ourselves.
|
147
|
-
|
148
|
-
def plain_text_field(method, options = {})
|
149
|
-
# From ActionView::Helpers::FormBuilder
|
150
|
-
@template.send('text_field', @object_name, method, objectify_options(options))
|
151
|
-
end
|
152
|
-
def plain_select(method, choices, options = {}, html_options = {})
|
153
|
-
# From ActionView::Helpers::FormOptionsHelper::FormBuilder
|
154
|
-
@template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
|
162
|
-
# Integrate with standard Rails form builder.
|
163
|
-
ActionView::Helpers::FormBuilder.send :include, AirBlade::GeoTools::FormHelpers
|
164
|
-
|
165
|
-
# Integrate with custom AirBudd form builder.
|
166
|
-
AirBlade::AirBudd::FormBuilder.send(:include, AirBlade::GeoTools::AirBuddFormHelpers) rescue nil
|