From: Ira W. Snyder Date: Thu, 22 Nov 2007 04:18:18 +0000 (-0800) Subject: Switch to a single joined table for Videos and Games X-Git-Tag: turned-in~75 X-Git-Url: https://www.irasnyder.com/gitweb/?a=commitdiff_plain;h=3650c5f411112965a12718b75ccbedde3186fa15;p=cs356-p2-videostore.git Switch to a single joined table for Videos and Games This is much simpler than the multiple inheritance thing, as well as making things like getting all of the types and titles much easier. Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 1b57435..175a63f 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -21,12 +21,7 @@ class GameController < ApplicationController end def create - # A new rentable must be created whenever we create a new game - @rentable = Rentable.new - @rentable.rtype = 'game' - @rentable.save @game = Game.new(params[:game]) - @game.rentable_id = @rentable.id if @game.save flash[:notice] = 'Game was successfully created.' redirect_to :action => 'list' diff --git a/app/controllers/video_controller.rb b/app/controllers/video_controller.rb index 4221c3c..a5508ae 100644 --- a/app/controllers/video_controller.rb +++ b/app/controllers/video_controller.rb @@ -21,13 +21,7 @@ class VideoController < ApplicationController end def create - # A new rentable must be created and saved whenever we create a new - # video. This is so we have a rentable_id to add to the video. - @rentable = Rentable.new - @rentable.rtype = 'video' - @rentable.save! @video = Video.new(params[:video]) - @video.rentable_id = @rentable.id if @video.save flash[:notice] = 'Video was successfully created.' redirect_to :action => 'list' diff --git a/app/models/game.rb b/app/models/game.rb index 9880b04..4991aa9 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,7 +1,4 @@ -class Game < ActiveRecord::Base - has_one :rentable - - validates_presence_of :title - validates_presence_of :genre +class Game < Rentable + validates_presence_of :game_genre validates_presence_of :platform end diff --git a/app/models/rentable.rb b/app/models/rentable.rb index 0699a20..cccbb57 100644 --- a/app/models/rentable.rb +++ b/app/models/rentable.rb @@ -1,6 +1,6 @@ class Rentable < ActiveRecord::Base has_many :coitem - belongs_to :video - belongs_to :game + validates_presence_of :title + # don't validate newrelease, false is ok end diff --git a/app/models/video.rb b/app/models/video.rb index 2ab598d..50733d5 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,6 +1,5 @@ -class Video < ActiveRecord::Base - has_one :rentable - +class Video < Rentable validates_presence_of :director - validates_presence_of :genre + validates_presence_of :video_genre + validates_presence_of :media end diff --git a/app/views/game/_form.rhtml b/app/views/game/_form.rhtml index a0ab990..b264601 100644 --- a/app/views/game/_form.rhtml +++ b/app/views/game/_form.rhtml @@ -4,10 +4,13 @@


<%= text_field 'game', 'title' %>

+


+<%= check_box 'game', 'newrelease' %>

+


<%= text_field 'game', 'platform' %>


-<%= text_field 'game', 'genre' %>

+<%= text_field 'game', 'game_genre' %>

diff --git a/app/views/rentable/_form.rhtml b/app/views/rentable/_form.rhtml index a5b3ba2..475b7a1 100644 --- a/app/views/rentable/_form.rhtml +++ b/app/views/rentable/_form.rhtml @@ -2,6 +2,6 @@


-<%= text_field 'rentable', 'rtype' %>

+<%= text_field 'rentable', 'type' %>

diff --git a/app/views/rentable/list.rhtml b/app/views/rentable/list.rhtml index 19f6f75..39883c5 100644 --- a/app/views/rentable/list.rhtml +++ b/app/views/rentable/list.rhtml @@ -2,8 +2,6 @@ - - <% for column in Rentable.content_columns %> <% end %> @@ -11,16 +9,6 @@ <% for rentable in @rentables %> - - <% for column in Rentable.content_columns %> <% end %> diff --git a/app/views/video/_form.rhtml b/app/views/video/_form.rhtml index 8e903a2..10f345a 100644 --- a/app/views/video/_form.rhtml +++ b/app/views/video/_form.rhtml @@ -5,12 +5,15 @@ <%= text_field 'video', 'title' %>


-

+<%= check_box 'video', 'newrelease' %>

+ +


+<%= text_field 'video', 'video_genre' %>


<%= text_field 'video', 'director' %>

-


-<%= text_field 'video', 'genre' %>

+


+<%= text_field 'video', 'media' %>

diff --git a/db/development.sqlite3 b/db/development.sqlite3 index c2859db..1c7c325 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ diff --git a/db/migrate/009_rentable_inheritance.rb b/db/migrate/009_rentable_inheritance.rb new file mode 100644 index 0000000..d0c2593 --- /dev/null +++ b/db/migrate/009_rentable_inheritance.rb @@ -0,0 +1,46 @@ +class RentableInheritance < ActiveRecord::Migration + def self.up + drop_table :games + drop_table :videos + drop_table :rentables + create_table :rentables do |t| + # for the inheritance + t.column :type, :string + + # common columns + t.column :title, :string + t.column :newrelease, :boolean, :default => false + + # video specific + t.column :video_genre, :integer + t.column :director, :integer + t.column :media, :integer + + # game specific + t.column :game_genre, :integer + t.column :platform, :integer + end + end + + def self.down + create_table :games do |t| + t.column :title, :string + t.column :platform, :integer + t.column :genre, :integer + t.column :rentable_id, :integer + end + + create_table :videos do |t| + t.column :title, :string + t.column :newrelease, :boolean, :default => false + t.column :director, :string + t.column :genre, :integer + t.column :rentable_id, :integer + end + + drop_table :rentables + create_table :rentables do |t| + t.column :rtype, :string + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 885c6ee..692ee89 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,7 +2,7 @@ # migrations feature of ActiveRecord to incrementally modify your database, and # then regenerate this schema definition. -ActiveRecord::Schema.define(:version => 8) do +ActiveRecord::Schema.define(:version => 9) do create_table "coitems", :force => true do |t| t.column "customer_id", :integer @@ -19,23 +19,15 @@ ActiveRecord::Schema.define(:version => 8) do t.column "debt", :decimal, :precision => 8, :scale => 2, :default => 0.0 end - create_table "games", :force => true do |t| - t.column "title", :string - t.column "platform", :integer - t.column "genre", :integer - t.column "rentable_id", :integer - end - create_table "rentables", :force => true do |t| - t.column "rtype", :string - end - - create_table "videos", :force => true do |t| - t.column "rentable_id", :integer - t.column "newrelease", :boolean, :default => false - t.column "director", :string - t.column "genre", :integer + t.column "type", :string t.column "title", :string + t.column "newrelease", :boolean, :default => false + t.column "video_genre", :integer + t.column "director", :integer + t.column "media", :integer + t.column "game_genre", :integer + t.column "platform", :integer end end
IDTitle<%= column.human_name %>
<%=h rentable.id %> - <% if Game.find(:first, :conditions => "rentable_id='#{rentable.id}'") %> - Game - <% elsif Video.find(:first, :conditions => "rentable_id='#{rentable.id}'") %> - Video - <% else %> - ERROR - <% end %> - <%=h rentable.send(column.name) %>