From 0dccca2d3c0e9d5e6eaa1c1ebee3670654fd8c43 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Wed, 21 Nov 2007 22:21:54 -0800 Subject: [PATCH] Add searching to the games controller This adds searching to the games controller, and adds an id and checkedout column to the list and search views. Signed-off-by: Ira W. Snyder --- app/controllers/game_controller.rb | 9 +++++++++ app/models/coitem.rb | 13 +++++++++++++ app/models/game.rb | 4 ++++ app/views/coitem/_form.rhtml | 8 ++++---- app/views/game/list.rhtml | 4 ++++ app/views/game/searchbyname.rhtml | 8 ++++++++ app/views/game/searchresults.rhtml | 28 ++++++++++++++++++++++++++++ db/development.sqlite3 | Bin 9216 -> 9216 bytes 8 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 app/views/game/searchbyname.rhtml create mode 100644 app/views/game/searchresults.rhtml diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 175a63f..39f5485 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -48,4 +48,13 @@ class GameController < ApplicationController Game.find(params[:id]).destroy redirect_to :action => 'list' end + + def searchbyname + render :action => 'searchbyname' + end + + def searchresults + query = params[:q] + @games = Game.find(:all, :conditions => ["title = ?", query]) + end end diff --git a/app/models/coitem.rb b/app/models/coitem.rb index a6bc1ad..8513465 100644 --- a/app/models/coitem.rb +++ b/app/models/coitem.rb @@ -2,11 +2,24 @@ class Coitem < ActiveRecord::Base belongs_to :customer belongs_to :rentable + # Make sure the user typed in the ids validates_presence_of :customer_id validates_presence_of :rentable_id + # Make sure the user typed in numbers + validates_numericality_of :customer_id + validates_numericality_of :rentable_id + + # Make sure this rentable was not already checked out validates_uniqueness_of :rentable_id + # Make sure the associated rentable and customer are valid and exist validates_associated :customer validates_associated :rentable + + protected + def validate + errors.add(:customer_id, "does not exist is the database") if customer.nil? + errors.add(:rentable_id, "does not exist in the database") if rentable.nil? + end end diff --git a/app/models/game.rb b/app/models/game.rb index 4991aa9..4c5d38c 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,4 +1,8 @@ class Game < Rentable validates_presence_of :game_genre validates_presence_of :platform + + def checkedout? + return Coitem.find_by_id(self.id) ? true : false + end end diff --git a/app/views/coitem/_form.rhtml b/app/views/coitem/_form.rhtml index 6cf12a5..2946273 100644 --- a/app/views/coitem/_form.rhtml +++ b/app/views/coitem/_form.rhtml @@ -1,11 +1,11 @@ <%= error_messages_for 'coitem' %> -


-<%= select 'coitem', 'customer_id', Customer.find(:all).collect {|c| [c.name.to_s, c.id] } %>

+


+<%= text_field 'coitem', 'customer_id' %>

-


-<%= select 'coitem', 'rentable_id', Rentable.find(:all).collect {|r| [r.title.to_s, r.id] } %>

+


+<%= text_field 'coitem', 'rentable_id' %>


<%= date_select 'coitem', 'out_date' %>

diff --git a/app/views/game/list.rhtml b/app/views/game/list.rhtml index f1317de..1a17dbf 100644 --- a/app/views/game/list.rhtml +++ b/app/views/game/list.rhtml @@ -2,6 +2,8 @@ + + <% for column in Game.content_columns %> <% end %> @@ -9,6 +11,8 @@ <% for game in @games %> + + <% for column in Game.content_columns %> <% end %> diff --git a/app/views/game/searchbyname.rhtml b/app/views/game/searchbyname.rhtml new file mode 100644 index 0000000..a5a4f8b --- /dev/null +++ b/app/views/game/searchbyname.rhtml @@ -0,0 +1,8 @@ +

Search for a Game by Name

+ +<%= start_form_tag :action => 'searchresults'%> +<%= text_field 'q', nil %> + <%= submit_tag 'Search' %> +<%= end_form_tag %> + +
diff --git a/app/views/game/searchresults.rhtml b/app/views/game/searchresults.rhtml new file mode 100644 index 0000000..54e7998 --- /dev/null +++ b/app/views/game/searchresults.rhtml @@ -0,0 +1,28 @@ +

Search Results

+ +<% if @games.empty? %> +

Sorry, there were no results

+<% else %> +
Game IDChecked Out<%= column.human_name %>
<%=h game.id %><%=h game.checkedout? %><%=h game.send(column.name) %>
+ + + + <% for column in Game.content_columns %> + + <% end %> + + +<% for game in @games %> + + + + <% for column in Game.content_columns %> + + <% end %> + + + + +<% end %> +
Game IDChecked Out<%= column.human_name %>
<%=h game.id %><%=h game.checkedout? %><%=h game.send(column.name) %><%= link_to 'Show', :action => 'show', :id => game %><%= link_to 'Edit', :action => 'edit', :id => game %><%= link_to 'Destroy', { :action => 'destroy', :id => game }, :confirm => 'Are you sure?', :post => true %>
+<% end %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 72eb9b0e1f0b41918244e9c5193e180349cf3ccd..58750cdfa908a401eb1f3b71854d0a81fea384c6 100644 GIT binary patch delta 174 zcmZqhXz-XI&A4i#%nNQVX69uK%#WClFfZFI$TEZ3L57))fss*Bk%`&Jz`$JB&`{UN z5W$jRg2*y5A;b_Y8AdjS$jSWSYEzUxbT+fdRs01aT(` Yuy8OjA7WsB%6tf@b{q5NL&A0f0I@p}`Tzg` -- 2.34.1