muck-friends 0.1.16 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/VERSION +1 -1
  2. data/app/controllers/muck/friends_controller.rb +2 -2
  3. data/muck-friends.gemspec +36 -6
  4. data/test/rails_root/Rakefile +8 -0
  5. data/test/rails_root/config/database.yml +14 -15
  6. data/test/rails_root/config/environment.rb +3 -1
  7. data/test/rails_root/config/initializers/geokit_config.rb +63 -0
  8. data/test/rails_root/db/migrate/{20090602041838_create_users.rb → 20090327231918_create_users.rb} +11 -10
  9. data/test/rails_root/db/migrate/20090402234137_create_languages.rb +1 -1
  10. data/test/rails_root/db/migrate/20090818204527_add_activity_indexes.rb +9 -0
  11. data/test/rails_root/db/migrate/20090819030523_add_attachable_to_activities.rb +13 -0
  12. data/test/rails_root/db/migrate/20091124203137_add_location_to_profiles.rb +15 -0
  13. data/test/rails_root/db/migrate/20091124205819_add_fields_to_profiles.rb +21 -0
  14. data/test/rails_root/public/images/fancybox/fancy_shadow_e.png +0 -0
  15. data/test/rails_root/public/images/fancybox/fancy_shadow_n.png +0 -0
  16. data/test/rails_root/public/images/fancybox/fancy_shadow_ne.png +0 -0
  17. data/test/rails_root/public/images/fancybox/fancy_shadow_nw.png +0 -0
  18. data/test/rails_root/public/images/fancybox/fancy_shadow_s.png +0 -0
  19. data/test/rails_root/public/images/fancybox/fancy_shadow_se.png +0 -0
  20. data/test/rails_root/public/images/fancybox/fancy_shadow_sw.png +0 -0
  21. data/test/rails_root/public/images/fancybox/fancy_shadow_w.png +0 -0
  22. data/test/rails_root/public/javascripts/jquery/jquery.fancybox.js +13 -6
  23. data/test/rails_root/public/javascripts/jquery/jquery.tips.js +7 -6
  24. data/test/rails_root/public/javascripts/muck.js +50 -3
  25. data/test/rails_root/public/javascripts/muck_activities.js +4 -5
  26. data/test/rails_root/public/stylesheets/jquery/jquery.fancybox.css +19 -25
  27. data/test/rails_root/public/stylesheets/styles.css +2 -0
  28. data/test/rails_root/test/functional/friends_controller_test.rb +1 -1
  29. data/test/rails_root/vendor/plugins/geokit-rails/init.rb +2 -0
  30. data/test/rails_root/vendor/plugins/geokit-rails/install.rb +14 -0
  31. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails.rb +26 -0
  32. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb +456 -0
  33. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/adapters/abstract.rb +31 -0
  34. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/adapters/mysql.rb +22 -0
  35. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/adapters/postgresql.rb +22 -0
  36. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/adapters/sqlserver.rb +43 -0
  37. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/defaults.rb +22 -0
  38. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/geocoder_control.rb +16 -0
  39. data/test/rails_root/vendor/plugins/geokit-rails/lib/geokit-rails/ip_geocode_lookup.rb +46 -0
  40. data/test/rails_root/vendor/plugins/geokit-rails/test/acts_as_mappable_test.rb +474 -0
  41. data/test/rails_root/vendor/plugins/geokit-rails/test/boot.rb +25 -0
  42. data/test/rails_root/vendor/plugins/geokit-rails/test/ip_geocode_lookup_test.rb +77 -0
  43. data/test/rails_root/vendor/plugins/geokit-rails/test/models/company.rb +3 -0
  44. data/test/rails_root/vendor/plugins/geokit-rails/test/models/custom_location.rb +12 -0
  45. data/test/rails_root/vendor/plugins/geokit-rails/test/models/location.rb +4 -0
  46. data/test/rails_root/vendor/plugins/geokit-rails/test/models/mock_address.rb +4 -0
  47. data/test/rails_root/vendor/plugins/geokit-rails/test/models/mock_family.rb +3 -0
  48. data/test/rails_root/vendor/plugins/geokit-rails/test/models/mock_house.rb +3 -0
  49. data/test/rails_root/vendor/plugins/geokit-rails/test/models/mock_organization.rb +4 -0
  50. data/test/rails_root/vendor/plugins/geokit-rails/test/models/mock_person.rb +4 -0
  51. data/test/rails_root/vendor/plugins/geokit-rails/test/models/store.rb +3 -0
  52. data/test/rails_root/vendor/plugins/geokit-rails/test/schema.rb +60 -0
  53. data/test/rails_root/vendor/plugins/geokit-rails/test/test_helper.rb +23 -0
  54. metadata +33 -4
