Skip to content

Commit

Permalink
add new rack functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lao9 committed Aug 2, 2017
1 parent 74ba556 commit f40f636
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`
})
}
2 changes: 1 addition & 1 deletion app/controllers/api/v1/bracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 28 additions & 1 deletion app/controllers/bracks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
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

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
7 changes: 2 additions & 5 deletions app/models/brack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
23 changes: 23 additions & 0 deletions app/views/bracks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Add a New Rack</h1>

<div class="well">
<h4>Edit and Confirm Bike Rack Information</h4>
<%= form_for @brack do |f| %>
<div class="form-group">
<%= f.label :cross_streets %>
<p>We tried autogenerating them for you, but we're not perfect:</p>
<%= f.text_field :cross_streets, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :owner %>
<p>Either your name or your organization's name:</p>
<%= f.text_field :owner, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.submit "Save New Rack", class:"btn btn-primary" %>
</div>

<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/bracks/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Add a New Rack</h1>

<div class="map-box">
<div class="map-box" data-id="<%= current_user.id %>" id="<%= ENV['POST_TO_BRACKS_KEY'] %>">
<h5>Input nearest address to new bike rack:</h5>
<input id="pac-input" class="controls" type="text" placeholder="Search">
<hr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Brackr</a>
<%= link_to "Brackr", root_path, method: :get, class:"navbar-brand" %>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<% unless current_user %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'

resources :bracks, only: [:index, :new]
resources :bracks, only: [:index, :new, :edit, :update]
resources :users, only: [:new, :create]

namespace :api do
Expand Down
18 changes: 16 additions & 2 deletions spec/requests/api/v1/bracks/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
it "adds a new brack location owned by user with an organization" do
expect(Brack.count).to eq(0)

post '/api/v1/bracks', params: {brack: {user_id: user1.id, lat: brack.lat, long: brack.long}}
creation_params = {
user_id: user1.id,
lat: brack.lat,
long: brack.long,
token: ENV['POST_TO_BRACKS_KEY']
}

post '/api/v1/bracks', params: {brack: creation_params}

expect(response).to be_success
expect(Brack.count).to eq(1)
Expand All @@ -22,7 +29,14 @@
it "adds a new brack location owned by user without an organization" do
expect(Brack.count).to eq(0)

post '/api/v1/bracks', params: {brack: {user_id: user2.id, lat: brack.lat, long: brack.long}}
creation_params = {
user_id: user2.id,
lat: brack.lat,
long: brack.long,
token: ENV['POST_TO_BRACKS_KEY']
}

post '/api/v1/bracks', params: {brack: creation_params}

expect(response).to be_success
expect(Brack.count).to eq(1)
Expand Down

0 comments on commit f40f636

Please sign in to comment.