Skip to content

Commit

Permalink
Started adding backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
JGefroh committed Aug 31, 2016
1 parent 166e3f8 commit 39bb22b
Show file tree
Hide file tree
Showing 33 changed files with 281 additions and 486 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ gem 'unicorn'
gem 'rails_12factor', group: :production
gem 'rack-cors', '0.4.0'


gem 'geocoder'

group :development do
gem 'spring'
gem 'byebug'
end
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ GEM
tzinfo (~> 1.1)
arel (6.0.3)
builder (3.2.2)
byebug (9.0.5)
concurrent-ruby (1.0.2)
erubis (2.7.0)
geocoder (1.2.11)
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
Expand Down Expand Up @@ -118,6 +120,8 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (~> 0.9.3)
byebug
geocoder
pg
rack-cors (= 0.4.0)
rails (= 4.2.5)
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/markets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MarketsController < ApplicationController
def index
@markets = Market.all
if params[:find_within]
search_distance_in_miles = 20
if params[:position_known] == 'true'
search_center = [params[:current_lat], params[:current_lng]]
@markets = @markets.near(search_center, search_distance_in_miles)
else
search_center = Geocoder::Calculations.geographic_center([[params[:sw_lat], params[:sw_lng]], [params[:ne_lat], params[:ne_lng]]])
@markets = @markets.within_bounding_box([params[:sw_lat], params[:sw_lng], params[:ne_lat], params[:ne_lng]])
end
end
render json: @markets
end

def create
Market.create()
end

private def location_params
params.require(:location).permit(:name, :address)
end
end
35 changes: 35 additions & 0 deletions app/models/concerns/locatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Locatable
extend ActiveSupport::Concern

included do
geocoded_by :address
after_validation :geocode, if: ->(object){ object.address_changed? }
validates :address, length: {maximum: 100}

def to_marker(display_html)
if geocoded?
return {
id: self.id,
lat: self.latitude + rand(-0.000050..0.000050),
lng: self.longitude + rand(-0.000050..0.000050),
infowindow: display_html
}
end
end

def latitude_with_fudge
if (geocoded?)
self.latitude + rand(-0.000050..0.000050)
end
end

def longitude_with_fudge
if (geocoded?)
self.longitude + rand(-0.000050..0.000050)
end
end
end

class_methods do
end
end
9 changes: 9 additions & 0 deletions app/models/concerns/schedulable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Schedulable
extend ActiveSupport::Concern

included do
end

class_methods do
end
end
4 changes: 4 additions & 0 deletions app/models/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Market < ActiveRecord::Base
include Locatable
include Schedulable
end
4 changes: 4 additions & 0 deletions app/serializers/market_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class MarketSerializer < ActiveModel::Serializer
attributes :id, :created_at, :updated_at
:address, :distance, :latitiude, :locale, :longitude, :name
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do

root 'application#index'

resources :markets
end
20 changes: 20 additions & 0 deletions db/migrate/20160831030315_create_markets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class CreateMarkets < ActiveRecord::Migration
def change
create_table :markets do |t|
# Markets
t.string :name

# Geocoding
t.float :latitude
t.float :longitude
t.string :address

# Scheduling
t.string :days_of_week


# Other
t.timestamps null: false
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160625075533) do
ActiveRecord::Schema.define(version: 20160831030315) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "markets", force: :cascade do |t|
t.string "name"
t.float "latitude"
t.float "longitude"
t.string "address"
t.string "days_of_week"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions frontend/app/modules/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
(function() {
'use strict';
var webServiceBase = '{!api_host!}';

angular
.module('ttfm', [
'ngSanitize',
'ui.router',
'ttfm.home',
'ttfm.locations',
'ttfm.markets',
'jgefroh.components'
]
)
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$urlRouterProvider.otherwise('/');
$urlRouterProvider.otherwise('/markets');
$locationProvider.html5Mode(true);
$stateProvider.state('ttfm', {
url: '',
Expand All @@ -24,7 +23,7 @@
});
}])
.constant('config', {webServiceBase: webServiceBase})
.controller('ApplicationController', ['$rootScope', '$scope', '$state',function($rootScope, $scope, $state) {
.controller('ApplicationController', ['$sce', '$rootScope', '$scope', '$state',function($sce, $rootScope, $scope, $state) {
var vm = this;
}]);
})();
2 changes: 1 addition & 1 deletion frontend/app/modules/components/base-service-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
};

service.getResponsePayload = function(response) {
return response.data;
return response.data[service.resourceNamePlural];
};

