From f40f63658c3403e438cb2f9b3dde14d71a054a24 Mon Sep 17 00:00:00 2001 From: Lauren Oliveri Date: Tue, 1 Aug 2017 20:03:35 -0600 Subject: [PATCH] add new rack functionality --- app/assets/javascripts/{new.js => new.js.erb} | 25 ++++++++++++++-- app/controllers/api/v1/bracks_controller.rb | 2 +- app/controllers/application_controller.rb | 5 ++-- app/controllers/bracks_controller.rb | 29 ++++++++++++++++++- app/models/brack.rb | 7 ++--- app/models/user.rb | 6 ++++ app/views/bracks/edit.html.erb | 23 +++++++++++++++ app/views/bracks/new.html.erb | 2 +- app/views/layouts/_navbar.html.erb | 2 +- config/routes.rb | 2 +- spec/requests/api/v1/bracks/create_spec.rb | 18 ++++++++++-- 11 files changed, 104 insertions(+), 17 deletions(-) rename app/assets/javascripts/{new.js => new.js.erb} (82%) create mode 100644 app/views/bracks/edit.html.erb diff --git a/app/assets/javascripts/new.js b/app/assets/javascripts/new.js.erb similarity index 82% rename from app/assets/javascripts/new.js rename to app/assets/javascripts/new.js.erb index ee7fe62..e38cc86 100644 --- a/app/assets/javascripts/new.js +++ b/app/assets/javascripts/new.js.erb @@ -76,11 +76,32 @@ function moveToStepTwo() { }, 3000); $(".new-next").on('click', function(){ console.log("Yay!") - debugger + moveToStepThree() }) } function moveToStepThree() { + var user_id = $(".map-box").data("id") + var lat = currentCenter[0].position.lat() + var lng = currentCenter[0].position.lng() + var token = $(".map-box").prop("id") + // make ajax post request to new bracks - + var brackData = { + brack: { + user_id: user_id, + lat: lat, + long: lng, + token: token + } + } + + $.ajax({ + url: "/api/v1/bracks", + method: "POST", + data: brackData + }).done(function(data){ + debugger + window.location.href = `/bracks/${data.id}/edit` + }) } diff --git a/app/controllers/api/v1/bracks_controller.rb b/app/controllers/api/v1/bracks_controller.rb index ef547c4..35e7329 100644 --- a/app/controllers/api/v1/bracks_controller.rb +++ b/app/controllers/api/v1/bracks_controller.rb @@ -18,7 +18,7 @@ def create private def brack_params - params.require(:brack).permit(:user_id, :lat, :long) + params.require(:brack).permit(:user_id, :lat, :long, :token) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c20d714..8454389 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,13 +5,12 @@ class ApplicationController < ActionController::Base def authenticate_user unless current_user - flash[:alert] = "Please login to continue!" - redirect_to login_path + redirect_to login_path, :alert => 'Please log in to continue!' end end def current_user @user ||= User.find(session[:user_id]) if session[:user_id] end - + end diff --git a/app/controllers/bracks_controller.rb b/app/controllers/bracks_controller.rb index 15f3ce6..1a216f5 100644 --- a/app/controllers/bracks_controller.rb +++ b/app/controllers/bracks_controller.rb @@ -1,5 +1,6 @@ class BracksController < ApplicationController - before_action :authenticate_user, only: [:new] + before_action :authenticate_user, only: [:new, :edit] + before_action :set_brack, only: [:edit, :update] def index end @@ -7,4 +8,30 @@ def index def new end + def edit + unless request.referer && (URI(request.referer).path == "/bracks/new") + render file: "/public/404" + end + end + + def update + if @brack.update_attributes(brack_params) + flash[:notice] = "New Bike Rack Added!" + redirect_to bracks_path + else + flash[:error] = "There was an issue saving your new bike rack." + redirect_to bracks_path + end + end + + private + + def set_brack + @brack = Brack.find(params[:id]) + end + + def brack_params + params.require(:brack).permit(:cross_streets, :owner) + end + end diff --git a/app/models/brack.rb b/app/models/brack.rb index 9609d8d..2b043ae 100644 --- a/app/models/brack.rb +++ b/app/models/brack.rb @@ -11,15 +11,12 @@ def self.sort_by_distance(latlng) end def self.add_new(params) + return new() unless params[:token] == ENV['POST_TO_BRACKS_KEY'] # set lat and long lat = params[:lat] long = params[:long] # set user - user = User.find(params[:user_id]) - owner = user.organization - unless owner - owner = "#{user.first_name} #{user.last_name[0]}." - end + owner = User.find(params[:user_id]).generate_username # get cross_streets lat_long = {lat: lat, long: long} cross_streets = CrossStreetService.find_by_coordinates(lat_long) diff --git a/app/models/user.rb b/app/models/user.rb index b45a044..a8902f1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,7 @@ class User < ApplicationRecord has_secure_password + validates :first_name, :last_name, presence: true validates :email, presence: true, uniqueness: true def check_user_errors(params) @@ -12,4 +13,9 @@ def check_user_errors(params) messages << "Password confirmation does not match." if params[:password] != params[:pass_confirm] return messages.join("\n") end + + def generate_username + return "#{first_name} #{last_name[0]}." unless organization + organization + end end diff --git a/app/views/bracks/edit.html.erb b/app/views/bracks/edit.html.erb new file mode 100644 index 0000000..5bef805 --- /dev/null +++ b/app/views/bracks/edit.html.erb @@ -0,0 +1,23 @@ +

Add a New Rack

+ +
+

Edit and Confirm Bike Rack Information

+ <%= form_for @brack do |f| %> +
+ <%= f.label :cross_streets %> +

We tried autogenerating them for you, but we're not perfect:

+ <%= f.text_field :cross_streets, class: 'form-control' %> +
+ +
+ <%= f.label :owner %> +

Either your name or your organization's name:

+ <%= f.text_field :owner, class: 'form-control' %> +
+ +
+ <%= f.submit "Save New Rack", class:"btn btn-primary" %> +
+ + <% end %> +
diff --git a/app/views/bracks/new.html.erb b/app/views/bracks/new.html.erb index 7c82764..ced8764 100644 --- a/app/views/bracks/new.html.erb +++ b/app/views/bracks/new.html.erb @@ -1,6 +1,6 @@

Add a New Rack

-
+
Input nearest address to new bike rack:

diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index 195c2b4..f9715eb 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -5,7 +5,7 @@ - Brackr + <%= link_to "Brackr", root_path, method: :get, class:"navbar-brand" %>