Permalink
Browse files

continuing route

  • Loading branch information...
1 parent 74cc0df commit 72d0fb3813a9a351c59a319773d2c6478a2540f9 @akwiatkowski committed Jun 10, 2012
@@ -1,3 +1,9 @@
class RouteElementsController < InheritedResources::Base
belongs_to :route
+
+ def continue
+ @route = Route.find(params[:route_id])
+ @last_route_element = @route.route_elements.last
+ new!
+ end
end
@@ -12,8 +12,54 @@ def calculate_distance
self.start.distance_to(self.finish)
end
+ def calculate_elevation
+ self.finish.elevation - self.start.elevation
+ end
+
def distance
self.distance = calculate_distance
super
end
+
+ def d_elevation
+ self.d_elevation = calculate_elevation
+ super
+ end
+
+ # List of waypoints for new route element: all from area or finish
+ # waypoint from last route element
+ def continue_waypoints_start
+ last_route_element = self.route.route_elements.last
+ if last_route_element
+ return [last_route_element.finish]
+ else
+ return self.route.area.waypoints
+ end
+ end
+
+ # List of waypoints for new route element: all from area or all except
+ # finish waypoint from last route element with proper order
+ def continue_waypoints_finish
+ last_route_element = self.route.route_elements.last
+ if last_route_element
+ _last_finish = last_route_element.finish
+ _all_waypoints = self.route.area.waypoints
+
+ # remove
+ _all_waypoints -= [_last_finish]
+
+ # add distance
+ _all_waypoints.each do |w|
+ w.tmp_distance = w.distance_to(_last_finish)
+ end
+
+ # and sort
+ _all_waypoints = _all_waypoints.sort{|a,b| a.tmp_distance <=> b.tmp_distance}
+
+ return _all_waypoints
+
+ else
+ return self.route.area.waypoints
+ end
+ end
end
View
@@ -11,6 +11,18 @@ class Waypoint < ActiveRecord::Base
#default_scope lambda { order(:created_at).joins("LEFT JOIN `areas` ON waypoints.area_id = areas.id") }
default_scope lambda { order(:created_at).includes(:area) }
+ # Temporary distance used in continuing route
+ attr_accessor :tmp_distance
+
+ # Used in continuing route to display distance to next waypoint
+ def collection_label
+ _str = self.name
+ if @tmp_distance
+ _str += " (#{tmp_distance})"
+ end
+ return _str
+ end
+
acts_as_mappable :default_units => :kms,
:default_formula => :sphere,
:lat_column_name => :lat,
@@ -2,15 +2,16 @@
= f.error_notification
.form-inputs
- = f.input :real_distance
- = f.input :real_d_elevation
+ - _waypoints_start = @route_element.continue_waypoints_start || @route.area.waypoints
+ - _waypoints_finish = @route_element.continue_waypoints_finish || @route.area.waypoints
+ = f.input :waypoint_start_id, as: :select, collection: _waypoints_start, include_blank: false, label_method: :collection_label
%br
- = f.input :waypoint_start_id, as: :select, collection: @route.area.waypoints, include_blank: false
+ = f.input :waypoint_finish_id, as: :select, collection: _waypoints_finish, include_blank: false, label_method: :collection_label
%br
- = f.input :waypoint_finish_id, as: :select, collection: @route.area.waypoints, include_blank: false
-
+ = f.input :real_distance
+ = f.input :real_d_elevation
.form-actions
= f.button :submit
@@ -0,0 +1,21 @@
+%h1 Continue route
+
+%p
+ Last
+ = link_to route_route_element_path(@route, @last_route_element) do
+ route element
+ (
+ = @last_route_element.id
+ )
+ was from
+ = link_to area_waypoint_path(@route.area.id, @last_route_element.start) do
+ = @last_route_element.start.name
+ to
+ = link_to area_waypoint_path(@route.area, @last_route_element.finish) do
+ = @last_route_element.finish.name
+
+
+
+= render 'form'
+
+= link_to 'Back', route_route_elements_path(params[:route_id])
@@ -4,6 +4,8 @@
%tr
%th Start
%th Finish
+ %th Distance [m]
+ %th &#916; Elevation [m]
%th
%th
%th
@@ -17,10 +19,29 @@
= link_to area_waypoint_path(@route.area, route_element.finish) do
= route_element.finish.name
+ %td
+ - if route_element.real_distance
+ = route_element.real_distance
+ (
+ = route_element.distance
+ )
+ - else
+ = route_element.distance
+
+ %td
+ - if route_element.real_d_elevation
+ = route_element.real_d_elevation
+ (
+ = route_element.d_elevation
+ )
+ - else
+ = route_element.d_elevation
+
%td= link_to 'Show', route_route_element_path(route_element.route, route_element)
%td= link_to 'Edit', edit_route_route_element_path(route_element.route, route_element)
%td= link_to 'Destroy', route_route_element_path(route_element.route, route_element), :confirm => 'Are you sure?', :method => :delete
%br
= link_to 'New Route element', new_route_route_element_path(params[:route_id])
+= link_to 'Continue route', continue_route_route_elements_path(params[:route_id])
View
@@ -1,9 +1,12 @@
WaypointManager::Application.routes.draw do
resources :routes do
- resources :route_elements
+ resources :route_elements do
+ collection do
+ get 'continue'
+ end
+ end
end
- #resources :route_elements
resources :areas do
resources :waypoints
@@ -0,0 +1,6 @@
+class AddRouteRealValues < ActiveRecord::Migration
+ def change
+ add_column :route_elements, :real_distance, :integer, null: true, default: nil
+ add_column :route_elements, :real_d_elevation, :integer, null: true, default: nil
+ end
+end

0 comments on commit 72d0fb3

Please sign in to comment.