Skip to content

Commit

Permalink
Add the ability to create cars and refuels
Browse files Browse the repository at this point in the history
  • Loading branch information
pjones committed Nov 4, 2011
1 parent 2ba4d36 commit da7d7de
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 14 deletions.
22 changes: 22 additions & 0 deletions app/assets/stylesheets/basic.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* This file is a Sass file (http://sass-lang.com/) which is very
similar to CSS3 but it is translated to CSS2 */

#content {
width: 800px;
margin: 0 auto;
padding: 20px 10px;
}

table {
margin: 20px 0;
border-collapse: collapse;

td, th {
border: 1px solid #eee;
padding: 6px;
}

thead tr {
background-color: #ddd;
}
}
52 changes: 52 additions & 0 deletions app/assets/stylesheets/forms.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
form {
margin: 0 0 25px 0;
font-family: Helvetica;

fieldset {
padding: 10px;
border: 1px solid #f1f1f1;
}

legend {
padding: 0 8px;
font-size: 1.1em;
font-weight: bold;
}

label {
margin: 0;
padding: 0 0 3px 0;
font-size: .8em;
font-weight: bold;
display: block;
}

input, textarea {
margin: 0;
padding: 0;
}

input[type="text"],
input[type="email"],
input[type="url"],
input[type="password"],
textarea {
width: 450px;
height: 23px;
font-size: 16px;
border: 1px solid silver;
}

textarea {
resize: none;
font-size: 12px;
height: 130px;
}

input[type="submit"],
input[type="button"] {
margin: 10px 0 10px 30px;
padding: 4px;
font-size: 1.2em;
}
}
50 changes: 50 additions & 0 deletions app/controllers/cars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
class CarsController < ApplicationController

##############################################################################
# The following class method tells Rails what formats we can respond
# with. This is necessary to use the `respond_with` helper later.
respond_to(:html, :xml, :json)

##############################################################################
# Load all cars for the current user. Rails automatically renders
# the app/views/cars/index.html.erb file.
def index
@cars = current_user.cars.order(:name)
respond_with(@cars)
end

##############################################################################
# Normally, if a model had enough information to display on a
# dedicated page you would do so here in the show action. Since a
# car only has a name it doesn't make much sense to show that by
# itself, so we'll just redirect to the index action.
def show
redirect_to(cars_path)
end

##############################################################################
# Using `respond_with` below will automatically render
# app/views/cars/new.html.erb if the current request wants HTML, or
# it can render XML or JSON if that's what the request calls for.
def new
@car = current_user.cars.new
respond_with(@car)
end

##############################################################################
def create
@car = current_user.cars.create(params[:car])
respond_with(@car)
end

##############################################################################
def edit
@car = current_user.cars.find(params[:id])
respond_with(@car)
end

##############################################################################
def update
@car = current_user.cars.find(params[:id])
@car.update_attributes(params[:car])
respond_with(@car)
end

##############################################################################
def destroy
@car = current_user.cars.find(params[:id])
@car.destroy
respond_with(@car)
end
end
68 changes: 68 additions & 0 deletions app/controllers/refuels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class RefuelsController < ApplicationController

##############################################################################
# Establish a filter that will run before any of the actions in this
# controller run. Its job will be to create a `@car` instance
# variable that we can use to access refuel objects. The
# `fetch_car` method can be found at the bottom of this file.
before_filter(:fetch_car)

##############################################################################
# See the note in cars_controller.rb
respond_to(:html, :xml, :json)

##############################################################################
def index
@refuels = @car.refuels.order('refueled_at DESC')
respond_with(@car, @refuels) # need @car because this is a nested resource
end

##############################################################################
def show
@refuel = @car.refuels.find(params[:id])
respond_with(@car, @refuel)
end

##############################################################################
def new
@refuel = @car.refuels.new
respond_with(@car, @refuel)
end

##############################################################################
def create
@refuel = @car.refuels.create(params[:refuel])
respond_with(@car, @refuel)
end

##############################################################################
def edit
@refuel = @car.refuels.find(params[:id])
respond_with(@car, @refuel)
end

##############################################################################
def update
@refuel = @car.refuels.find(params[:id])
@refuel.update_attributes(params[:refuel])
respond_with(@car, @refuel)
end

##############################################################################
def destroy
@refuel = @car.refuels.find(params[:id])
@refuel.destroy
respond_with(@car, @refuel)
end

##############################################################################
private

