From d49f6030ed7de29987660b6fcb0a76e0a770eee1 Mon Sep 17 00:00:00 2001 From: Louis Knapp Date: Thu, 13 Jul 2017 18:44:58 -0500 Subject: [PATCH] lk | add time_spent field to bikes --- app/controllers/bikes_controller.rb | 1 + app/models/bike.rb | 1 + app/views/bikes/_fields.html.haml | 16 ++++++++++------ .../20170713233459_add_time_spent_to_bikes.rb | 5 +++++ db/schema.rb | 3 ++- spec/models/bike_spec.rb | 6 ++++++ 6 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20170713233459_add_time_spent_to_bikes.rb diff --git a/app/controllers/bikes_controller.rb b/app/controllers/bikes_controller.rb index 338c0cd..50c69b3 100644 --- a/app/controllers/bikes_controller.rb +++ b/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][:date_sold] = DateTime.strptime(params[:bike][:date_sold], "%m/%d/%Y") if params[:bike][:date_sold].present? params.require(:bike).permit( + :time_spent, :fixed_at, :brand, :model, diff --git a/app/models/bike.rb b/app/models/bike.rb index e2e7115..e3cc12f 100644 --- a/app/models/bike.rb +++ b/app/models/bike.rb @@ -5,6 +5,7 @@ class Bike < ActiveRecord::Base validates :bike_type, presence: true validates :color, presence: true validates :serial_number, presence: true + validates_numericality_of :time_spent, greater_than_or_equal_to: 0, allow_nil: true has_one :client FREECYCLERY = "Freecyclery" diff --git a/app/views/bikes/_fields.html.haml b/app/views/bikes/_fields.html.haml index 2a16b8f..a0d80b4 100644 --- a/app/views/bikes/_fields.html.haml +++ b/app/views/bikes/_fields.html.haml @@ -30,6 +30,11 @@ .col-sm-6 = 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 .form-group = f.label "Mechanic:", class: "col-sm-4 control-label" @@ -56,14 +61,13 @@ .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 - .row - .col-sm-6 - %br - %br .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.select :purpose, [["Freecyclery", "Freecyclery"], ["Sale", "Sale"]], {}, class: "selectpicker", disabled: disabled + = f.text_field :time_spent, class: "form-control", type: "number", disabled: disabled + + .row + .col-sm-6 .form-group = f.label "New Parts", class: "col-sm-4 control-label" diff --git a/db/migrate/20170713233459_add_time_spent_to_bikes.rb b/db/migrate/20170713233459_add_time_spent_to_bikes.rb new file mode 100644 index 0000000..84615db --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index f7dffc6..5c3b91e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 enable_extension "plpgsql" @@ -46,6 +46,7 @@ ActiveRecord::Schema.define(version: 20160731020658) do t.datetime "date_sold" t.integer "bike_index_id" t.datetime "fixed_at" + t.integer "time_spent" end create_table "clients", force: :cascade do |t| diff --git a/spec/models/bike_spec.rb b/spec/models/bike_spec.rb index 2dc8acd..0f66422 100644 --- a/spec/models/bike_spec.rb +++ b/spec/models/bike_spec.rb @@ -1,6 +1,12 @@ require 'spec_helper' 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 it "returns true if the date_sold is present" do