Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def acts_as_geolocated(options = {})
end

def within_box(radius, lat, lng)
radius = radius.try(:*, MILES_TO_METERS_FACTOR) if distance_unit === :miles
radius = radius.to_f * MILES_TO_METERS_FACTOR if distance_unit === :miles
earth_box = Arel::Nodes::NamedFunction.new(
"earth_box",
[Utils.ll_to_earth_coords(lat, lng), Utils.quote_value(radius)]
Expand All @@ -35,7 +35,7 @@ def within_box(radius, lat, lng)
end

def within_radius(radius, lat, lng)
radius = radius.try(:*, MILES_TO_METERS_FACTOR) if distance_unit === :miles
radius = radius.to_f * MILES_TO_METERS_FACTOR if distance_unit === :miles
earth_distance = Utils.earth_distance(through_table_klass, lat, lng)
within_box(radius, lat, lng)
.where(Arel::Nodes::InfixOperation.new("<=", earth_distance, Utils.quote_value(radius)))
Expand Down
12 changes: 11 additions & 1 deletion spec/act_as_geolocated_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
it { is_expected.to eq [@place] }
end

context "when radius is a string" do
let(:test_data) { { radius: '2400', lat: -27.5969039, lng: -48.5494544 } }

it { is_expected.to eq [@place] }
end

context "when query for place within the box, but outside the radius" do
let(:test_data) { { radius: 186, lat: -27.5969039, lng: -48.5494544 } }

Expand Down Expand Up @@ -211,7 +217,6 @@
let(:test_data) { { lat: nil, lng: nil, radius: nil } }
subject { Place.within_radius(test_data[:radius], test_data[:lat], test_data[:lng]) }
before(:all) do
# Place.distance_unit = :miles
Place.acts_as_geolocated distance_unit: :miles
@place = Place.create!(lat: -30.0277041, lng: -51.2287346)
end
Expand All @@ -235,6 +240,11 @@
it { is_expected.to eq [@place] }
end

context "when radius is a string" do
let(:test_data) { { radius: '2400', lat: -27.5969039, lng: -48.5494544 } }
it { is_expected.to eq [@place] }
end

context "when query for place outside the radius" do
let(:test_data) { { radius: 0.62, lat: -27.5969039, lng: -48.5494544 } }
it { is_expected.to eq [] }
Expand Down