Class Client
In: app/models/client.rb
Parent: ActiveRecord::Base

Client

A client can make a Booking to see a Clinician. Currently there is only a small amount of information in the Client object. This is likely to be expanded.

Clients are mainly controlled through the ClientController

Rules

  • A client may have many Bookings
  • A client must have a first_name
  • A client must have a last_name
  • Each client must have a unique record_number
  • Client names and record numbers may only consist of letters, numbers and spaces
  • A phone number may only consist of numbers, brackets and spaces

Methods

Public Class methods

Finds a client record from a string either in format: name (record number) OR record_number

[Source]

    # File app/models/client.rb, line 40
40:    def Client.find_by_full_name(client_name)
41:      # Try to extract record number
42:      if client_record_number = /\((.)*\)/.match(client_name).to_s
43:        client_record_number = client_record_number.to_s[1, client_record_number.length-2]
44:      end
45:     
46:      # If can't find client, try using whole string as record_number
47:      if !client = Client.find_by_record_number(client_record_number)
48:        client = Client.find_by_record_number(client_name)
49:      end
50:     
51:      return client
52:    end

Public Instance methods

Psuedo property full_name returns the client‘s name in the form last_name, first_name

Usage:

   client_display_name = client.full_name

[Source]

    # File app/models/client.rb, line 33
33:    def full_name
34:      return "#{self.last_name}, #{self.first_name}"
35:    end

Method to determine whether a particular client has and bookings. Returns true if there is at least one either past or future.

Usage

   clients_with_bookings << client if client.has_bookings?

[Source]

    # File app/models/client.rb, line 60
60:    def has_bookings?
61:      has_bookings = false
62:      self.bookings.each do |b|
63:       has_bookings = true
64:      end   
65:      return has_bookings
66:    end

Method to determine whether a client is booked during a time period. Optionally takes a param to ignore a specific session

Usage

   make_booking unless client.is_booked?(session.start, session.finish)

[Source]

     # File app/models/client.rb, line 74
 74:    def is_booked?(start, finish, ignore_session = 0)
 75:     isbooked = false
 76:       
 77:     if cs = ClinicalSession.find(:all, :conditions => "start <= '#{start.to_s(:db)}' AND '#{start.to_s(:db)}' < finish AND id <> #{ignore_session}")
 78:       cs.each do | session |
 79:         if session.booked?
 80:           isbooked = true if session.client_booked_in?(self)
 81:         end
 82:       end
 83:     end
 84: 
 85:     if cs = ClinicalSession.find(:all, :conditions => "start < '#{finish.to_s(:db)}' AND '#{finish.to_s(:db)}' <= finish AND id <> #{ignore_session}")
 86:       cs.each do | session |
 87:         if session.booked?
 88:           isbooked = true if session.client_booked_in?(self)
 89:         end
 90:       end
 91:     end
 92:       
 93:     # Test for (1)
 94:     if cs = ClinicalSession.find(:all, :conditions => "start > '#{start.to_s(:db)} ' AND '#{finish.to_s(:db)}' > finish AND id <> #{ignore_session}")
 95:       cs.each do | session |
 96:         if session.booked?
 97:           isbooked = true if session.client_booked_in?(self)
 98:         end
 99:       end
100:     end
101:     
102:     return isbooked
103:   end

[Validate]