Skip to content

Commit

Permalink
Use #first and #last rather than #[] for lat/lng pairs
Browse files Browse the repository at this point in the history
ActiveRecord::Base responds to #[], as an alternate way of accessing
attributes (who knew?).

[sunspot#69 state:resolved]
  • Loading branch information
Mat Brown committed Mar 10, 2010
1 parent 351cd58 commit 8c813c2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sunspot/lib/sunspot/util.rb
Expand Up @@ -187,8 +187,8 @@ def initialize(coords)
end

def lat
if @coords.respond_to?(:[])
@coords[0]
if @coords.respond_to?(:first)
@coords.first
elsif @coords.respond_to?(:lat)
@coords.lat
else
Expand All @@ -197,8 +197,8 @@ def lat
end

def lng
if @coords.respond_to?(:[])
@coords[1]
if @coords.respond_to?(:last)
@coords.last
elsif @coords.respond_to?(:lng)
@coords.lng
elsif @coords.respond_to?(:lon)
Expand Down
2 changes: 2 additions & 0 deletions sunspot_rails/spec/mock_app/app/models/location.rb
@@ -0,0 +1,2 @@
class Location < ActiveRecord::Base
end
3 changes: 3 additions & 0 deletions sunspot_rails/spec/mock_app/app/models/post.rb
@@ -1,5 +1,8 @@
class Post < ActiveRecord::Base
belongs_to :location

searchable :auto_index => false, :auto_remove => false do
string :title
coordinates :location
end
end
8 changes: 8 additions & 0 deletions sunspot_rails/spec/model_spec.rb
Expand Up @@ -154,6 +154,14 @@
end.results.should == [@post]
end

it 'should use an ActiveRecord object for coordinates' do
post = Post.new(:title => 'Test Post')
post.location = Location.create!(:lat => 40.0, :lng => -70.0)
post.save
post.index!
Post.search { near([40.0, -70.0], :distance => 1) }.results.should == [post]
end

end

describe 'search_ids()' do
Expand Down
6 changes: 6 additions & 0 deletions sunspot_rails/spec/schema.rb
Expand Up @@ -2,11 +2,17 @@
create_table :posts, :force => true do |t|
t.string :title
t.string :type
t.integer :location_id
t.text :body
t.references :blog
t.timestamps
end

create_table :locations, :force => true do |t|
t.float :lat
t.float :lng
end

create_table :blogs, :force => true do |t|
t.string :name
t.string :subdomain
Expand Down

0 comments on commit 8c813c2

Please sign in to comment.