@@ -1,14 +1,13 @@
1
1
  jQuery(document).ready(function() {
2
- apply_comment_methods();
2
+ apply_activity_ajax_methods();
3
3
  });
4
4
 
5
- function apply_comment_methods(){
5
+ function apply_activity_ajax_methods(){
6
6
  setup_comment_submit();
7
7
  hide_comment_boxes();
8
8
  apply_comment_hover();
9
9
  apply_activity_hover();
10
- jQuery('.activity-no-comments').hide();
11
-
10
+ jQuery('.activity-no-comments').hide();
12
11
  jQuery('.activity-has-comments').find('textarea').click(function(){
13
12
  show_comment_box(this);
14
13
  });
@@ -57,7 +56,7 @@ function setup_comment_submit(){
57
56
  comment_box.removeClass('activity-no-comments');
58
57
  comment_box.addClass('activity-has-comments');
59
58
  comment_box.find('textarea').show();
60
- apply_comment_methods();
59
+ apply_activity_ajax_methods();
61
60
  }
62
61
  });
63
62
  return false;
@@ -1,44 +1,38 @@
1
- html,body{height:100%;}
2
- div#fancy_overlay{position:fixed;top:0;left:0;width:100%;height:100%;background-color:#666;display:none;z-index:30;}
3
- * html div#fancy_overlay{position:absolute;height:expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight :document.body.offsetHeight + 'px');}
4
- div#fancy_wrap{text-align:left;}
1
+ div#fancy_overlay{position:fixed;top:0;left:0;width:100%;height:100%;display:none;z-index:30;}
5
2
  div#fancy_loading{position:absolute;height:40px;width:40px;cursor:pointer;display:none;overflow:hidden;background:transparent;z-index:100;}
6
3
  div#fancy_loading div{position:absolute;top:0;left:0;width:40px;height:480px;background:transparent url('/images/fancybox/fancy_progress.png') no-repeat;}
7
- div#fancy_loading_overlay{position:absolute;background-color:#FFF;z-index:30;}
8
- div#fancy_loading_icon{position:absolute;background:url('/images/fancybox/fancy_loading.gif') no-repeat;z-index:35;width:16px;height:16px;}
9
- div#fancy_outer{position:absolute;top:0;left:0;z-index:90;padding:18px 18px 33px 18px;margin:0;overflow:hidden;background:transparent;display:none;}
10
- div#fancy_inner{position:relative;width:100%;height:100%;border:1px solid #BBB;background:#FFF;}
4
+ div#fancy_outer{position:absolute;top:0;left:0;z-index:90;padding:20px 20px 40px 20px;margin:0;background:transparent;display:none;}
5
+ div#fancy_inner{position:relative;width:100%;height:100%;background:#FFF;}
11
6
  div#fancy_content{margin:0;z-index:100;position:absolute;}
12
7
  div#fancy_div{background:#000;color:#FFF;height:100%;width:100%;z-index:100;}
13
8
  img#fancy_img{position:absolute;top:0;left:0;border:0;padding:0;margin:0;z-index:100;width:100%;height:100%;}
14
9
  div#fancy_close{position:absolute;top:-12px;right:-15px;height:30px;width:30px;background:url('/images/fancybox/fancy_closebox.png') top left no-repeat;cursor:pointer;z-index:181;display:none;}
15
10
  #fancy_frame{position:relative;width:100%;height:100%;display:none;}
16
11
  #fancy_ajax{width:100%;height:100%;overflow:auto;}
17
- a#fancy_left,a#fancy_right{position:absolute;bottom:0px;height:100%;width:35%;cursor:pointer;z-index:111;display:none;background-image:url(data:image/gif;base64,AAAA);outline:none;}
12
+ a#fancy_left,a#fancy_right{position:absolute;bottom:0px;height:100%;width:35%;cursor:pointer;z-index:111;display:none;background-image:url("data:image/gif;base64,AAAA");outline:none;overflow:hidden;}
18
13
  a#fancy_left{left:0px;}
19
14
  a#fancy_right{right:0px;}
20
15
  span.fancy_ico{position:absolute;top:50%;margin-top:-15px;width:30px;height:30px;z-index:112;cursor:pointer;display:block;}
21
16
  span#fancy_left_ico{left:-9999px;background:transparent url('/images/fancybox/fancy_left.png') no-repeat;}
22
17
  span#fancy_right_ico{right:-9999px;background:transparent url('/images/fancybox/fancy_right.png') no-repeat;}
23
- a#fancy_left:hover{visibility:visible;}
24
- a#fancy_right:hover{visibility:visible;}
18
+ a#fancy_left:hover,a#fancy_right:hover{visibility:visible;background-color:transparent;}
25
19
  a#fancy_left:hover span{left:20px;}
26
20
  a#fancy_right:hover span{right:20px;}
