Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeBlyth committed Jan 18, 2013
1 parent 5f874ac commit 05cca47
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
File renamed without changes.
4 changes: 3 additions & 1 deletion app/controllers/patients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def do_show

def create
@record = patient = Patient.new(params[:patient])
# binding.pry
if patient.save
flash[:notice] = "Created new patient #{patient}"
render :show
Expand All @@ -39,7 +40,8 @@ def update
flash[:notice] = 'Patient updated'
redirect_to patient_path(patient.id)
else
render :edit

render :update
end
end

Expand Down
32 changes: 31 additions & 1 deletion app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class Patient < ActiveRecord::Base
include NamesHelper

attr_accessible :first_name, :ident, :last_name, :other_names, :sex, :residence, :phone, :caregiver, :birth_date,
:death_date, :birth_date, :birth_date_exact,:death_date,
:death_date, :birth_date_text, :birth_date_exact,:death_date,
:allergies, :hemoglobin_type, :hiv_status, :maternal_hiv_status

attr_writer :birth_date_text

has_many :visits
has_many :lab_requests
has_many :lab_results, through: :lab_requests
Expand All @@ -51,9 +53,11 @@ class Patient < ActiveRecord::Base

validates_presence_of :last_name, :ident, :birth_date
validates_uniqueness_of :ident
before_validation :save_birth_date_text
validate :valid_birth_date



############ NAME METHODS
def to_s
name_id
Expand Down Expand Up @@ -167,6 +171,15 @@ def next_appt

############### OTHER METHODS

def birth_date_text
@birth_date_text || birth_date.to_s(:day_mon_year)
end

def save_birth_date_text
self.birth_date = validate_birth_date_text unless @birth_date_text.blank?
#binding.pry
end

def date
birth_date
end
Expand Down Expand Up @@ -204,6 +217,23 @@ def recent_drugs(since=3)
RxDrugList.new.add_prescriptions(recent_prescriptions)
end

def validate_birth_date_text
if @birth_date_text.blank?
errors.add :birth_date_text, "cannot be blank"
return nil
end
parsed = Time.zone.parse(@birth_date_text)
if parsed
return parsed
else
errors.add :birth_date_text, "cannot be parsed"
return nil
end
rescue ArgumentError
errors.add :birth_date_text, "is out of range"
return nil
end

#def time_valid?(str)
# return false unless str =~ /\A\s*([0-9]{1,2}):([0-9]{2,2})(\s+|\Z)(am|pm)?/i
# hour, minute, am_pm = $1, $2, $4
Expand Down
2 changes: 1 addition & 1 deletion app/views/patients/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
= form.input :ident, :input_html => {class: :span12 }
.row-fluid
.span2
= form.input :birth_date, as: :string, :input_html => {class: "span12 datepicker" }
= form.input :birth_date_text, as: :string, :input_html => {class: "span12" }
.span2
= form.input :birth_date_exact, label: "Exact birth date?"
.span2
Expand Down
1 change: 1 addition & 0 deletions config/initializers/date_formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# TIME::DATE_FORMATS for formatting .to_s.

Time::DATE_FORMATS[:date_long] = "%-1e %B %Y"
Time::DATE_FORMATS[:day_mon_year] = "%-1e %b %Y"
Time::DATE_FORMATS[:us_date_long] = "%B %-1e, %Y"
Time::DATE_FORMATS[:date] = "%-1e %B"
Time::DATE_FORMATS[:us_date] = "%B %-1e"
Expand Down

0 comments on commit 05cca47

Please sign in to comment.