Skip to content

Commit

Permalink
fixup! bookings
Browse files Browse the repository at this point in the history
* Added bookings controller
* Added booking model
* Added booking migration
* radio button for selecting a flight
* button to submit data to create booking

todo: figure out how to wire turbodrive so that
the params persist after the form is submitted

chore: add passenger ref to bookings

* css stylesheet for flashes
* show flashes in app layouts
  • Loading branch information
sean-garwood committed Oct 9, 2024
1 parent 5fd06f8 commit e989c0d
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 34 deletions.
15 changes: 15 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@
*= require_tree .
*= require_self
*/
.flash {
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}

.flash.notice {
background-color: #dff0d8;
color: #3c763d;
}

.flash.alert {
background-color: #f2dede;
color: #a94442;
}
22 changes: 22 additions & 0 deletions app/controllers/bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class BookingsController < ApplicationController
def new
@booking = Booking.new
end

def create
@booking = Booking.new(booking_params)
@booking.passenger = current_user

if @booking.save
redirect_to @booking
else
render :new
end
end

private

def booking_params
params.require(:booking).permit(:flight_id, :passenger_id)
end
end
2 changes: 1 addition & 1 deletion app/controllers/flights_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def get_departure_dates
end

def flight_params
params.permit(:departure_airport, :arrival_airport, :available_seats, :depart_date)
params.permit(:id, :commit, :departure_airport, :arrival_airport, :available_seats, :depart_date)
end
end
2 changes: 2 additions & 0 deletions app/helpers/bookings_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BookingsHelper
end
3 changes: 3 additions & 0 deletions app/models/booking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Booking < ApplicationRecord
belongs_to :passenger, class_name: "User", foreign_key: "passenger_id"
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +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"
end
66 changes: 37 additions & 29 deletions app/views/flights/_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
<h2 class="flight-results">
Flight results
</h2>
<table>
<td class="flights">
<tr>
<th>Departs from</th>
<th>Arrives at</th>
<th>Depart time</th>
<th>Duration</th>
<th>Available seats</th>
</tr>
<% @flights.each do |flight| %>
<%= form_with url: bookings_path, method: :post, local: true do |form| %>
<table>
<thead>
<tr>
<td>
<%= flight.departure_airport.code %>
</td>
<td>
<%= flight.arrival_airport.code %>
</td>
<td>
<%= flight.depart_time %>
</td>
<td>
<%# duration is in minutes, so we need to format as HH:MM %>
<%= Time.at(flight.duration * 60).utc.strftime("%H:%M") %>
</td>
<td>
<%= flight.available_seats %>
</td>
<th>Departs from</th>
<th>Arrives at</th>
<th>Depart time</th>
<th>Duration</th>
<th>Available seats</th>
<th>Select</th>
</tr>
<% end %>
</td>
</table>
</thead>
<tbody>
<% @flights.each do |flight| %>
<tr>
<td>
<%= flight.departure_airport.code %>
</td>
<td>
<%= flight.arrival_airport.code %>
</td>
<td>
<%= flight.depart_time %>
</td>
<td>
<%= Time.at(flight.duration * 60).utc.strftime("%H:%M") %>
</td>
<td>
<%= flight.available_seats %>
</td>
<td>
<%= form.radio_button :selected_flight_id, flight.id %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= form.submit "Book Selected Flight" %>
<% end %>
6 changes: 3 additions & 3 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div>
<% flash.each do |msg| %>
<div><%= msg %></div>
<div class="flash-messages">
<% flash.each do |k, message| %>
<div class="flash <%= k %>"><%= message %></div>
<% end %>
</div>
<%= yield %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Rails.application.routes.draw do
devise_for :users
resources :flights, only: [ :index ]
resources :bookings, only: [ :index, :show, :new, :create ]
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20241008203508_create_bookings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateBookings < ActiveRecord::Migration[7.2]
def change
create_table :bookings do |t|
t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20241009133310_add_passenger_ref_to_bookings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPassengerRefToBookings < ActiveRecord::Migration[7.2]
def change
add_reference :bookings, :passenger, null: false, foreign_key: { to_table: :users }
end
end
10 changes: 9 additions & 1 deletion db/schema.rb

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

7 changes: 7 additions & 0 deletions test/controllers/bookings_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class BookingsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/bookings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/booking_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class BookingTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit e989c0d

Please sign in to comment.