##############################################################################
# Since this controller is a nested resource under the cars
# resource, all invocations will include a `:car_id` parameter to
# tell us which car we are working with.
def fetch_car
@car = current_user.cars.find(params[:car_id])
end
end
2 changes: 2 additions & 0 deletions app/helpers/refuels_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RefuelsHelper
end
18 changes: 18 additions & 0 deletions app/models/refuel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Refuel < ActiveRecord::Base
##############################################################################
belongs_to(:car) # refuel.car

##############################################################################
# A scope is a way to give a name to database query so it can be
# accessed by other models, controllers, and views. You can see
# this being used in app/views/cars/index.html.erb.
scope(:most_recent, order('refueled_at DESC').limit(1))

##############################################################################
# This method is used to find a refuel that occurred just prior to
# this one. We'll use this later to calculate MPG and distance
Expand All @@ -42,6 +48,18 @@ def following
car.refuels.where('refueled_at > ?', refueled_at).order('refueled_at').first
end

##############################################################################
def formatted_mpg
"%.2f" % [mpg || 0.0]
end

##############################################################################
def cost_per_mile
if other = preceding
preceding.price / distance
end
end

##############################################################################
private

Expand Down
17 changes: 17 additions & 0 deletions app/views/cars/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= form_for(@car) do |form| %>
<fieldset>
<legend><%= @title %></legend>

<%# Error messages would go here %>

<p>
<%= form.label(:name) %>
<%= form.text_field(:name) %>
</p>

<p>
<%= button_to_function('Cancel', "window.location='#{cars_path}'") %>
<%= form.submit %>
</p>
</fieldset>
<% end %>
2 changes: 2 additions & 0 deletions app/views/cars/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% @title = 'Edit Car' %>
<%= render('form') %>
23 changes: 21 additions & 2 deletions app/views/cars/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% @title = 'Cars' %>
<% @title = 'Your Cars' %>
<h1><%= @title %></h1>

<% if @cars.empty? %>
<p>
Expand All @@ -11,14 +12,32 @@
<thead>
<tr>
<th>Name</th>
<th>Last MPG</th>
<th>Cost Per Mile</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @cars.each do |car| %>
<% refuel = car.refuels.most_recent.first %>

<tr>
<td><%= car.name %></td>
<td><%= link_to(car.name, edit_car_path(car)) %></td>
<td><%= refuel && refuel.formatted_mpg %></td>
<td><%= refuel && refuel.cost_per_mile.try(:format) %></td>
<td><%= link_to('Refuel', new_car_refuel_path(car)) %></td>
<td><%= link_to('History', car_refuels_path(car)) %></td>
<td><%= link_to('Delete', car_path(car), :method => :delete, :confirm => 'Are you sure?') %></td>
</tr>
<% end %>
</tbody>
</table>

<% if !@cars.empty? %>
<%= link_to('Create a new car', new_car_path) %><br/>
<% end %>

<%= link_to('Log out', logout_path) %>
3 changes: 3 additions & 0 deletions app/views/cars/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @title = 'New Car' %>
<%# The following line renders app/views/cars/_form.html.erb %>
<%= render('form') %>
24 changes: 13 additions & 11 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
<head>
<title><%= @title %></title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="content">
<% if flash.notice %><div class="notice"><%= flash.notice %></div><% end %>
<% if flash.alert %><div class="alert" ><%= flash.alert %></div><% end %>
<%= yield %>
</div>
</body>
</html>
32 changes: 32 additions & 0 deletions app/views/refuels/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= form_for([@car, @refuel]) do |form| %>
<fieldset>
<legend><%= @title %></legend>

<%# Error messages would go here %>

<p>
<%= form.label(:refueled_at, 'Refuel Date') %>
<%= form.datetime_select(:refueled_at) %>
</p>

<p>
<%= form.label(:odometer, 'Current Odometer Reading') %>
<%= form.number_field(:odometer) %>
</p>

<p>
<%= form.label(:gallons, 'Number of Gallons for This Refuel') %>
<%= form.number_field(:gallons) %>
</p>

<p>
<%= form.label(:price, 'Total Cost of This Refuel') %>
<%= form.number_field(:price) %>
</p>

<p>
<%= button_to_function('Cancel', "window.location='#{cars_path}'") %>
<%= form.submit %>
</p>
</fieldset>
<% end %>
2 changes: 2 additions & 0 deletions app/views/refuels/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% @title = 'Edit Refuel Record For Your ' + @car.name %>
<%= render('form') %>
Loading

0 comments on commit da7d7de

Please sign in to comment.