Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/delivery' into paypal
Browse files Browse the repository at this point in the history
Conflicts:
	app/controllers/users_controller.rb
	app/models/user.rb
	config/routes.rb
	db/schema.rb
  • Loading branch information
adriansutanahadi committed Aug 15, 2015
2 parents 0a84644 + 08a9143 commit d07d087
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 20 deletions.
41 changes: 36 additions & 5 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
class OrdersController < ApplicationController
before_filter :authenticate_user!
# before_action :set_order, only: [:show]
before_action :set_order, only: [:show, :deliver]

def index
@orders = Order.unassigned.all
end

def search
current_loc = Location.create(purchase_location_params)
destination = Location.create( longitude: delivery_location_params[:longitude_to],
latitude: delivery_location_params[:latitude_to],
address: delivery_location_params[:address_to])
delivery_trip = Trip.new(start_location_id: current_loc.id, end_location_id: destination.id)

@orders = Order.unassigned.all.sort do |x, y|
x_trip_distance = x.trip.distance(delivery_trip)
y_trip_distance = y.trip.distance(delivery_trip)
p "\n\n\n\n\n"
p x_trip_distance, x.inspect
p y_trip_distance, y.inspect
p "\n\n\n\n\n"
y_trip_distance <=> x_trip_distance
end

render :index
end

def new
end
Expand All @@ -17,7 +41,14 @@ def create
end

def show
@order = Order.find(params[:id])
end

# GET /orders/:id/deliver
def deliver
@order.deliverer = current_user
@order.status = 'assigned'
@order.save
redirect_to user_deliveries_path, notice: 'Order was successfully assigned.'
end

def pay order
Expand Down Expand Up @@ -50,9 +81,9 @@ def hook

private
# # Use callbacks to share common setup or constraints between actions.
# def set_order
# @order = Order.find(current_customer.id)
# end
def set_order
@order = Order.find(params[:id])
end

def order_params
params.require(:order).permit(:tips)
Expand Down
20 changes: 16 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
class UsersController < ApplicationController
before_filter :authenticate_user!
before_action :set_user, only: [:show]

def index
redirect_to new_user_session_path if !user_signed_in?
@user = current_user
end

def show
@user = User.find(params[:id])
unless @user == current_user
redirect_to :back, :alert => "Access denied."
end

end

def deliveries
@user = current_user
@orders = @user.deliveries
end

def account
Expand All @@ -20,4 +23,13 @@ def account
end
end

private
# # Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
unless @user == current_user
redirect_to :index, :alert => "Access denied."
end
end
>>>>>>> origin/delivery
end
16 changes: 16 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
class Location < ActiveRecord::Base
def self.distance loc1, loc2
rad_per_deg = Math::PI/180 # PI / 180
rkm = 6371 # Earth radius in kilometers
rm = rkm * 1000 # Radius in meters

dlat_rad = (loc2.latitude-loc1.latitude) * rad_per_deg # Delta, converted to rad
dlon_rad = (loc2.longitude-loc1.longitude) * rad_per_deg

lat1_rad, lon1_rad = [loc1.latitude, loc1.longitude].map {|i| i * rad_per_deg }
lat2_rad, lon2_rad = [loc2.latitude, loc2.longitude] .map {|i| i * rad_per_deg }

a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))

rm * c # Delta in meters
end
end
3 changes: 3 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Order < ActiveRecord::Base
has_many :items
belongs_to :user
belongs_to :deliverer, class_name: 'User', foreign_key: 'deliverer_id'
has_one :trip

scope :unassigned, -> { where(status: 'unassigned')}
end
10 changes: 10 additions & 0 deletions app/models/trip.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
class Trip < ActiveRecord::Base
belongs_to :order
belongs_to :user

def distance(trip)
start_loc1 = Location.find(start_location_id)
end_loc1 = Location.find(end_location_id)

start_loc2 = Location.find(trip.start_location_id)
end_loc2 = Location.find(trip.end_location_id)

Location.distance(start_loc1, start_loc2) + Location.distance(end_loc1, end_loc2)
end
end
7 changes: 5 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class User < ActiveRecord::Base
has_one :order
has_one :account
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


has_one :order
has_one :account
has_many :deliveries, class_name: 'Order', foreign_key: 'deliverer_id'
end
3 changes: 3 additions & 0 deletions app/views/layouts/_navigation_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li><%= link_to 'Delivery', orders_path %></li>
<li><%= link_to 'Order', new_order_path %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><%= current_user.name %><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to 'My Deliveries', user_deliveries_path %></li>
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
</ul>
Expand Down
83 changes: 83 additions & 0 deletions app/views/orders/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
h1 Available Orders
br
= form_tag search_orders_path
div.form-group
= label :location, :address, "Travelling From"
= text_field :location, :address, class:"form-control purchase-addr", required:true
div.form-group.hidden
= label :location, :longitude, class: "col-sm-1"
div.col-sm-3
= text_field :location, :longitude, class: "form-control purchase-lon", required:true
= label :location, :latitude, class: "col-sm-1"
div.col-sm-3
= text_field :location, :latitude, class: "form-control purchase-lat", required:true

