From 63d4c0c3b0bd53f6d2f1b895f106112d0554696c Mon Sep 17 00:00:00 2001 From: Max Furman Date: Tue, 24 Apr 2018 11:58:48 -0400 Subject: [PATCH 1/2] Do not multiply strings --- .../acts_as_geolocated.rb | 4 ++-- spec/act_as_geolocated_spec.rb | 11 +++++++++++ spec/spec_helper.rb | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb b/lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb index ed7e0f5..cead55a 100644 --- a/lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb +++ b/lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb @@ -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)] @@ -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))) diff --git a/spec/act_as_geolocated_spec.rb b/spec/act_as_geolocated_spec.rb index b8bd002..2bda798 100644 --- a/spec/act_as_geolocated_spec.rb +++ b/spec/act_as_geolocated_spec.rb @@ -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 } } @@ -235,6 +241,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 [] } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74ac493..a04ee98 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,8 +12,8 @@ ActiveRecord::Base.establish_connection( adapter: "postgresql", host: "localhost", - username: "postgres", - password: "postgres", + # username: "postgres", + # password: "postgres", port: 5432, database: "ar_pg_earthdistance_test" ) From ed7db2ae04c4453db92530f267f6c4bf444ced72 Mon Sep 17 00:00:00 2001 From: Max Furman Date: Tue, 24 Apr 2018 12:01:44 -0400 Subject: [PATCH 2/2] no need to change spec_helper --- spec/act_as_geolocated_spec.rb | 1 - spec/spec_helper.rb | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/act_as_geolocated_spec.rb b/spec/act_as_geolocated_spec.rb index 2bda798..e0ff9b8 100644 --- a/spec/act_as_geolocated_spec.rb +++ b/spec/act_as_geolocated_spec.rb @@ -217,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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a04ee98..74ac493 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,8 +12,8 @@ ActiveRecord::Base.establish_connection( adapter: "postgresql", host: "localhost", - # username: "postgres", - # password: "postgres", + username: "postgres", + password: "postgres", port: 5432, database: "ar_pg_earthdistance_test" )