27
- .fancy_bigIframe{position:absolute;top:0;left:0;width:100%;height:100%;background:transparent;}
21
+ #fancy_bigIframe{position:absolute;top:0;left:0;width:100%;height:100%;background:transparent;}
28
22
  div#fancy_bg{position:absolute;top:0;left:0;width:100%;height:100%;z-index:70;border:0;padding:0;margin:0;}
29
23
  div.fancy_bg{position:absolute;display:block;z-index:70;border:0;padding:0;margin:0;}
30
- div.fancy_bg_n{top:-18px;width:100%;height:18px;background:transparent url('/images/fancybox/fancy_shadow_n.png') repeat-x;}
31
- div.fancy_bg_ne{top:-18px;right:-13px;width:13px;height:18px;background:transparent url('/images/fancybox/fancy_shadow_ne.png') no-repeat;}
32
- div.fancy_bg_e{right:-13px;height:100%;width:13px;background:transparent url('/images/fancybox/fancy_shadow_e.png') repeat-y;}
33
- div.fancy_bg_se{bottom:-18px;right:-13px;width:13px;height:18px;background:transparent url('/images/fancybox/fancy_shadow_se.png') no-repeat;}
34
- div.fancy_bg_s{bottom:-18px;width:100%;height:18px;background:transparent url('/images/fancybox/fancy_shadow_s.png') repeat-x;}
35
- div.fancy_bg_sw{bottom:-18px;left:-13px;width:13px;height:18px;background:transparent url('/images/fancybox/fancy_shadow_sw.png') no-repeat;}
36
- div.fancy_bg_w{left:-13px;height:100%;width:13px;background:transparent url('/images/fancybox/fancy_shadow_w.png') repeat-y;}
37
- div.fancy_bg_nw{top:-18px;left:-13px;width:13px;height:18px;background:transparent url('/images/fancybox/fancy_shadow_nw.png') no-repeat;}
38
- div#fancy_title{position:absolute;bottom:-33px;left:0;width:100%;z-index:100;display:none;}
39
- div#fancy_title div{color:#FFF;font:bold 12px Arial;padding-bottom:3px;}
24
+ div#fancy_bg_n{top:-20px;left:0;width:100%;height:20px;background:transparent url('/images/fancybox/fancy_shadow_n.png') repeat-x;}
25
+ div#fancy_bg_ne{top:-20px;right:-20px;width:20px;height:20px;background:transparent url('/images/fancybox/fancy_shadow_ne.png') no-repeat;}
26
+ div#fancy_bg_e{right:-20px;height:100%;width:20px;background:transparent url('/images/fancybox/fancy_shadow_e.png') repeat-y;}
27
+ div#fancy_bg_se{bottom:-20px;right:-20px;width:20px;height:20px;background:transparent url('/images/fancybox/fancy_shadow_se.png') no-repeat;}
28
+ div#fancy_bg_s{bottom:-20px;left:0;width:100%;height:20px;background:transparent url('/images/fancybox/fancy_shadow_s.png') repeat-x;}
29
+ div#fancy_bg_sw{bottom:-20px;left:-20px;width:20px;height:20px;background:transparent url('/images/fancybox/fancy_shadow_sw.png') no-repeat;}
30
+ div#fancy_bg_w{left:-20px;height:100%;width:20px;background:transparent url('/images/fancybox/fancy_shadow_w.png') repeat-y;}
31
+ div#fancy_bg_nw{top:-20px;left:-20px;width:20px;height:20px;background:transparent url('/images/fancybox/fancy_shadow_nw.png') no-repeat;}
32
+ div#fancy_title{position:absolute;z-index:100;display:none;}
33
+ div#fancy_title div{color:#FFF;font:bold 12px Arial;padding-bottom:3px;white-space:nowrap;}
40
34
  div#fancy_title table{margin:0 auto;}
41
35
  div#fancy_title table td{padding:0;vertical-align:middle;}
42
- td#fancy_title_left{height:32px;width:15px;background:transparent url(fancy_title_left.png) repeat-x;}
43
- td#fancy_title_main{height:32px;background:transparent url(fancy_title_main.png) repeat-x;}
44
- td#fancy_title_right{height:32px;width:15px;background:transparent url(fancy_title_right.png) repeat-x;}
36
+ td#fancy_title_left{height:32px;width:15px;background:transparent url('/images/fancybox/fancy_title_left.png') repeat-x;}
37
+ td#fancy_title_main{height:32px;background:transparent url('/images/fancybox/fancy_title_main.png') repeat-x;}
38
+ td#fancy_title_right{height:32px;width:15px;background:transparent url('/images/fancybox/fancy_title_right.png') repeat-x;}
@@ -1,6 +1,7 @@
1
1
  /* general */
2
2
  .center{text-align:center;}
