Skip to content

Commit

Permalink
Merge pull request #190 from RSid/listofpopularcities-79-rsid
Browse files Browse the repository at this point in the history
Listofpopularcities 79 rsid
  • Loading branch information
tkwidmer committed Jan 17, 2015
2 parents 4bb050d + 3e771ec commit e0c45a3
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ geocoder.rb
/.bundle
/vendor/bundle
.env
.powrc

## Ignore .DS_Store files in case people haven't setup a global gitignore
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*= require nav
*= require search
*= require _mobile
*= require top_cities
*/

@import 'variables';
Expand Down
49 changes: 49 additions & 0 deletions app/assets/stylesheets/top_cities.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#welcome-container{
margin-top: 25px;
width: 100%;
display: inline-block;
}

#top-cities-content {
border: 5px solid #41326b;
width: 30%;
display: inline-block;
float: left;
margin-left: 10px;
overflow: scroll;
}

#top-cities-content > p{
font-size: 18px;
background-color: #8377AF;
border-bottom: 2px solid #41326b;
color: white;
padding: 3px;
}

ul {
margin-top: 5px;
}

ul li{
text-align: left;
}

ul li > a{
text-decoration: none;
color: black;
}

ul li > a:hover{
text-decoration: none;
color: #8377AF;
}

#preamble {
width: 60%;
display: inline-block;
float: left;
padding-left: 3em;
text-align: left;
padding-top: 2em;
}
4 changes: 4 additions & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class WelcomeController < ApplicationController
layout 'splash', only: [:index]

def index
@cities = Restroom.top_cities
end
end
16 changes: 16 additions & 0 deletions app/models/restroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ def rating_percentage
upvote.to_f / (upvote + downvote).to_f * 100
end

def self.top_cities
Rails.cache.fetch("topcities", expires_in: 1.month) do
sql = "SELECT LOWER(city), state, COUNT(DISTINCT id) AS count FROM " +
"restrooms GROUP BY LOWER(city), state ORDER BY count DESC LIMIT 5"

cities = ActiveRecord::Base.connection.execute(sql).values

cities.each do |city|
city.pop
city[0].capitalize!
end

return cities
end
end

# PostgreSQL Full-Text Search for the API.
def self.text_search(query)
if query.present?
Expand Down
7 changes: 7 additions & 0 deletions app/views/welcome/_topcities.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%div{:id => "top-cities-content"}
%p
Popular Cities
%ul
- @cities.each do |city|
%li
= link_to city.join(", "), restrooms_path(search: city)
11 changes: 8 additions & 3 deletions app/views/welcome/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
= image_tag('logo-dark.svg')
= render partial: "layouts/search", locals: {splash: true}
.centerText
%p
= t('landing.development', contact_link: (link_to 'message', contact_path), github_link: (link_to 'Github', "http://www.github.com/RefugeRestrooms/refugerestrooms") ).html_safe
%br/
%div{:id => "welcome-container"}
%p
= render "topcities"
%p{:id => "preamble"}
= t('landing.development',
contact_link: (link_to 'message', contact_path),
github_link: (link_to 'Github',
"http://www.github.com/RefugeRestrooms/refugerestrooms") ).html_safe
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
config.cache_store = :null_store

# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
Expand Down
65 changes: 65 additions & 0 deletions spec/models/restroom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,69 @@
it { expect(Restroom.new.accessible?).to be false }
it { expect(Restroom.new(accessible: true).accessible?).to be true }
end

describe '#topcities' do
it 'should return the top five cities with the most restroom data' do
city_with_more_data = "City1"
2.times do
FactoryGirl.create(:restroom, city: city_with_more_data, state: "IL")
FactoryGirl.create(:restroom, city: "City2")
FactoryGirl.create(:restroom, city: "City3")
FactoryGirl.create(:restroom, city: "City4")
FactoryGirl.create(:restroom, city: "City5")
end

bathroom_in_city_with_less_data = FactoryGirl.create(:restroom,
city: "City6", state: "MA")

cities = Restroom.top_cities
expect(cities.count).to be 5
expect(cities).not_to include([bathroom_in_city_with_less_data.city,
"MA"])
expect(cities).to include([city_with_more_data, "IL"])
end

it 'should discriminate between same name cities in different states' do
city_exists_in_multiple_states = "City1"
states_city_with_less_data = "MA"
states_city_with_more_data = "IL"

2.times do
FactoryGirl.create(:restroom, city: city_exists_in_multiple_states,
state: states_city_with_more_data)
FactoryGirl.create(:restroom, city: "City2")
FactoryGirl.create(:restroom, city: "City3")
FactoryGirl.create(:restroom, city: "City4")
FactoryGirl.create(:restroom, city: "City5")
end

FactoryGirl.create(:restroom,
city: city_exists_in_multiple_states, state: states_city_with_less_data)

cities = Restroom.top_cities

expect(cities).not_to include([city_exists_in_multiple_states,
states_city_with_less_data])
expect(cities).to include([city_exists_in_multiple_states,
states_city_with_more_data])
end

it 'should retrieve top cities case insensitively' do
correctly_capitalized_city_name= "City1"
miscapitalized_city_name="CITY1"

2.times do
FactoryGirl.create(:restroom, city: correctly_capitalized_city_name,
state: "IL")
FactoryGirl.create(:restroom, city: miscapitalized_city_name,
state: "IL")
FactoryGirl.create(:restroom, city: "City3")
end

cities = Restroom.top_cities

expect(cities).not_to include([miscapitalized_city_name, "IL"])
expect(cities).to include([correctly_capitalized_city_name, "IL"])
end
end
end

0 comments on commit e0c45a3

Please sign in to comment.