Class ClientController
In: app/controllers/client_controller.rb
Parent: ApplicationController

Client Controller

The Client controller manages the display and management of all clients.

Also contains the client report

Methods

Public Instance methods

[Source]

     # File app/controllers/client_controller.rb, line 145
145:   def auto_complete_for_client_full_name
146:     conditions = "last_name = ''"
147:     client_name = params[:client][:full_name].split(',')
148:     client_name.each do |name|
149:       conditions << "OR last_name LIKE '%#{name.strip}%' OR first_name LIKE '%#{name.strip}%'"
150:     end
151:     
152:     @client_matches = Client.find(:all, 
153:                                   :conditions => conditions)  
154:                                   
155:     @client_matches.each do |client|
156:       client[:full_name] = client.full_name + " (#{client.record_number})"
157:     end
158:     
159:     render :inline => "<%= auto_complete_result @client_matches, 'full_name' %>"
160:   end

Create the new client

Redirects to index after

[Source]

    # File app/controllers/client_controller.rb, line 63
63:   def create
64:     if params[:cancel]
65:         if !params[:referer] || params[:referer].match(/new/) || params[:referer].match(/create/)
66:           redirect_to :action => :index
67:         else
68:           redirect_to params[:referer]
69:         end
70:     else
71:       @client = Client.new(params[:client])
72:       if @client.save
73:         flash[:notice] = 'Client succesfully created.'
74:         redirect_to :action => :show, :id => @client.id
75:       else
76:         render :action => :new
77:       end
78:     end
79:   end

Delete Client

Cancels all bookings, past and present

Redirects to index after

[Source]

    # File app/controllers/client_controller.rb, line 31
31:   def destroy
32:     begin
33:       @client = Client.find(params[:id])
34:       
35:       @client.destroy
36:       flash[:notice] = 'Client succesfully deleted.'
37:     rescue
38:       flash[:notice] = 'Unable to delete: Client not found!'
39:     end
40:     
41:     if request.env['HTTP_REFERER'].match(/client\/[a-zA-Z]*\/[0-9]*$/)
42:       redirect_to :action => :index
43:     else
44:       redirect_to :back
45:     end
46:   end

Display edit options for Client

Sends form to the update method

[Source]

    # File app/controllers/client_controller.rb, line 51
51:   def edit
52:     begin
53:       @client = Client.find(params[:id])
54:     rescue
55:       flash[:notice] = "Client not found!"
56:       redirect_to :action => :index
57:     end
58:   end

Display a paginated list of clients, ordered alphabetically by last name

[Source]

    # File app/controllers/client_controller.rb, line 13
13:   def index
14:     pages, client_list = paginate(:clients, :order => :last_name)
15:     @clients = {:pages   => pages,
16:                 :clients => client_list}
17:   end

Displays form for creating a new Client

Sends form to the create method

[Source]

    # File app/controllers/client_controller.rb, line 22
22:   def new
23:     @client = Client.new
24:   end

Client report

Shows the details of the selected client and all their bookings

Can be selected by id or record_number or *full_name(record_number)*

[Source]

     # File app/controllers/client_controller.rb, line 118
118:   def show
119:     begin
120:       @client = if params[:id]
121:                   Client.find(params[:id])
122:                 elsif params[:record_number]
123:                   Client.find_by_record_number(params[:record_number])
124:                 else
125:                   Client.find_by_full_name(params[:client][:full_name])
126:                 end
127:       @time_format = "%B %d (%a)<br /> %I:%M%p"
128:       
129:       @coming_sessions = []
130:       @old_sessions = []
131:       bookings = Booking.find(:all, :conditions => "client_id = #{@client.id}")
132:       bookings.each do |booking|
133:         @coming_sessions << booking.clinical_session unless booking.clinical_session.start < Time.now
134:         @old_sessions << booking.clinical_session unless booking.clinical_session.start > Time.now      
135:       end
136:       
137:       @coming_sessions = @coming_sessions.sort {|a,b| a.start <=> b.start}
138:       @old_sessions = @old_sessions.sort {|a,b| b.start <=> a.start}
139:     rescue
140:       flash[:notice] = "Client not found!"
141:       redirect_to :action => :index
142:     end
143:   end

Update the selected client

Redirects to index after

[Source]

     # File app/controllers/client_controller.rb, line 84
 84:   def update
 85:     if params[:cancel]
 86:       flash[:notice] = 'Update cancelled'
 87:       if !params[:referer] || params[:referer].match(/edit/) || params[:referer].match(/update/)
 88:         redirect_to :action => :show, :id => params[:id]
 89:       else
 90:         redirect_to params[:referer]
 91:       end
 92:     else
 93:       begin
 94:         @client = Client.find(params[:id])
 95:         if @client.update_attributes(params[:client])
 96:           flash[:notice] = 'Client succesfully updated.'
 97:           if !params[:referer] || params[:referer].match(/edit/)
 98:             redirect_to :action => :show, :id => @client.id
 99:           else
100:             redirect_to params[:referer] 
101:           end
102:         else
103:           render :action => :edit
104:         end
105:       rescue
106:         flash[:notice] = "Unable to update: Client not found"
107:         redirect_to :action => :index
108:       end
109:     end
110:   end

[Validate]