From: Ira W. Snyder Date: Thu, 22 Nov 2007 06:21:54 +0000 (-0800) Subject: Add searching to the games controller X-Git-Tag: turned-in~71 X-Git-Url: https://irasnyder.com/gitweb/?a=commitdiff_plain;ds=sidebyside;h=0dccca2d3c0e9d5e6eaa1c1ebee3670654fd8c43;p=cs356-p2-videostore.git 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 --- 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 72eb9b0..58750cd 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