Skip to content

Commit

Permalink
chore: add flight_id to bookings
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-garwood committed Oct 11, 2024
1 parent c2395c3 commit 18d6d20
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 32 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*= require_self
*= require_flight_table
*/
* {
/* better font */
font-family: 'Open Sans', sans-serif;
/* rm link formatting */
text-decoration: none;
}

.flash {
padding: 10px;
margin-bottom: 10px;
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/flight_table.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
tr:nth-child(odd) {
background-color: #f9f9f9;
}

/* better font */
font-family: 'Open Sans',
sans-serif;
}
4 changes: 0 additions & 4 deletions app/controllers/flights_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class FlightsController < ApplicationController
def index
@arrival_codes = Airport.arrival_codes
@departure_codes = Airport.departure_codes
@departure_dates = Flight.departure_dates
@available_seats = (1..4).to_a
if flight_params.empty?
@flights = Flight.all
else
Expand Down
1 change: 1 addition & 0 deletions app/models/booking.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Booking < ApplicationRecord
belongs_to :passenger, class_name: "User", foreign_key: "passenger_id"
belongs_to :flight
end
7 changes: 6 additions & 1 deletion app/models/flight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ class Flight < ApplicationRecord
end
end

def self.available_seats
select(:available_seats).map(&:available_seats).uniq.sort
end

def self.departure_dates
select("DISTINCT DATE(flights.depart_time) as depart_date").order("depart_date").map(&:depart_date)
select("DISTINCT DATE(flights.depart_time) as depart_date").
order("depart_date").map(&:depart_date)
end

def self.search_by_airports(departure_airport_code, arrival_airport_code)
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :bookings, foreign_key: "passenger_id"
has_many :bookings
end
10 changes: 5 additions & 5 deletions app/views/flights/_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h2 class="flight-results">
<h2>
Flight results
</h2>
<%= form_with model: @booking, url: bookings_path, local: true do |form| %>
<table>
<%= form_with model: @booking, url: new_booking_path do |form| %>
<table class="flight-results">
<thead>
<tr>
<th>Departs from</th>
Expand All @@ -14,7 +14,7 @@
</tr>
</thead>
<tbody>
<% if @flights.present? %>
<% if @flights.any? %>
<% @flights.each do |flight| %>
<tr>
<td>
Expand Down Expand Up @@ -45,5 +45,5 @@
</tbody>
</table>
<%= form.submit "Book Selected Flight" %>
<%= link_to "Show all", flights_path %>
<% end %>
<%= link_to "Show all", flights_path %>
32 changes: 12 additions & 20 deletions app/views/flights/_search.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
<h2>Enter your desired flight attributes here.</h2>
<%= form_with url: flights_path, method: :get, local: true do |form| %>
<div>
<%= form.label :departure_airport, "Departure Airport" %>
<%= form.select :departure_airport, options_for_select(@departure_codes),
include_blank: true %>
</div>
<div>
<%= form.label :arrival_airport, "Arrival Airport" %>
<%= form.select :arrival_airport, options_for_select(@arrival_codes),
include_blank: true %>
</div>
<div>
<%= form.label :available_seats, "Number of passengers" %>
<%= form.select :available_seats, options_for_select(@available_seats),
include_blank: true%>
</div>
<div>
<%= form.label :depart_date, "Departure date" %>
<%= form.select :depart_date, options_for_select(@departure_dates),
include_blank: true%>
<%= form_with method: :get do |form| %>
<div>Depart from:
<%= form.select :departure_airport, Airport.departure_codes, include_blank:
true %></div>
<div>Arrive at:
<%= form.select :arrival_airport, Airport.arrival_codes,
include_blank: true %></div>
<div>Required seats:
<%= form.select :available_seats, Flight.available_seats,
include_blank: true %></div>
<div>Departure date:
<%= form.select :depart_date, Flight.departure_dates, include_blank: true%>
</div>
<div>
<%= form.submit "Search flights" %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241011205934_add_flight_id_to_bookings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFlightIdToBookings < ActiveRecord::Migration[7.2]
def change
add_reference :bookings, :flight, null: false, foreign_key: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 18d6d20

Please sign in to comment.