Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added more filter options, improved search performance

  • Loading branch information...
commit c7e0bf7356f2a0847625786ee55622445f089b7f 1 parent b6975e2
Oldrich Vetesnik authored
69 app/controllers/airports_controller.rb
... ... @@ -1,14 +1,24 @@
1 1 class AirportsController < ApplicationController
  2 + autocomplete :aircraft, :name, :full => true
2 3 autocomplete :airport, :name_with_code, :full => true, :extra_data => [ :code ], :limit => 20
3 4 autocomplete :country, :name, :full => true
4 5
5 6 def index
6   -
7 7 end
8 8
9 9 def select_options
10   - @aircraft = Aircraft.find(params[:aircraft]) if params[:aircraft].present?
11   - @country = Country.find(params[:country]) if params[:country].present?
  10 + if params[:aircraft_name].present?
  11 + @aircraft = Aircraft.where(:name.matches => params[:aircraft_name]).first
  12 + elsif params[:aircraft_id].present?
  13 + @aircraft = Aircraft.find params[:aircraft_id]
  14 + end
  15 +
  16 + if params[:country_name].present?
  17 + @country = Country.where(:name.matches => params[:country_name]).first
  18 + elsif params[:country_id].present?
  19 + @country = Country.find params[:country_id]
  20 + end
  21 +
12 22 list
13 23 render 'index'
14 24 end
@@ -20,34 +30,14 @@ def list
20 30 modify_relation_by_aircraft!
21 31 set_relation_order!
22 32 @call_count = 0
23   - 5.times { create_waypoint! }
24   -
25   - puts '========================='
26   - p @airports.map(&:code)
27   - puts '========================='
28   -
29   - # #rel.where(:id.not_eq => @airport1.id, :country_id.eq => @airport1.country.id).each do |airport|
30   - # rel.where(:id.not_eq => @airport1.id).each do |airport|
31   - # #puts "#{ airport.id }"
32   - # if @aircraft.helicopter
33   - # if airport.distance_to(@airport1) <= 50
34   - # @airport2 = airport
35   - # break
36   - # end
37   - # else
38   - # if airport.distance_to(@airport1) <= @aircraft.range
39   - # @airport2 = airport
40   - # break
41   - # end
42   - # end
43   - # end
44   - #
45   - # @distance = @airport1.distance_to @airport2
46   - # t = (@distance.to_f / @aircraft.cruise_speed * 60 * 60).round
47   - # mm, ss = t.divmod 60
48   - # hh, mm = mm.divmod 60
49   - # dd, hh = hh.divmod 24
50   - # @eta = "#{ hh }:#{ mm } m"
  33 + (params[:no_of_waypoints].present? ? params[:no_of_waypoints] : 2).to_i.times { create_waypoint! }
  34 +
  35 + @total_distance = find_total_distance @first_waypoint
  36 + t = (@total_distance.to_f / @aircraft.cruise_speed * 60 * 60).round
  37 + mm, ss = t.divmod 60
  38 + hh, mm = mm.divmod 60
  39 + dd, hh = hh.divmod 24
  40 + @eta = sprintf "%02d:%02d", hh, mm
51 41 end
52 42
53 43 protected
@@ -70,7 +60,6 @@ def modify_relation_by_aircraft!
70 60
71 61 def set_relation_order!
72 62 @rel = @rel.order('RAND()')
73   - #@rel = @rel.where(:country_id.eq => 69)
74 63 @rel = @rel.where(:country_id.eq => @country.id) if @country
75 64 end
76 65
@@ -97,11 +86,23 @@ def create_waypoint!
97 86 end
98 87
99 88 def next_airport!
100   - @next_airport = @rel.first
  89 + unless @airports.empty?
  90 + @rel = @rel.where(:country_id.eq => @first_waypoint.airport.country.id) if @aircraft.range < 500
  91 + @next_airport = @rel.where(:id.not_in => @airports.map(&:id)).first
  92 + else
  93 + @next_airport = @rel.first
  94 + end
101 95 end
102 96
103 97 def exclude_selected_airports!
104 98 @airports << @next_airport
105   - @rel = @rel.where(:id.not_in => @airports.map(&:id)) unless @airports.empty?
  99 + end
  100 +
  101 + def find_total_distance(waypoint)
  102 + distance = waypoint.distance
  103 + if next_waypoint = waypoint.to
  104 + distance += find_total_distance(next_waypoint)
  105 + end
  106 + distance
106 107 end
107 108 end
15 app/views/airports/index.html.haml
@@ -2,16 +2,21 @@
2 2
3 3 = form_tag select_options_path do
4 4 %p
5   - = label_tag :aircraft, 'Select an aircraft'
6   - = select_tag :aircraft, options_for_select(Aircraft.all.map { |i| [ i.name, i.id ] }, params[:aircraft]), :include_blank => 'Choose'
  5 + = label_tag :aircraft_name, 'Select an aircraft'
  6 + = autocomplete_field_tag :aircraft_name, params[:aircraft_name], airports_autocomplete_aircraft_name_path, :update_elements => { :code => '#nevim_start' }
  7 + = select_tag :aircraft_id, options_for_select(Aircraft.all.map { |i| [ i.name, i.id ] }, params[:aircraft_id]), :include_blank => 'Choose'
7 8 %p
8   - = label_tag :country, 'Select a country'
9   - = select_tag :country, options_for_select(Country.all.map { |i| [ i.name, i.id ] }, params[:country]), :include_blank => 'Choose'
  9 + = label_tag :country_name, 'Select a country'
  10 + = autocomplete_field_tag :country_name, params[:country_name], airports_autocomplete_country_name_path, :update_elements => { :code => '#nevim_start' }
  11 + = select_tag :country_id, options_for_select(Country.all.map { |i| [ i.name, i.id ] }, params[:country_id]), :include_blank => 'Choose'
  12 + %p
  13 + = label_tag :no_of_waypoints, 'Number of waypoints'
  14 + = text_field_tag :no_of_waypoints, params[:no_of_waypoints].present? ? params[:no_of_waypoints] : 2, :size => 2
10 15 %p= submit_tag 'Select'
11 16
12 17
13 18 - if @first_waypoint.present?
14   - %p= "Aircraft #{ @aircraft.name }"
  19 + %p= "Aircraft #{ @aircraft.name }, cruise speed #{ @aircraft.cruise_speed }, ETA #{ @eta }."
15 20
16 21 %table
17 22 %thead
1  config/routes.rb
... ... @@ -1,4 +1,5 @@
1 1 Fsx::Application.routes.draw do
  2 + get 'airports/autocomplete_aircraft_name'
2 3 get 'airports/autocomplete_airport_name_with_code'
3 4 get 'airports/autocomplete_country_name'
4 5

0 comments on commit c7e0bf7

Please sign in to comment.
Something went wrong with that request. Please try again.