Fix LIKE clauses in searches to match anywhere in the field
[cs356-p2-videostore.git] / app / controllers / customer_controller.rb
1 class CustomerController < ApplicationController
2   layout "admin"
3
4   # Make sure that the user has logged in before they can take any action
5   before_filter :authorize
6
7   def index
8     render :action => 'index'
9   end
10
11   # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
12   verify :method => :post, :only => [ :destroy, :create, :update ],
13          :redirect_to => { :action => :list }
14
15   def list
16     @customer_pages, @customers = paginate :customers, :per_page => 10
17   end
18
19   def show
20     @customer = Customer.find(params[:id])
21   end
22
23   def new
24     @customer = Customer.new
25   end
26
27   def create
28     @customer = Customer.new(params[:customer])
29     if @customer.save
30       flash[:notice] = 'Customer was successfully created.'
31       redirect_to :action => 'list'
32     else
33       render :action => 'new'
34     end
35   end
36
37   def edit
38     @customer = Customer.find(params[:id])
39   end
40
41   def update
42     @customer = Customer.find(params[:id])
43     if @customer.update_attributes(params[:customer])
44       flash[:notice] = 'Customer was successfully updated.'
45       redirect_to :action => 'show', :id => @customer
46     else
47       render :action => 'edit'
48     end
49   end
50
51   def destroy
52     Customer.find(params[:id]).destroy
53     redirect_to :action => 'list'
54   end
55
56   def search
57     if request.post?
58       @query = params[:q]
59       @customers = Customer.find(:all, :conditions => ["name like ?", "%#{@query[0]}%"])
60       render :action => 'searchresults'
61     else
62       render :action => 'search'
63     end
64   end
65 end