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..e0ff9b8 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 } } @@ -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 @@ -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 [] }