div.form-group
= label :location, :address_to, "Destination"
= text_field :location, :address_to, class:"form-control deliver-addr", required:true
div.form-group.hidden
= label :location, :longitude_to, class: "col-sm-1"
div.col-sm-3
= text_field :location, :longitude_to, class: "form-control deliver-lon", required:true
= label :location, :latitude_to, class: "col-sm-1"
div.col-sm-3
= text_field :location, :latitude_to, class: "form-control deliver-lat", required:true

= submit_tag 'Find My Customised Orders', class: "btn btn-default"

div.hidden id="purchase" style="width: 500px; height: 400px;"
div.hidden id="deliver" style="width: 500px; height: 400px;"

br
br

- @orders.each do |order|
div.panel.panel-default
div.panel-heading
h3 = order.user.name
div.panel-body
- item = order.items.first
p
| wants to buy&nbsp;
= item.name + ' '
| with quantity of&nbsp;
= item.quantity
- purchase_loc = Location.find(order.trip.start_location_id)
p
| Purchase From:&nbsp;
= purchase_loc.address
- delivery_loc = Location.find(order.trip.end_location_id)
p
| Delivery To:&nbsp;
= delivery_loc.address
p
| Tips: $
= order.tips.to_s + ' '
| Estimated Price: $
= item.estimated_price
div.panel-footer
= link_to "I'll Deliver", deliver_order_path(order), class: "btn btn-default"


javascript:
$('#purchase').locationpicker({
location: {latitude: -37.8239402221389, longitude: 144.9911028355707},
radius: 3,
inputBinding: {
latitudeInput: $('.purchase-lat'),
longitudeInput: $('.purchase-lon'),
locationNameInput: $('.purchase-addr')
},
enableAutocomplete: true,
});
$('#deliver').locationpicker({
location: {latitude: -37.8239402221389, longitude: 144.9911028355707},
radius: 3,
inputBinding: {
latitudeInput: $('.deliver-lat'),
longitudeInput: $('.deliver-lon'),
locationNameInput: $('.deliver-addr')
},
enableAutocomplete: true,
});
5 changes: 3 additions & 2 deletions app/views/orders/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
= text_field :item, :quantity, class:"form-control", required:true
div.form-group
= label :item, :estimated_price, "Estimated Price"
= text_field :item, :estimated_price, class:"form-control", required:true
div.input-group
div.input-group-addon $
= text_field :item, :estimated_price, class:"form-control", required:true
div.form-group
= label :order, :tips, "Tips"
div.input-group
div.input-group-addon $
= text_field :order, :tips, class:"form-control", required:true
div.input-group-addon .00
div.form-group
= label :location, :address, "Purchase From"
= text_field :location, :address, class:"form-control purchase-addr", required:true
Expand Down
27 changes: 27 additions & 0 deletions app/views/users/deliveries.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
h1 My Assigned Deliveries
- @orders.each do |order|
div.panel.panel-default
div.panel-heading
h3 = order.user.name
div.panel-body
- item = order.items.first
p
| wants to buy&nbsp;
= item.name + ' '
| with quantity of&nbsp;
= item.quantity
- purchase_loc = Location.find(order.trip.start_location_id)
p
| Purchase From:&nbsp;
= purchase_loc.address
- delivery_loc = Location.find(order.trip.end_location_id)
p
| Delivery To:&nbsp;
= delivery_loc.address
p
| Tips: $
= order.tips.to_s + ' '
| Estimated Price: $
= item.estimated_price
div.panel-footer
= link_to "Cancel Delivery", '#', class: "btn btn-default"
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="container">
<div class="row">
<h3>I want to ...</h3>
<button type="button" class="btn btn-default btn-lg"><%= link_to "Order", new_order_path %></button>
<%= link_to "Order", new_order_path, class: 'btn btn-default btn-lg' %>
<a><button type="button" class="btn btn-default btn-lg">Deliver</button></a>
</div>
</div>
15 changes: 12 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
Rails.application.routes.draw do
root to: 'users#index'
get 'users/account' => 'users#account'
devise_for :users
resources :users
get 'users/account' => 'users#account'
get 'users/index', to: 'users#index'
get 'users/show', to: 'users#show'
get 'users/deliveries', to: 'users#deliveries', as: 'user_deliveries'

resources :orders
resources :orders do
member do
get 'deliver'
end
collection do
post 'search'
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20150815193437_add_deliverer_id_to_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDelivererIdToOrder < ActiveRecord::Migration
def change
add_column :orders, :deliverer_id, :integer
end
end
11 changes: 8 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#
# It's strongly recommended that you check this file into your version control system.

<<<<<<< HEAD
ActiveRecord::Schema.define(version: 20150815205250) do
=======
ActiveRecord::Schema.define(version: 20150815193437) do
>>>>>>> origin/delivery

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -46,11 +50,12 @@
end

create_table "orders", force: :cascade do |t|
t.string "status", default: "unassigned"
t.string "status", default: "unassigned"
t.integer "tips"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "deliverer_id"
end

add_index "orders", ["user_id"], name: "index_orders_on_user_id", using: :btree
Expand Down

0 comments on commit d07d087

Please sign in to comment.