Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change bounding box method to be geocoder compatible #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ Saves you the hassle of writing your own location queries and uses pre-built and

The current version only supports Rails **4.2+**. Request other versions by opening an issue.

### Release Notes for 0.2.0

This update changes the `within_bounding_box` method to `within_geo_query_bounding_box` in order to be compatible with the [Geocoder gem](https://github.com/alexreisner/geocoder). If you are updating to version `0.2.0` please remember to change method calls to new syntax

##Install
*These instructions already assume you have postgis enabled on your database, please follow the instructions here to enabled it https://github.com/rgeo/activerecord-postgis-adapter#install*

**NOTE:** These instructions already assume you have postgis enabled on your database.

If you do not have postgis enabled please follow the instructions [here]( https://github.com/rgeo/activerecord-postgis-adapter#install*) to enabled it.*

**Include gem**
```ruby
gem 'geo_query'
```

**Generate the migration**

*Will add a geography field to your model (Skip if already exists)*
```ruby
# Where user is your model name
rails g geo_query:resource User
```

**Attach the helper to your model**
```ruby
class User < ActiveRecord::Base
Expand All @@ -32,17 +41,19 @@ The helper gives your model the following methods
**Instance Methods**
```ruby
# Returns nearest objects
object.near(radius)
object.near(radius) #in metres
```

**Class Methods**
```ruby
# Returns all objects within the specified radius ordered by nearest
Class.near_lat_lon(lat, lon, radius) #radius defaults to 500metres
Class.near_lat_lon(lat, lon, radius) #in metres, defaults to 500
```
```ruby
# Returns all objects within a rectangle
Class.within_bounding_box(min_lat, min_lon, max_lat, max_lon)
Class.within_geo_query_bounding_box(min_lat, min_lon, max_lat, max_lon)
```

**Attribute setters**
```ruby
User.create(lat: -38, lon: 144)
Expand Down
4 changes: 2 additions & 2 deletions lib/geo_query/geo_queryable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def self.near_lat_lon(lat, lon, radius=500) #radius in metres
end

## Returns all objects within a bounding box
def self.within_bounding_box(min_lat, min_lon, max_lat, max_lon)
def self.within_geo_query_bounding_box(min_lat, min_lon, max_lat, max_lon)
select("#{self.table_name}.*").
where("#{self.table_name}.#{self.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
min_lon, min_lat, max_lon, max_lat)
Expand All @@ -78,4 +78,4 @@ def geo_queryable(options = {})
end
end
end
end
end
6 changes: 3 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@
end
end

describe "self.within_bounding_box" do
describe "self.within_geo_query_bounding_box" do

it "returns user within bounding box" do
user = create(:user, lat: 37.8117802, lon: 144.9651743)
expect(User.within_bounding_box(37.8117802, 144.9651743, 37.8117802, 144.9651743)).to include user
expect(User.within_geo_query_bounding_box(37.8117802, 144.9651743, 37.8117802, 144.9651743)).to include user
end

it "doesn't return user outside bounding box" do
user = create(:user, lat: -37.8142383, lon: 144.9656678)
expect(User.within_bounding_box(37.8117802, 144.9651743, 37.8117802, 144.9651743)).to_not include user
expect(User.within_geo_query_bounding_box(37.8117802, 144.9651743, 37.8117802, 144.9651743)).to_not include user
end
end

Expand Down