Browse Source

lk | add time_spent field to bikes

master
Louis Knapp 8 years ago
parent
commit
d49f6030ed
  1. 1
      app/controllers/bikes_controller.rb
  2. 1
      app/models/bike.rb
  3. 16
      app/views/bikes/_fields.html.haml
  4. 5
      db/migrate/20170713233459_add_time_spent_to_bikes.rb
  5. 3
      db/schema.rb
  6. 6
      spec/models/bike_spec.rb

1
app/controllers/bikes_controller.rb

@ -76,6 +76,7 @@ class BikesController < ApplicationController
params[:bike][:fixed_at] = DateTime.strptime(params[:bike][:fixed_at], "%m/%d/%Y") if params[:bike][:fixed_at].present? params[:bike][:fixed_at] = DateTime.strptime(params[:bike][:fixed_at], "%m/%d/%Y") if params[:bike][:fixed_at].present?
params[:bike][:date_sold] = DateTime.strptime(params[:bike][:date_sold], "%m/%d/%Y") if params[:bike][:date_sold].present? params[:bike][:date_sold] = DateTime.strptime(params[:bike][:date_sold], "%m/%d/%Y") if params[:bike][:date_sold].present?
params.require(:bike).permit( params.require(:bike).permit(
:time_spent,
:fixed_at, :fixed_at,
:brand, :brand,
:model, :model,

1
app/models/bike.rb

@ -5,6 +5,7 @@ class Bike < ActiveRecord::Base
validates :bike_type, presence: true validates :bike_type, presence: true
validates :color, presence: true validates :color, presence: true
validates :serial_number, presence: true validates :serial_number, presence: true
validates_numericality_of :time_spent, greater_than_or_equal_to: 0, allow_nil: true
has_one :client has_one :client
FREECYCLERY = "Freecyclery" FREECYCLERY = "Freecyclery"

16
app/views/bikes/_fields.html.haml

@ -30,6 +30,11 @@
.col-sm-6 .col-sm-6
= f.text_field :color, class: "form-control", disabled: disabled = f.text_field :color, class: "form-control", disabled: disabled
.form-group
= f.label "Purpose:", class: "col-sm-4 control-label"
.col-sm-6
= f.select :purpose, [["Freecyclery", "Freecyclery"], ["Sale", "Sale"]], {}, class: "selectpicker", disabled: disabled
.col-sm-6 .col-sm-6
.form-group .form-group
= f.label "Mechanic:", class: "col-sm-4 control-label" = f.label "Mechanic:", class: "col-sm-4 control-label"
@ -56,14 +61,13 @@
.col-sm-6 .col-sm-6
= f.text_field :fixed_at, class: "form-control datepicker", :value => @bike.fixed_at ? @bike.fixed_at.strftime("%m/%d/%Y") : "", disabled: disabled = f.text_field :fixed_at, class: "form-control datepicker", :value => @bike.fixed_at ? @bike.fixed_at.strftime("%m/%d/%Y") : "", disabled: disabled
.row
.col-sm-6
%br
%br
.form-group .form-group
= f.label "Purpose:", class: "col-sm-4 control-label" = f.label "Time Spent (Hours):", class: "col-sm-4 control-label"
.col-sm-6
= f.text_field :time_spent, class: "form-control", type: "number", disabled: disabled
.row
.col-sm-6 .col-sm-6
= f.select :purpose, [["Freecyclery", "Freecyclery"], ["Sale", "Sale"]], {}, class: "selectpicker", disabled: disabled
.form-group .form-group
= f.label "New Parts", class: "col-sm-4 control-label" = f.label "New Parts", class: "col-sm-4 control-label"

5
db/migrate/20170713233459_add_time_spent_to_bikes.rb

@ -0,0 +1,5 @@
class AddTimeSpentToBikes < ActiveRecord::Migration
def change
add_column :bikes, :time_spent, :int
end
end

3
db/schema.rb

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160731020658) do ActiveRecord::Schema.define(version: 20170713233459) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -46,6 +46,7 @@ ActiveRecord::Schema.define(version: 20160731020658) do
t.datetime "date_sold" t.datetime "date_sold"
t.integer "bike_index_id" t.integer "bike_index_id"
t.datetime "fixed_at" t.datetime "fixed_at"
t.integer "time_spent"
end end
create_table "clients", force: :cascade do |t| create_table "clients", force: :cascade do |t|

6
spec/models/bike_spec.rb

@ -1,6 +1,12 @@
require 'spec_helper' require 'spec_helper'
describe Bike do describe Bike do
describe "validations" do
it "is invalid if time_spent is negative" do
bike = build :bike, time_spent: -3
expect(bike.valid?).to be false
end
end
describe "#sold?" do describe "#sold?" do
it "returns true if the date_sold is present" do it "returns true if the date_sold is present" do

Loading…
Cancel
Save