Skip to content

Commit

Permalink
Merge branch 'enhancements' into website_fave
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwainstock committed Dec 19, 2018
2 parents 251e99f + a1cc840 commit c267a1d
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 12 deletions.
Binary file added app/assets/images/heart-empty.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/heart-filled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions app/assets/javascripts/application.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ function switchSection(newSection) {
$("#" + newSection + "_section_link").toggleClass("active_section_link");
}

function initSearch(operatorIDs, locationIDs, locationTypeIDs, zoneIDs, machineIDs, ipdbIDs, opdbIDs, cityName, machineGroupId, showLocationDistance, lat, lon, byAtLeastNMachinesCity, byAtLeastNMachinesType, byAtLeastNMachinesZone, region) {
if (operatorIDs || locationIDs || locationTypeIDs || zoneIDs || machineIDs || ipdbIDs || opdbIDs || cityName || machineGroupId || byAtLeastNMachinesCity || byAtLeastNMachinesType || byAtLeastNMachinesZone || region) {
function initSearch(userFaved, operatorIDs, locationIDs, locationTypeIDs, zoneIDs, machineIDs, ipdbIDs, opdbIDs, cityName, machineGroupId, showLocationDistance, lat, lon, byAtLeastNMachinesCity, byAtLeastNMachinesType, byAtLeastNMachinesZone, region) {
if (userFaved || operatorIDs || locationIDs || locationTypeIDs || zoneIDs || machineIDs || ipdbIDs || opdbIDs || cityName || machineGroupId || byAtLeastNMachinesCity || byAtLeastNMachinesType || byAtLeastNMachinesZone || region) {

if (region && !(operatorIDs || locationIDs || locationTypeIDs || zoneIDs || machineIDs || ipdbIDs || opdbIDs || cityName || machineGroupId || byAtLeastNMachinesCity || byAtLeastNMachinesType || byAtLeastNMachinesZone)) {
if (region && !(userFaved || operatorIDs || locationIDs || locationTypeIDs || zoneIDs || machineIDs || ipdbIDs || opdbIDs || cityName || machineGroupId || byAtLeastNMachinesCity || byAtLeastNMachinesType || byAtLeastNMachinesZone)) {
return;
}

var url = '/locations?by_location_id=' + locationIDs + ';by_operator_id=' + operatorIDs + ';by_type_id=' + locationTypeIDs + ';by_zone_id=' + zoneIDs + ';by_machine_id=' + machineIDs + ';by_city_id=' + cityName + ';by_machine_group_id=' + machineGroupId + ';by_ipdb_id=' + ipdbIDs + ';by_opdb_id=' + opdbIDs + ';show_location_distance=' + showLocationDistance + ';lat=' + lat + ';lon=' + lon + ';by_at_least_n_machines_city=' + byAtLeastNMachinesCity + ';by_at_least_n_machines_type=' + byAtLeastNMachinesType + ';by_at_least_n_machines_zone=' + byAtLeastNMachinesZone + ';region=' + region;
var url = '/locations?user_faved=' + userFaved + ';by_location_id=' + locationIDs + ';by_operator_id=' + operatorIDs + ';by_type_id=' + locationTypeIDs + ';by_zone_id=' + zoneIDs + ';by_machine_id=' + machineIDs + ';by_city_id=' + cityName + ';by_machine_group_id=' + machineGroupId + ';by_ipdb_id=' + ipdbIDs + ';by_opdb_id=' + opdbIDs + ';show_location_distance=' + showLocationDistance + ';lat=' + lat + ';lon=' + lon + ';by_at_least_n_machines_city=' + byAtLeastNMachinesCity + ';by_at_least_n_machines_type=' + byAtLeastNMachinesType + ';by_at_least_n_machines_zone=' + byAtLeastNMachinesZone + ';region=' + region;

$('#locations').html(loadingHTML());
$.get(url, function(data) {
Expand Down
11 changes: 10 additions & 1 deletion app/assets/stylesheets/application.css.scss.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,16 @@ a.active_section_link:hover {
padding: 10px 5px;
@include radius(5px 5px 0 0);
}
span.fave_location {
float: right;
width: 60px;
margin-top: -43px;
}
.fave_location img {
height: 20px;
width: 20px;
margin-top: 10px;
}
span.permalink {
float: right;
width: 30px;
Expand All @@ -1058,7 +1068,6 @@ span.permalink {
height: 20px;
width: 20px;
margin-top: 10px;

}
.address, .address_full {
font-size: 16px;
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class LocationsController < InheritedResources::Base
respond_to :xml, :json, :html, :js, :rss
has_scope :by_location_name, :by_location_id, :by_ipdb_id, :by_opdb_id, :by_machine_id, :by_machine_name, :by_city_id, :by_machine_group_id, :by_zone_id, :by_operator_id, :by_type_id, :by_at_least_n_machines_city, :by_at_least_n_machines_zone, :by_at_least_n_machines_type, :by_center_point_and_ne_boundary, :region, :by_is_stern_army
has_scope :by_location_name, :by_location_id, :by_ipdb_id, :by_opdb_id, :by_machine_id, :by_machine_name, :by_city_id, :by_machine_group_id, :by_zone_id, :by_operator_id, :by_type_id, :by_at_least_n_machines_city, :by_at_least_n_machines_zone, :by_at_least_n_machines_type, :by_center_point_and_ne_boundary, :region, :by_is_stern_army, :user_faved
before_action :authenticate_user!, only: %i[update_desc update_metadata confirm]

def create
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class PagesController < ApplicationController
respond_to :xml, :json, :html, :js, :rss
has_scope :by_location_name, :by_location_id, :by_machine_name, :by_machine_id
has_scope :by_location_name, :by_location_id, :by_machine_name, :by_machine_id, :user_faved

def params
request.parameters
Expand All @@ -11,7 +11,7 @@ def params
def regionless_location_data
@locations = []

if params[:address].blank? && params[:by_machine_id].blank? && params[:by_machine_name].blank? && params[:by_location_name].blank?
if params[:address].blank? && params[:by_machine_id].blank? && params[:by_machine_name].blank? && params[:by_location_name].blank? && params[:user_faved].blank?
@locations = []
else
params.delete(:by_machine_name) unless params[:by_machine_id].blank?
Expand All @@ -31,7 +31,11 @@ def regionless_location_data
render partial: 'locations/locations', layout: false
end

def regionless; end
def regionless
user = current_user.nil? ? nil : current_user

params[:user_faved] = user.id if user && !params[:user_faved].blank?
end

def region
@locations = Location.where('region_id = ?', @region.id).includes(:location_type)
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ def create
end
end

def fave_locations
@user = User.find(params[:id])
end

def toggle_fave_location
user = User.find(params[:id])
location = Location.find(params[:location_id])

if UserFaveLocation.where(user: user, location: location).any?
UserFaveLocation.where(user: user, location: location).destroy_all
else
UserFaveLocation.create(user: user, location: location)
end
end

def profile
search_param = params[:id]

Expand Down
8 changes: 8 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class Location < ApplicationRecord
scope :by_is_stern_army, (->(_non_blank_param) { where(is_stern_army: true) })
scope :regionless_only, (->(_non_blank_param) { where(region_id: nil) })
scope :zoneless, (-> { where(zone_id: nil) })
scope :user_faved, (lambda { |user_id|
fave_ids = UserFaveLocation.where(user_id: user_id).map(&:location_id)
where(id: fave_ids)
})

before_destroy do |record|
Event.where(location_id: record.id).destroy_all
Expand All @@ -93,6 +97,10 @@ def self.by_at_least_n_machines_sql(number_of_machines)
"locations.id in (select location_id from (select location_id, count(*) as count from location_machine_xrefs group by location_id) x where x.count >= #{number_of_machines})"
end

def user_fave?(user_id)
UserFaveLocation.where(user_id: user_id, location_id: id).any?
end

def num_machines
machines.length
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
= csrf_meta_tag
= render 'application/favicon'

- init_search = "initSearch('#{params['by_operator_id']}', '#{params['by_location_id']}', '#{params['by_type_id']}', '#{params['by_zone_id']}', '#{params['by_machine_id']}','#{params['by_ipdb_id']}','#{params['by_opdb_id']}', '#{params['by_city_id']}', '#{params['by_machine_group_id']}', '#{params['show_location_distance']}', '#{params['lat']}', '#{params['lon']}', '#{params['by_at_least_n_machines_city']}', '#{params['by_at_least_n_machines_type']}', '#{params['by_at_least_n_machines_zone']}', '#{params['region']}')"
- init_search = "initSearch('#{params['user_faved']}', '#{params['by_operator_id']}', '#{params['by_location_id']}', '#{params['by_type_id']}', '#{params['by_zone_id']}', '#{params['by_machine_id']}','#{params['by_ipdb_id']}','#{params['by_opdb_id']}', '#{params['by_city_id']}', '#{params['by_machine_group_id']}', '#{params['show_location_distance']}', '#{params['lat']}', '#{params['lon']}', '#{params['by_at_least_n_machines_city']}', '#{params['by_at_least_n_machines_type']}', '#{params['by_at_least_n_machines_zone']}', '#{params['region']}')"
%body{ :onload => defined?(@region) ? "setSearchSections(#{@region.available_search_sections}); setRegion('#{@region.name.downcase}'); switchSection('#{(@region.default_search_type && !@region.default_search_type.empty?) ? @region.default_search_type : 'location'}'); #{init_search}" : "#{init_search}" }
#container
#page{:class => "#{@region ? '' : (defined? @region) and current_page?(submitted_new_location_path) ? 'page_home submitted' : 'page_home'}"}
Expand Down
17 changes: 17 additions & 0 deletions app/views/locations/_render_location_detail.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
%ul
%li.name{:onclick => "locationLookupMapCenter(#{l.id});"}
=l.name
- if user_signed_in?
%span.fave_location{:id => "fave_location_#{l.id}"}
=image_tag( l.user_fave?(current_user.id) ? 'heart-filled.png' : 'heart-empty.png', :alt => 'fave this location', :id => "fave_location_img_#{l.id}")
%span.permalink
=link_to image_tag( 'link.png' ), "/#{l.region ? l.region.name.downcase : 'regionless'}/?by_location_id=#{l.id}", :id => l.id, :alt => 'link to this location'
.clear
Expand Down Expand Up @@ -47,4 +50,18 @@
$("#location_detail_location_#{l.id} .more_pictures").css("display","block");
$("#location_detail_location_#{l.id} .less_pictures").css("display","none");
});
$("#fave_location_#{l.id}").click(function() {
$.post('/users/#{current_user ? current_user.id : 'null'}/toggle_fave_location.json', {location_id: #{l.id}}, function(data) {
if ($("#fave_location_img_#{l.id}").attr('src') === '#{asset_path('heart-filled.png')}') {
$("#fave_location_img_#{l.id}").attr('src', '#{asset_path('heart-empty.png')}');
} else {
$("#fave_location_img_#{l.id}").attr('src', '#{asset_path('heart-filled.png')}');
}
});
});
$("#location_detail_location_#{l.id} .less_pictures").click(function() {
$("#location_detail_location_#{l.id} .location_thumbs").removeClass("toggle_picture_height_off",500);
$("#location_detail_location_#{l.id} .more_pictures").css("display","block");
$("#location_detail_location_#{l.id} .less_pictures").css("display","none");
});
});
6 changes: 6 additions & 0 deletions app/views/pages/_header.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
- else
.header_buttons_child.header_menu_01
=link_to 'Add Location', suggest_path, :class => "bold"
- if (current_user)
.header_buttons_child
=link_to 'Fave Locations', saved_path, :class => "bold"
- if (@region || current_page?(regionless_path))
.header_buttons_child{:class => "#{@region ? 'header_menu_03' : ''}"}
%span.bold{:onclick => "findClosestLocations('#{@region ? @region.name : 'regionless'}')"}
Expand Down Expand Up @@ -77,6 +80,9 @@
.header_buttons_child=link_to 'Add Location', regionless_location_suggest_path, :class => "bold"
- else
.header_buttons_child=link_to 'Add Location', suggest_path, :class => "bold"
- if (current_user)
.header_buttons_child
=link_to 'Fave Locations', saved_path, :class => "bold"
- if (@region || current_page?(regionless_path))
%span.header_buttons_child{:onclick => "findClosestLocations('#{@region ? @region.name : 'regionless'}')"}
= link_to 'Closest Locations', '#', {:id => 'find_closest_locations'}
Expand Down
4 changes: 4 additions & 0 deletions app/views/users/fave_locations.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#page_body
- @user.user_fave_locations.each do |fave_location|
= link_to(fave_location.location.name, (fave_location.location.region ? region_homepage_path(fave_location.location.region.name) : regionless_path) + "/?by_location_id=#{fave_location.location.id}")
%br/
8 changes: 8 additions & 0 deletions app/views/users/profile.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@
%span.score_meta_gen #{location_name}
%span.score_meta on
%span.score_meta_gen #{date_created}
- if !@user.user_fave_locations.empty?
%div.darkbl.stats
%div.stats_o
Fave Locations:
%span.stats_list
- @user.user_fave_locations.each do |fave_location|
%span.score_machine
= link_to(fave_location.location.name, (fave_location.location.region ? region_homepage_path(fave_location.location.region.name) : regionless_path) + "/?by_location_id=#{fave_location.location.id}")
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@
end
end

resources :users, only: [:profile] do
resources :users, only: [:profile, :fave_locations, :toggle_fave_location] do
member do
get :profile
get :profile, :fave_locations
post :toggle_fave_location
end
end

get 'inspire_profile' => 'pages#inspire_profile'
get 'pages/home'
get 'regionless' => 'pages#regionless'
get 'saved' => 'pages#regionless', user_faved: true
get 'regionless_location_data' => 'pages#regionless_location_data'
get 'suggest' => 'pages#suggest_new_location', as: 'regionless_location_suggest'
get 'submitted_new_location' => 'pages#submitted_new_location', as: 'regionless_submitted_new_location'
Expand Down
38 changes: 38 additions & 0 deletions spec/features/locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@
@region = FactoryBot.create(:region, name: 'portland', full_name: 'portland', lat: 1, lon: 2, motd: 'This is a MOTD', n_search_no: 4, should_email_machine_removal: 1)
end

describe 'add as fave', type: :feature, js: true do
it "doesn't give you the option unless you're logged in" do
location = FactoryBot.create(:location, region: @region, name: 'Cleo')

visit "/#{@region.name}/?by_location_id=" + location.id.to_s

expect(page).to_not have_selector("#fave_location_#{location.id}")
end

it 'toggles between faved and unfaved when clicked' do
user = FactoryBot.create(:user)
page.set_rack_session("warden.user.user.key": User.serialize_into_session(user))

location = FactoryBot.create(:location, region: @region, name: 'Cleo')

visit "/#{@region.name}/?by_location_id=" + location.id.to_s

expect(page).to have_selector("#fave_location_#{location.id}")

expect(UserFaveLocation.where(location: location, user: user).size).to eq(0)
expect(page.find("#fave_location_img_#{location.id}")['src']).to have_content('heart-empty')

find("#fave_location_img_#{location.id}").click

sleep 1

expect(UserFaveLocation.where(location: location, user: user).size).to eq(1)
expect(page.find("#fave_location_img_#{location.id}")['src']).to have_content('heart-filled')

find("#fave_location_img_#{location.id}").click

sleep 1

expect(UserFaveLocation.where(location: location, user: user).size).to eq(0)
expect(page.find("#fave_location_img_#{location.id}")['src']).to have_content('heart-empty')
end
end

describe 'confirm location', type: :feature, js: true do
before(:each) do
@user = FactoryBot.create(:user, username: 'ssw')
Expand Down
15 changes: 15 additions & 0 deletions spec/features/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@
expect(page.body).to_not have_content('No Way')
end

it 'respects user_faved filter' do
user = FactoryBot.create(:user)
page.set_rack_session("warden.user.user.key": User.serialize_into_session(user))

FactoryBot.create(:user_fave_location, user: user, location: FactoryBot.create(:location, name: 'Foo'))
FactoryBot.create(:user_fave_location, user: user, location: FactoryBot.create(:location, name: 'Bar'))
FactoryBot.create(:user_fave_location, location: FactoryBot.create(:location, name: 'Baz'))

visit '/saved'

expect(page.body).to have_content('Foo')
expect(page.body).to have_content('Bar')
expect(page.body).to_not have_content('Baz')
end

it 'lets you search by address -- displays 0 results instead of saying "Not Found"' do
FactoryBot.create(:location, region: nil, name: 'Troy', zip: '48098', lat: 42.5925, lon: 83.1756)

Expand Down
36 changes: 36 additions & 0 deletions spec/features/users_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
require 'spec_helper'

describe UsersController do
describe 'Fave Locations', type: :feature, js: true do
before(:each) do
@user = FactoryBot.create(:user, username: 'ssw', email: 'ssw@yeah.com', created_at: '02/02/2016')
page.set_rack_session("warden.user.user.key": User.serialize_into_session(@user))
end

it 'lists fave locations for users' do
FactoryBot.create(:user_fave_location, user: @user, location: FactoryBot.create(:location, name: 'Foo'))
FactoryBot.create(:user_fave_location, user: @user, location: FactoryBot.create(:location, name: 'Bar'))
FactoryBot.create(:user_fave_location, location: FactoryBot.create(:location, name: 'Baz'))

visit "/users/#{@user.id}/fave_locations"

expect(page).to have_link('Foo')
expect(page).to have_link('Bar')

expect(page).to_not have_link('Baz')
end
end

describe 'Profile', type: :feature, js: true do
before(:each) do
@user = FactoryBot.create(:user, username: 'ssw', email: 'ssw@yeah.com', created_at: '02/02/2016')
Expand Down Expand Up @@ -29,6 +49,20 @@
expect(page.title).to eq(title)
end

it 'lists saved locations' do
FactoryBot.create(:user_fave_location, user: @user, location: FactoryBot.create(:location, name: 'Foo'))
FactoryBot.create(:user_fave_location, user: @user, location: FactoryBot.create(:location, name: 'Bar'))
FactoryBot.create(:user_fave_location, location: FactoryBot.create(:location, name: 'Baz'))

visit "/users/#{@user.id}/profile"

expect(page).to have_content('Fave Locations:')
expect(page).to have_link('Foo')
expect(page).to have_link('Bar')

expect(page).to_not have_link('Baz')
end

it 'display metrics about the users account' do
FactoryBot.create(:user_submission, user: @user, location: FactoryBot.create(:location, id: 100), submission_type: UserSubmission::NEW_LMX_TYPE)
FactoryBot.create(:user_submission, user: @user, location: Location.find(100), submission_type: UserSubmission::NEW_CONDITION_TYPE)
Expand Down Expand Up @@ -57,6 +91,8 @@
expect(page).to have_content("3\nLocations Submitted")
expect(page).to have_content("5\nLocations Edited")
expect(page).to have_content("High Scores (Last 50):\nMachine One\n1\nat Location One on Jan-02-2016\nMachine Two\n3\nat Location Two on Jan-01-2016")

expect(page).to_not have_content('Fave Locations:')
end

it 'adds commas to high scores' do
Expand Down
29 changes: 29 additions & 0 deletions spec/models/location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@
@lmx2 = FactoryBot.create(:location_machine_xref, location: @l, machine: @m2, created_at: '2014-01-15 05:00:00')
end

describe '#user_fave?' do
it 'tells you if the location is a fave of the user passed in as a param' do
user = FactoryBot.create(:user)
location = FactoryBot.create(:location)
other_location = FactoryBot.create(:location)

FactoryBot.create(:user_fave_location, user: user, location: location)
FactoryBot.create(:user_fave_location, location: other_location)

expect(location.user_fave?(user.id)).to be_truthy
expect(other_location.user_fave?(user.id)).to_not be_truthy
end
end

describe 'respects user_faved scope' do
it 'should filter to locations that the user has faved' do
user = FactoryBot.create(:user)
location = FactoryBot.create(:location)
other_location = FactoryBot.create(:location)

FactoryBot.create(:user_fave_location, user: user, location: location)
FactoryBot.create(:user_fave_location, user: user, location: other_location)

FactoryBot.create(:user_fave_location, location: location)

expect(Location.user_faved(user.id)).to eq([location, other_location])
end
end

describe 'validates phone' do
it 'only allows valid formats' do
[
Expand Down

0 comments on commit c267a1d

Please sign in to comment.