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

Add filter 'by_postal' #32

Merged
merged 8 commits into from
Dec 29, 2019
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Authorization
- Brewery update suggestions feature

## [0.4.2] - 2019-12-28

### Added

- Breweries filtering by postal code: 'by_postal'

## [0.4.1] - 2019-03-07

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/breweries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class BreweriesController < ApplicationController
has_scope :by_tag, only: :index
# FILTER: /breweries?by_tags=dog-friendly,patio
has_scope :by_tags, only: :index
# FILTER /breweries?by_postal=44107
has_scope :by_postal, only: :index

# GET /breweries
def index
Expand Down
2 changes: 2 additions & 0 deletions app/models/brewery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Brewery < ApplicationRecord
scope :by_type, ->(type) { where('lower(brewery_type) = ?', type.downcase) }
scope :by_tag, ->(tag) { tagged_with(tag.downcase) }
scope :by_tags, ->(tags) { tagged_with(tags.downcase.split(',')) }
scope :by_postal, ->(postal) { where('postal_code LIKE ?', "#{postal}%") }


def address
[street, city, state, country].join(', ')
Expand Down
24 changes: 24 additions & 0 deletions spec/requests/breweries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@
end
end

context "when postal param is passed" do
before do
create_list(:brewery, 5)
create_list(:brewery, 3, postal_code: "44107")
create_list(:brewery, 2, postal_code: "44107-5555")
create_list(:brewery, 1, postal_code: "WC2N 5DU")
end

it "returns a filtered list of breweries for US postal codes" do
get "/breweries", params: { by_postal: "44107" }
expect(json.size).to eq(5)
end

it "returns a filtered list of breweries for US postal code ZIP+4" do
get "/breweries", params: { by_postal: "44107-5555" }
expect(json.size).to eq(2)
end

it "returns a filtered list of breweries for international postal codes" do
get "/breweries", params: { by_postal: "WC2N" }
expect(json.size).to eq(1)
end
end

context "when sort param is passed" do
before do
create(
Expand Down