Skip to content

Commit

Permalink
Customised Search
Browse files Browse the repository at this point in the history
  • Loading branch information
hexinpeter committed Aug 15, 2015
1 parent d3e5749 commit 08a9143
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ 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 Down
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
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
58 changes: 57 additions & 1 deletion app/views/orders/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
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
Expand All @@ -24,4 +56,28 @@ h1 Available Orders
| Estimated Price: $
= item.estimated_price
div.panel-footer
= link_to "I'll Deliver", deliver_order_path(order), class: "btn btn-default"
= 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,
});
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
member do
get 'deliver'
end
collection do
post 'search'
end
end
end

0 comments on commit 08a9143

Please sign in to comment.