3
3
  #popup_wrapper{width:auto;}
4
+ .waiting{margin:3px 0;padding-left:20px;background:transparent url('/images/spinner.gif') no-repeat scroll left bottom;}
4
5
 
5
6
  /* forms */
6
7
  input{margin:0 10px 5px 0;padding:4px;font-size:1.3em;}
@@ -23,6 +24,7 @@ form fieldset input[type="text"],form fieldset input[type="password"]{width:500p
23
24
  .button-link {background-color:transparent;border-top-width: 0px;border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;}
24
25
 
25
26
  /*images*/
27
+ .tiny{width:24px;height:24px;}
26
28
  .icon{width:50px;height:50px;}
27
29
  .thumb{width:100px;height:100px;}
28
30
  .medium{width:300px;height:300px;}
@@ -114,7 +114,7 @@ class Muck::FriendsControllerTest < ActionController::TestCase
114
114
  Friend.add_follower(@quentin, @aaron)
115
115
  Friend.make_friends(@quentin, @aaron)
116
116
  @quentin.block_user(@aaron)
117
- post :update, { :id => @aaron.to_param, :block => false, :format=>'js'}
117
+ post :update, { :id => @aaron.to_param, :unblock => true, :format=>'js' }
118
118
  end
119
119
  should_respond_with :success
120
120
  should_not_set_the_flash
@@ -0,0 +1,2 @@
1
+ require 'geokit'
2
+ require 'geokit-rails'
@@ -0,0 +1,14 @@
1
+ # Display to the console the contents of the README file.
2
+ puts IO.read(File.join(File.dirname(__FILE__), 'README.markdown'))
3
+
4
+ # place the api_keys_template in the application's /config/initializers/geokit_config.rb
5
+ path=File.expand_path(File.join(File.dirname(__FILE__), '../../../config/initializers/geokit_config.rb'))
6
+ template_path=File.join(File.dirname(__FILE__), '/assets/api_keys_template')
7
+ if File.exists?(path)
8
+ puts "It looks like you already have a configuration file at #{path}. We've left it as-is. Recommended: check #{template_path} to see if anything has changed, and update config file accordingly."
9
+ else
10
+ File.open(path, "w") do |f|
11
+ f.puts IO.read(template_path)
12
+ puts "We created a configuration file for you in config/initializers/geokit_config.rb. Add your Google API keys, etc there."
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ # Load modules and classes needed to automatically mix in ActiveRecord and
2
+ # ActionController helpers. All other functionality must be explicitly
3
+ # required.
4
+ #
5
+ # Note that we don't explicitly require the geokit gem.
6
+ # You should specify gem dependencies in your config/environment.rb: config.gem "geokit"
7
+ #
8
+ if defined? Geokit
9
+ require 'geokit-rails/defaults'
10
+ require 'geokit-rails/adapters/abstract'
11
+ require 'geokit-rails/acts_as_mappable'
12
+ require 'geokit-rails/ip_geocode_lookup'
13
+
14
+ # Automatically mix in distance finder support into ActiveRecord classes.
15
+ ActiveRecord::Base.send :include, GeoKit::ActsAsMappable
16
+
17
+ # Automatically mix in ip geocoding helpers into ActionController classes.
18
+ ActionController::Base.send :include, GeoKit::IpGeocodeLookup
19
+ else
20
+ message=%q(WARNING: geokit-rails requires the Geokit gem. You either don't have the gem installed,
21
+ or you haven't told Rails to require it. If you're using a recent version of Rails:
22
+ config.gem "geokit" # in config/environment.rb
23
+ and of course install the gem: sudo gem install geokit)
24
+ puts message
25
+ Rails.logger.error message
26
+ end
@@ -0,0 +1,456 @@
1
+ module Geokit
2
+ # Contains the class method acts_as_mappable targeted to be mixed into ActiveRecord.
3
+ # When mixed in, augments find services such that they provide distance calculation
4
+ # query services. The find method accepts additional options:
5
+ #
6
+ # * :origin - can be
7
+ # 1. a two-element array of latititude/longitude -- :origin=>[37.792,-122.393]
8
+ # 2. a geocodeable string -- :origin=>'100 Spear st, San Francisco, CA'
9
+ # 3. an object which responds to lat and lng methods, or latitude and longitude methods,
10
+ # or whatever methods you have specified for lng_column_name and lat_column_name
11
+ #
12
+ # Other finder methods are provided for specific queries. These are:
13
+ #
14
+ # * find_within (alias: find_inside)
15
+ # * find_beyond (alias: find_outside)
16
+ # * find_closest (alias: find_nearest)
17
+ # * find_farthest
18
+ #
19
+ # Counter methods are available and work similarly to finders.
20
+ #
21
+ # If raw SQL is desired, the distance_sql method can be used to obtain SQL appropriate
22
+ # to use in a find_by_sql call.
23
+ module ActsAsMappable
24
+ class UnsupportedAdapter < StandardError ; end
25
+
26
+ # Mix below class methods into ActiveRecord.
27
+ def self.included(base) # :nodoc:
28
+ base.extend ClassMethods
29
+ end
30
+
31
+ # Class method to mix into active record.
32
+ module ClassMethods # :nodoc:
33
+
34
+ # Class method to bring distance query support into ActiveRecord models. By default
35
+ # uses :miles for distance units and performs calculations based upon the Haversine
36
+ # (sphere) formula. These can be changed by setting Geokit::default_units and
37
+ # Geokit::default_formula. Also, by default, uses lat, lng, and distance for respective
38
+ # column names. All of these can be overridden using the :default_units, :default_formula,
39
+ # :lat_column_name, :lng_column_name, and :distance_column_name hash keys.
40
+ #
41
+ # Can also use to auto-geocode a specific column on create. Syntax;
42
+ #
43
+ # acts_as_mappable :auto_geocode=>true
44
+ #
45
+ # By default, it tries to geocode the "address" field. Or, for more customized behavior:
46
+ #
47
+ # acts_as_mappable :auto_geocode=>{:field=>:address,:error_message=>'bad address'}
48
+ #
49
+ # In both cases, it creates a before_validation_on_create callback to geocode the given column.
50
+ # For anything more customized, we recommend you forgo the auto_geocode option
51
+ # and create your own AR callback to handle geocoding.
52
+ def acts_as_mappable(options = {})
53
+ metaclass = (class << self; self; end)
54
+
55
+ # Mix in the module, but ensure to do so just once.
56
+ return if !defined?(Geokit::Mappable) || metaclass.included_modules.include?(Geokit::ActsAsMappable::SingletonMethods)
57
+
58
+ send :extend, Geokit::ActsAsMappable::SingletonMethods
59
+ send :include, Geokit::Mappable
60
+
61
+ cattr_accessor :through
62
+ self.through = options[:through]
63
+
64
+ if reflection = Geokit::ActsAsMappable.end_of_reflection_chain(self.through, self)
65
+ metaclass.instance_eval do
66
+ [ :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name ].each do |method_name|
67
+ define_method method_name do
68
+ reflection.klass.send(method_name)
69
+ end
70
+ end
71
+ end
72
+ else
73
+ cattr_accessor :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name
74
+
75
+ self.distance_column_name = options[:distance_column_name] || 'distance'
76
+ self.default_units = options[:default_units] || Geokit::default_units
77
+ self.default_formula = options[:default_formula] || Geokit::default_formula
78
+ self.lat_column_name = options[:lat_column_name] || 'lat'
79
+ self.lng_column_name = options[:lng_column_name] || 'lng'
80
+ self.qualified_lat_column_name = "#{table_name}.#{lat_column_name}"
81
+ self.qualified_lng_column_name = "#{table_name}.#{lng_column_name}"
82
+
83
+ if options.include?(:auto_geocode) && options[:auto_geocode]
84
+ # if the form auto_geocode=>true is used, let the defaults take over by suppling an empty hash
85
+ options[:auto_geocode] = {} if options[:auto_geocode] == true
86
+ cattr_accessor :auto_geocode_field, :auto_geocode_error_message
87
+ self.auto_geocode_field = options[:auto_geocode][:field] || 'address'
88
+ self.auto_geocode_error_message = options[:auto_geocode][:error_message] || 'could not locate address'
89
+
90
+ # set the actual callback here
91
+ before_validation_on_create :auto_geocode_address
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ # this is the callback for auto_geocoding
98
+ def auto_geocode_address
99
+ address=self.send(auto_geocode_field).to_s
100
+ geo=Geokit::Geocoders::MultiGeocoder.geocode(address)
101
+
102
+ if geo.success
103
+ self.send("#{lat_column_name}=", geo.lat)
104
+ self.send("#{lng_column_name}=", geo.lng)
105
+ else
106
+ errors.add(auto_geocode_field, auto_geocode_error_message)
107
+ end
108
+
109
+ geo.success
110
+ end
111
+
112
+ def self.end_of_reflection_chain(through, klass)
113
+ while through
114
+ reflection = nil
115
+ if through.is_a?(Hash)
116
+ association, through = through.to_a.first
117
+ else
118
+ association, through = through, nil
119
+ end
120
+
121
+ if reflection = klass.reflect_on_association(association)
122
+ klass = reflection.klass
123
+ else
124
+ raise ArgumentError, "You gave #{association} in :through, but I could not find it on #{klass}."
125
+ end
126
+ end
127
+
128
+ reflection
129
+ end
130
+
131
+ # Instance methods to mix into ActiveRecord.
132
+ module SingletonMethods #:nodoc:
133
+
134
+ # A proxy to an instance of a finder adapter, inferred from the connection's adapter.
135
+ def adapter
136
+ @adapter ||= begin
137
+ require File.join(File.dirname(__FILE__), 'adapters', connection.adapter_name.downcase)
138
+ klass = Adapters.const_get(connection.adapter_name.camelcase)
139
+ klass.load(self) unless klass.loaded
140
+ klass.new(self)
141
+ rescue LoadError
142
+ raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter."
143
+ end
144
+ end
145
+
146
+ # Extends the existing find method in potentially two ways:
147
+ # - If a mappable instance exists in the options, adds a distance column.
148
+ # - If a mappable instance exists in the options and the distance column exists in the
149
+ # conditions, substitutes the distance sql for the distance column -- this saves
150
+ # having to write the gory SQL.
151
+ def find(*args)
152
+ prepare_for_find_or_count(:find, args)
153
+ super(*args)
154
+ end
155
+
156
+ # Extends the existing count method by:
157
+ # - If a mappable instance exists in the options and the distance column exists in the
158
+ # conditions, substitutes the distance sql for the distance column -- this saves
159
+ # having to write the gory SQL.
160
+ def count(*args)
161
+ prepare_for_find_or_count(:count, args)
162
+ super(*args)
163
+ end
164
+
165
+ # Finds within a distance radius.
166
+ def find_within(distance, options={})
167
+ options[:within] = distance
168
+ find(:all, options)
169
+ end
170
+ alias find_inside find_within
171
+
172
+ # Finds beyond a distance radius.
173
+ def find_beyond(distance, options={})
174
+ options[:beyond] = distance
175
+ find(:all, options)
176
+ end
177
+ alias find_outside find_beyond
178
+
179
+ # Finds according to a range. Accepts inclusive or exclusive ranges.
180
+ def find_by_range(range, options={})
181
+ options[:range] = range
182
+ find(:all, options)
183
+ end
184
+
185
+ # Finds the closest to the origin.
186
+ def find_closest(options={})
187
+ find(:nearest, options)
188
+ end
189
+ alias find_nearest find_closest
190
+
191
+ # Finds the farthest from the origin.
192
+ def find_farthest(options={})
193
+ find(:farthest, options)
194
+ end
195
+
196
+ # Finds within rectangular bounds (sw,ne).
197
+ def find_within_bounds(bounds, options={})
198
+ options[:bounds] = bounds
199
+ find(:all, options)
200
+ end
201
+
202
+ # counts within a distance radius.
203
+ def count_within(distance, options={})
204
+ options[:within] = distance
205
+ count(options)
206
+ end
207
+ alias count_inside count_within
208
+
209
+ # Counts beyond a distance radius.
210
+ def count_beyond(distance, options={})
211
+ options[:beyond] = distance
212
+ count(options)
213
+ end
214
+ alias count_outside count_beyond
215
+
216
+ # Counts according to a range. Accepts inclusive or exclusive ranges.
217
+ def count_by_range(range, options={})
218
+ options[:range] = range
219
+ count(options)
220
+ end
221
+
222
+ # Finds within rectangular bounds (sw,ne).
223
+ def count_within_bounds(bounds, options={})
224
+ options[:bounds] = bounds
225
+ count(options)
226
+ end
227
+
228
+ # Returns the distance calculation to be used as a display column or a condition. This
229
+ # is provide for anyone wanting access to the raw SQL.
230
+ def distance_sql(origin, units=default_units, formula=default_formula)
231
+ case formula
232
+ when :sphere
233
+ sql = sphere_distance_sql(origin, units)
234
+ when :flat
235
+ sql = flat_distance_sql(origin, units)
236
+ end
237
+ sql
238
+ end
239
+
240
+ private
241
+
242
+ # Prepares either a find or a count action by parsing through the options and
243
+ # conditionally adding to the select clause for finders.
244
+ def prepare_for_find_or_count(action, args)
245
+ options = args.extract_options!
246
+ #options = defined?(args.extract_options!) ? args.extract_options! : extract_options_from_args!(args)
247
+ # Obtain items affecting distance condition.
248
+ origin = extract_origin_from_options(options)
249
+ units = extract_units_from_options(options)
250
+ formula = extract_formula_from_options(options)
251
+ bounds = extract_bounds_from_options(options)
252
+
253
+ # Only proceed if this is a geokit-related query
254
+ if origin || bounds
255
+ # if no explicit bounds were given, try formulating them from the point and distance given
256
+ bounds = formulate_bounds_from_distance(options, origin, units) unless bounds
257
+ # Apply select adjustments based upon action.
258
+ add_distance_to_select(options, origin, units, formula) if origin && action == :find
259
+ # Apply the conditions for a bounding rectangle if applicable
260
+ apply_bounds_conditions(options,bounds) if bounds
261
+ # Apply distance scoping and perform substitutions.
262
+ apply_distance_scope(options)
263
+ substitute_distance_in_conditions(options, origin, units, formula) if origin && options.has_key?(:conditions)
264
+ # Order by scoping for find action.
265
+ apply_find_scope(args, options) if action == :find
266
+ # Handle :through
267
+ apply_include_for_through(options)
268
+ # Unfortunatley, we need to do extra work if you use an :include. See the method for more info.
269
+ handle_order_with_include(options,origin,units,formula) if options.include?(:include) && options.include?(:order) && origin
270
+ end
271
+
272
+ # Restore options minus the extra options that we used for the
273
+ # Geokit API.
274
+ args.push(options)
275
+ end
276
+
277
+ def apply_include_for_through(options)
278
+ if self.through
279
+ case options[:include]
280
+ when Array
281
+ options[:include] << self.through
282
+ when Hash, String, Symbol
283
+ options[:include] = [ self.through, options[:include] ]
284
+ else
285
+ options[:include] = [ self.through ]
286
+ end
287
+ end
288
+ end
289
+
290
+ # If we're here, it means that 1) an origin argument, 2) an :include, 3) an :order clause were supplied.
291
+ # Now we have to sub some SQL into the :order clause. The reason is that when you do an :include,
292
+ # ActiveRecord drops the psuedo-column (specificically, distance) which we supplied for :select.
293
+ # So, the 'distance' column isn't available for the :order clause to reference when we use :include.
294
+ def handle_order_with_include(options, origin, units, formula)
295
+ # replace the distance_column_name with the distance sql in order clause
296
+ options[:order].sub!(distance_column_name, distance_sql(origin, units, formula))
297
+ end
298
+
299
+ # Looks for mapping-specific tokens and makes appropriate translations so that the
300
+ # original finder has its expected arguments. Resets the the scope argument to
301
+ # :first and ensures the limit is set to one.
302
+ def apply_find_scope(args, options)
303
+ case args.first
304
+ when :nearest, :closest
305
+ args[0] = :first
306
+ options[:limit] = 1
307
+ options[:order] = "#{distance_column_name} ASC"
308
+ when :farthest
309
+ args[0] = :first
310
+ options[:limit] = 1
311
+ options[:order] = "#{distance_column_name} DESC"
312
+ end
313
+ end
314
+
315
+ # If it's a :within query, add a bounding box to improve performance.
316
+ # This only gets called if a :bounds argument is not otherwise supplied.
317
+ def formulate_bounds_from_distance(options, origin, units)
318
+ distance = options[:within] if options.has_key?(:within)
319
+ distance = options[:range].last-(options[:range].exclude_end?? 1 : 0) if options.has_key?(:range)
320
+ if distance
321
+ res=Geokit::Bounds.from_point_and_radius(origin,distance,:units=>units)
322
+ else
323
+ nil
324
+ end
325
+ end
326
+
327
+ # Replace :within, :beyond and :range distance tokens with the appropriate distance
328
+ # where clauses. Removes these tokens from the options hash.
329
+ def apply_distance_scope(options)
330
+ distance_condition = if options.has_key?(:within)
331
+ "#{distance_column_name} <= #{options[:within]}"
332
+ elsif options.has_key?(:beyond)
333
+ "#{distance_column_name} > #{options[:beyond]}"
334
+ elsif options.has_key?(:range)
335
+ "#{distance_column_name} >= #{options[:range].first} AND #{distance_column_name} <#{'=' unless options[:range].exclude_end?} #{options[:range].last}"
336
+ end
337
+
338
+ if distance_condition
339
+ [:within, :beyond, :range].each { |option| options.delete(option) }
340
+ options[:conditions] = merge_conditions(options[:conditions], distance_condition)
341
+ end
342
+ end
343
+
344
+ # Alters the conditions to include rectangular bounds conditions.
345
+ def apply_bounds_conditions(options,bounds)
346
+ sw,ne = bounds.sw, bounds.ne
347
+ lng_sql = bounds.crosses_meridian? ? "(#{qualified_lng_column_name}<#{ne.lng} OR #{qualified_lng_column_name}>#{sw.lng})" : "#{qualified_lng_column_name}>#{sw.lng} AND #{qualified_lng_column_name}<#{ne.lng}"
348
+ bounds_sql = "#{qualified_lat_column_name}>#{sw.lat} AND #{qualified_lat_column_name}<#{ne.lat} AND #{lng_sql}"
349
+ options[:conditions] = merge_conditions(options[:conditions], bounds_sql)
350
+ end
351
+
352
+ # Extracts the origin instance out of the options if it exists and returns
353
+ # it. If there is no origin, looks for latitude and longitude values to
354
+ # create an origin. The side-effect of the method is to remove these
355
+ # option keys from the hash.
356
+ def extract_origin_from_options(options)
357
+ origin = options.delete(:origin)
358
+ res = normalize_point_to_lat_lng(origin) if origin
359
+ res
360
+ end
361
+
362
+ # Extract the units out of the options if it exists and returns it. If
363
+ # there is no :units key, it uses the default. The side effect of the
364
+ # method is to remove the :units key from the options hash.
365
+ def extract_units_from_options(options)
366
+ units = options[:units] || default_units
367
+ options.delete(:units)
368
+ units
369
+ end
370
+
371
+ # Extract the formula out of the options if it exists and returns it. If
372
+ # there is no :formula key, it uses the default. The side effect of the
373
+ # method is to remove the :formula key from the options hash.
374
+ def extract_formula_from_options(options)
375
+ formula = options[:formula] || default_formula
376
+ options.delete(:formula)
377
+ formula
378
+ end
379
+
380
+ def extract_bounds_from_options(options)
381
+ bounds = options.delete(:bounds)
382
+ bounds = Geokit::Bounds.normalize(bounds) if bounds
383
+ end
384
+
385
+ # Geocode IP address.
386
+ def geocode_ip_address(origin)
387
+ geo_location = Geokit::Geocoders::MultiGeocoder.geocode(origin)
388
+ return geo_location if geo_location.success
389
+ raise Geokit::Geocoders::GeocodeError
390
+ end
391
+
392
+ # Given a point in a variety of (an address to geocode,
393
+ # an array of [lat,lng], or an object with appropriate lat/lng methods, an IP addres)
394
+ # this method will normalize it into a Geokit::LatLng instance. The only thing this
395
+ # method adds on top of LatLng#normalize is handling of IP addresses
396
+ def normalize_point_to_lat_lng(point)
397
+ res = geocode_ip_address(point) if point.is_a?(String) && /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(point)
398
+ res = Geokit::LatLng.normalize(point) unless res
399
+ res
400
+ end
401
+
402
+ # Augments the select with the distance SQL.
403
+ def add_distance_to_select(options, origin, units=default_units, formula=default_formula)
404
+ if origin
405
+ distance_selector = distance_sql(origin, units, formula) + " AS #{distance_column_name}"
406
+ selector = options.has_key?(:select) && options[:select] ? options[:select] : "*"
407
+ options[:select] = "#{selector}, #{distance_selector}"
408
+ end
409
+ end
410
+
411
+ # Looks for the distance column and replaces it with the distance sql. If an origin was not
412
+ # passed in and the distance column exists, we leave it to be flagged as bad SQL by the database.
413
+ # Conditions are either a string or an array. In the case of an array, the first entry contains
414
+ # the condition.
415
+ def substitute_distance_in_conditions(options, origin, units=default_units, formula=default_formula)
416
+ condition = options[:conditions].is_a?(String) ? options[:conditions] : options[:conditions].first
417
+ pattern = Regexp.new("\\b#{distance_column_name}\\b")
418
+ condition.gsub!(pattern, distance_sql(origin, units, formula))
419
+ end
420
+
421
+ # Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
422
+ # to the database in use.
423
+ def sphere_distance_sql(origin, units)
424
+ lat = deg2rad(origin.lat)
425
+ lng = deg2rad(origin.lng)
426
+ multiplier = units_sphere_multiplier(units)
427
+
428
+ adapter.sphere_distance_sql(lat, lng, multiplier) if adapter
429
+ end
430
+
431
+ # Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
432
+ # to the database in use.
433
+ def flat_distance_sql(origin, units)
434
+ lat_degree_units = units_per_latitude_degree(units)
435
+ lng_degree_units = units_per_longitude_degree(origin.lat, units)
436
+
437
+ adapter.flat_distance_sql(origin, lat_degree_units, lng_degree_units)
438
+ end
439
+ end
440
+ end
441
+ end
442
+
443
+ # Extend Array with a sort_by_distance method.
444
+ class Array
445
+ # This method creates a "distance" attribute on each object, calculates the
446
+ # distance from the passed origin, and finally sorts the array by the
447
+ # resulting distance.
448
+ def sort_by_distance_from(origin, opts={})
449
+ distance_attribute_name = opts.delete(:distance_attribute_name) || 'distance'
450
+ self.each do |e|
451
+ e.class.send(:attr_accessor, distance_attribute_name) if !e.respond_to?("#{distance_attribute_name}=")
452
+ e.send("#{distance_attribute_name}=", e.distance_to(origin,opts))
453
+ end
454
+ self.sort!{|a,b|a.send(distance_attribute_name) <=> b.send(distance_attribute_name)}
455
+ end
456
+ end