service.createPayload = function(resource) {
Expand Down
23 changes: 19 additions & 4 deletions frontend/app/modules/components/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
controllerAs: 'vm',
bindToController: true,
scope: {
markers: '='
markers: '=',
bounds: '='
},
link: function(scope, element, attributes) {
var currentPositionMarker = null;
var myLatLng = {lat: 21.3000, lng: -157.8167};
var infoWindow = new google.maps.InfoWindow({});
var allMarkers = [];
Expand All @@ -31,6 +33,11 @@
center: myLatLng
});

google.maps.event.addListener(map, "bounds_changed", function() {
scope.$applyAsync(function() {
scope.vm.bounds = map.getBounds();
});
});
scope.$on('location:show', function(event, payload) {
var marker = getMarkerWithId(payload.id);
if (marker && marker.latitude) {
Expand All @@ -40,6 +47,10 @@
}
else {
map.panTo(new google.maps.LatLng(payload.latitude, payload.longitude));
if (currentPositionMarker) {
currentPositionMarker.setMap(null);
}
currentPositionMarker = addMarker(map, -1, payload, 'You are here!', '/images/marker-current-location.png');
map.setZoom(16);
}
});
Expand Down Expand Up @@ -70,7 +81,10 @@
var content = '';
var label = marker.name;
if (!marker.isHidden) {
addMarker(map, marker.id, marker, label);
var marker = addMarker(map, marker.id, marker, label);
if (marker) {
allMarkers.push(marker);
}
}
});
}, true);
Expand All @@ -82,18 +96,19 @@
allMarkers = [];
}

function addMarker(map, id, position, content) {
function addMarker(map, id, position, content, icon) {
if (angular.isNumber(position.latitude) && angular.isNumber(position.longitude)) {
var marker = new google.maps.Marker({
id: id,
map: map,
position: {lat: position.latitude, lng: position.longitude},
icon: icon,
content: content
});
marker.addListener('click', function() {
openInfoWindow(map, marker);
});
allMarkers.push(marker);
return marker;
}
}

Expand Down
120 changes: 0 additions & 120 deletions frontend/app/modules/home/home-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,5 @@

function Controller($scope, LocationsService, locations) {
var vm = this;
function initialize() {
initializeDateFilters();
initializeLocations();
}

vm.toggleDateFilter = function(dateFilter) {
var index = vm.activeDateFilters.indexOf(dateFilter);
if (index === -1) {
vm.activeDateFilters.push(dateFilter);
}
else {
vm.activeDateFilters.splice(index, 1);
}
}

vm.detectLocation = function() {
vm.detectingLocation = true;
navigator.geolocation.getCurrentPosition(function(response) {
$scope.$applyAsync(function() {
vm.currentLocation = response;
vm.panToLocation(vm.currentLocation.coords);
vm.detectingLocation = false;
});
}, function(response) {
$scope.$applyAsync(function() {
vm.detectingLocation = false;
});
});
}

vm.isActiveDateFilter = function(dateFilter) {
return vm.activeDateFilters.indexOf(dateFilter) !== -1;
}

vm.panToLocation = function(location) {
$scope.$broadcast('location:show', location);
}

vm.dateFilter = function(location) {
if (!vm.activeDateFilters.length) {
location.isHidden = false;
return true;
}
var match = false;
angular.forEach(vm.activeDateFilters, function(activeDateFilter) {
if (location.schedule[activeDateFilter]) {
match = true;
}
});
if (match) {
location.isHidden = false;
}
else {
location.isHidden = true;
}
return match;
}


function initializeLocations() {
if (!locations) {
LocationsService.query().then(function(locations) {
vm.locations = locations;
LocationsService.cache('locations', locations);
});
}
else {
vm.locations = locations;
}
}

vm.setFilter = function(dateFilter) {
vm.activeDateFilters = [];
vm.activeDateFilters.push(dateFilter.value);
vm.isShowingOther = false;
}

function initializeDateFilters() {
vm.activeDateFilters = [];
vm.dateFilters = [
{
label: 'sun',
value: 'sunday'
},
{
label: 'mon',
value: 'monday'
},
{
label: 'tue',
value: 'tuesday'
},
{
label: 'wed',
value: 'wednesday'
},
{
label: 'thu',
value: 'thursday'
},
{
label: 'fri',
value: 'friday'
},
{
label: 'sat',
value: 'saturday'
}
];
var todayDay = new Date().getDay();
vm.today = vm.dateFilters[todayDay];
if (todayDay <= 6) {
vm.tomorrow = vm.dateFilters[todayDay + 1];
}
else {
vm.tomorrow = vm.dateFilters[0];
}
vm.setFilter(vm.today);
}
initialize();
}
})();
Loading

0 comments on commit 39bb22b

Please sign in to comment.