Browse Source

lk | changes way application date is stored in db

master
Louis Knapp 9 years ago
parent
commit
ef63d964d4
  1. 11
      app/controllers/clients_controller.rb
  2. 2
      app/views/clients/_fields.html.haml
  3. 2
      config/initializers/date_time.rb
  4. 17
      db/migrate/20151219225720_change_client_application_date_to_string.rb
  5. 5
      db/schema.rb
  6. 3
      notes.txt
  7. 1
      spec/controllers/bikes_controller_spec.rb
  8. 17
      spec/controllers/clients_controller_spec.rb

11
app/controllers/clients_controller.rb

@ -23,14 +23,12 @@ class ClientsController < ApplicationController
end end
def update def update
if @client.update(client_params) @client.update(client_params)
if @client.save
notice = 'Client was successfully updated.' notice = 'Client was successfully updated.'
unless @client.bike.update_attribute(:date_sold, @client.pickup_date) redirect_to edit_client_url(@client), notice: "Client updated"
notice = "Unable to update client's bike sale date"
end
redirect_to edit_client_url(@client), notice: notice
else else
render action: 'edit' render action: 'edit', notice: "Unable to update client"
end end
end end
@ -40,6 +38,7 @@ class ClientsController < ApplicationController
end end
def client_params def client_params
params["client"]["application_date"] = Date.strptime(params["client"]["application_date"], '%m/%d/%Y')
params.require(:client).permit( params.require(:client).permit(
:first_name, :first_name,
:last_name, :last_name,

2
app/views/clients/_fields.html.haml

@ -33,7 +33,7 @@
.form-group .form-group
= f.label "Application Date:", class: "col-sm-2 control-label" = f.label "Application Date:", class: "col-sm-2 control-label"
.col-sm-10 .col-sm-10
= f.text_field :application_date, class: "form-control datepicker", disabled: disabled = f.text_field :application_date, value: @client.application_date && @client.application_date.strftime('%m/%d/%Y'), class: "form-control datepicker", disabled: disabled
.form-group .form-group
= f.label "Agency:", class: "col-sm-2 control-label" = f.label "Agency:", class: "col-sm-2 control-label"

2
config/initializers/date_time.rb

@ -1,5 +1,7 @@
# Date # Date
Date::DATE_FORMATS[:default] = "%m/%d/%Y" Date::DATE_FORMATS[:default] = "%m/%d/%Y"
Date::DATE_FORMATS[:db] = "%m/%d/%Y"
# Time # Time
Time::DATE_FORMATS[:default] = "%m/%d/%Y" Time::DATE_FORMATS[:default] = "%m/%d/%Y"
Date::DATE_FORMATS[:db] = "%m/%d/%Y"

17
db/migrate/20151219225720_change_client_application_date_to_string.rb

@ -0,0 +1,17 @@
class ChangeClientApplicationDateToString < ActiveRecord::Migration
def up
add_column :clients, :application_datetime, :datetime
Client.all.to_a.each{ |client|
if client.application_date
client.update_attribute(:application_datetime, client.application_date)
end
}
rename_column :clients, :application_date, :application_date_bkp
rename_column :clients, :application_datetime, :application_date
end
def down
remove_column :clients, :application_date
rename_column :clients, :application_date_bkp, :application_date
end
end

5
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: 20151205223152) do ActiveRecord::Schema.define(version: 20151219225720) 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"
@ -51,7 +51,7 @@ ActiveRecord::Schema.define(version: 20151205223152) do
create_table "clients", force: :cascade do |t| create_table "clients", force: :cascade do |t|
t.string "first_name" t.string "first_name"
t.string "last_name" t.string "last_name"
t.date "application_date" t.date "application_date_bkp"
t.string "gender" t.string "gender"
t.integer "age" t.integer "age"
t.boolean "helmet" t.boolean "helmet"
@ -70,6 +70,7 @@ ActiveRecord::Schema.define(version: 20151205223152) do
t.string "volunteer_at_pickup" t.string "volunteer_at_pickup"
t.float "weight" t.float "weight"
t.float "height" t.float "height"
t.datetime "application_date"
end end
add_index "clients", ["agency_id"], name: "index_clients_on_agency_id", using: :btree add_index "clients", ["agency_id"], name: "index_clients_on_agency_id", using: :btree

3
notes.txt

@ -1,3 +1,6 @@
on website, tailwind for tools initiative looks weird in carousel
on mobile
Freecyclery Receipts Freecyclery Receipts
convert to bootstrap-less convert to bootstrap-less

1
spec/controllers/bikes_controller_spec.rb

@ -28,6 +28,7 @@ describe BikesController do
end end
end end
describe "GET #new" do describe "GET #new" do
it "assigns a log number" do it "assigns a log number" do
FactoryGirl.create(:bike, log_number: 3) FactoryGirl.create(:bike, log_number: 3)

17
spec/controllers/clients_controller_spec.rb

@ -0,0 +1,17 @@
require 'spec_helper'
describe ClientsController do
let(:user){FactoryGirl.create(:user)}
let(:client){FactoryGirl.create(:client)}
before :each do
sign_in user
end
describe "PUT #update" do
it "updates a client with an application date" do
put :update, id: client.id, client: {application_date: "12/21/2015"}
expect(client.reload.application_date.strftime('%m/%d/%Y')).to eq("12/21/2015")
end
end
end
Loading…
Cancel
Save