Class CustomersController
In: app/controllers/customers_controller.rb
Parent: ApplicationController
File:customers_controller.rb
(C):Hipposoft 2008, 2009
Purpose:Manage Customer objects. See models/customer.rb for more.

          04-Jan-2008 (ADH): Created.

Methods

create   delete   delete_confirm   index   new   show   update  

Public Instance methods

Create a Customer (via ApplicationController.appctrl_create).

[Source]

     # File app/controllers/customers_controller.rb, line 117
117:   def create
118:     appctrl_create( 'Customer' )
119:   end

Customers should not normally be destroyed. Only administrators can do this. Works via ApplicationController.appctrl_delete.

[Source]

     # File app/controllers/customers_controller.rb, line 158
158:   def delete
159:     appctrl_delete( 'Customer' )
160:   end

Show an ‘Are you sure?’ prompt.

[Source]

     # File app/controllers/customers_controller.rb, line 164
164:   def delete_confirm
165:     return appctrl_not_permitted() unless ( @current_user.admin? )
166: 
167:     begin
168:       Customer.transaction do
169:         destroy_tasks    = ! params[ :destroy_tasks    ].nil?
170:         destroy_projects = ! params[ :destroy_projects ].nil?
171: 
172:         record = Customer.find_by_id( params[ :id ] )
173:         record.destroy_with_side_effects( destroy_projects, destroy_tasks )
174: 
175:         if ( destroy_projects )
176:           if ( destroy_tasks )
177:             message = 'Customer, customer\'s projects and associated tasks deleted'
178:           else
179:             message = 'Customer and customer\'s projects deleted; tasks left alone'
180:           end
181:         else
182:           message = 'Customer deleted; projects and tasks left alone'
183:         end
184: 
185:         flash[ :notice ] = message
186:         redirect_to( customers_path() )
187:       end
188: 
189:     rescue => error
190:       flash[ :error ] = "Could not destroy customer: #{ error }"
191:       redirect_to( home_path() )
192: 
193:     end
194:   end

List customers.

[Source]

    # File app/controllers/customers_controller.rb, line 23
23:   def index
24: 
25:     # Set up the column data; see the index helper functions in
26:     # application_helper.rb for details.
27: 
28:     @columns = [
29:       { :header_text  => 'Customer title', :value_method => :title,      :value_in_place => true                },
30:       { :header_text  => 'Customer code',  :value_method => :code,       :value_in_place => true                },
31:       { :header_text  => 'Created at',     :value_method => :created_at, :value_helper   => :apphelp_created_at },
32:     ]
33: 
34:     # Get the basic options hash from ApplicationController, then work out
35:     # the conditions on objects being fetched, including handling the search
36:     # form data.
37: 
38:     options        = appctrl_index_assist( Customer )
39:     active_vars    = { :active => true  }
40:     inactive_vars  = { :active => false }
41:     conditions_sql = "( customers.active = :active )\n"
42: 
43:     # The user may only be able to see projects associated with tasks matching
44:     # a specific list of IDs.
45: 
46:     restrictions_sql = ''
47: 
48:     if ( @current_user.restricted? )
49:       if ( @current_user.task_ids.empty? )
50:         restrictions_sql << 'WHERE ( customers.id = -1 )' # Never matches - forces no results
51:         conditions_sql    = 'AND ' << conditions_sql
52:       else
53:         restrictions_sql << "INNER JOIN projects ON ( projects.customer_id = customers.id )\n" <<
54:                             "INNER JOIN tasks    ON ( tasks.project_id = projects.id AND tasks.id IN (#{ @current_user.task_ids.join( ',' ) } ) )\n"
55:         conditions_sql    = 'WHERE ' << conditions_sql
56:       end
57:     else
58:       conditions_sql = 'WHERE ' << conditions_sql
59:     end
60: 
61:     # If asked to search for something, build extra conditions to do so.
62: 
63:     unless ( params[ :search ].nil? )
64:       if ( params[ :search ].empty? or params[ :search_cancel ] )
65:         params.delete( :search )
66:       else
67:         search = "%#{ params[ :search ] }%" # SQL wildcards either side of the search string
68:         conditions_sql << "AND ( customers.title ILIKE :search OR customers.code ILIKE :search )\n"
69:         vars = { :search => search }
70:         active_vars.merge!( vars )
71:         inactive_vars.merge!( vars )
72:       end
73:     end
74: 
75:     # Sort order is already partially compiled in 'options' from the earlier
76:     # call to 'ApplicationController.appctrl_index_assist'.
77: 
78:     order_sql = "ORDER BY #{ options[ :order ] }"
79:     options.delete( :order )
80: 
81:     # Compile the main SQL statement. Select all columns of the project, fetching
82:     # customers where the project's customer ID matches those customer IDs, with
83:     # only projects containing tasks in the user's permitted task list (if any)
84:     # are included, returned in the required order.
85: 
86:     finder_sql  = "SELECT DISTINCT customers.* FROM customers\n" <<
87:                   "#{ restrictions_sql }\n" <<
88:                   "#{ conditions_sql   }\n" <<
89:                   "#{ order_sql        }"
90: 
91:     # Now paginate using this SQL. The only difference between the active and
92:     # inactive cases is the value of the variables passed to Active Record for
93:     # substitution into the final SQL query going to the database.
94: 
95:     @active_customers   = Customer.paginate_by_sql( [ finder_sql, active_vars   ], options )
96:     @inactive_customers = Customer.paginate_by_sql( [ finder_sql, inactive_vars ], options )
97:   end

Show a ‘Create Customer’ view (via ApplicationController.appctrl_new).

[Source]

     # File app/controllers/customers_controller.rb, line 111
111:   def new
112:     appctrl_new( 'Customer' )
113:   end

Show the Customer (via ApplicationController.appctrl_show).

[Source]

     # File app/controllers/customers_controller.rb, line 105
105:   def show
106:     appctrl_show( 'Customer' )
107:   end

Update the customer details. We may need to update associated projects and tasks too, so the update is wrapped in a transaction to allow the database to roll back if anything goes wrong.

@record is set by the "can_be_modified?" before_filter method.

[Source]

     # File app/controllers/customers_controller.rb, line 127
127:   def update
128:     begin
129:       Customer.transaction do
130: 
131:         update_tasks    = ! params[ :update_tasks    ].nil?
132:         update_projects = ! params[ :update_projects ].nil?
133: 
134:         @record.update_with_side_effects!(
135:           params[ :customer ],
136:           update_projects,
137:           update_tasks
138:         )
139: 
140:         flash[ :notice ] = 'Customer details updated'
141:         redirect_to( customers_path() )
142:       end
143: 
144:     rescue ActiveRecord::StaleObjectError
145:       flash[ :error ] = 'The customer details were modified by someone else while you were making changes. Please examine the updated information before editing again.'
146:       redirect_to( customer_path( @record ) )
147: 
148:     rescue => error
149:       flash[ :error ] = "Could not update customer details: #{ error }"
150:       render( :action => 'edit' )
151: 
152:     end
153:   end

[Validate]