From 3650c5f411112965a12718b75ccbedde3186fa15 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Wed, 21 Nov 2007 20:18:18 -0800 Subject: [PATCH] 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 --- app/controllers/game_controller.rb | 5 --- app/controllers/video_controller.rb | 6 ---- app/models/game.rb | 7 ++-- app/models/rentable.rb | 4 +-- app/models/video.rb | 7 ++-- app/views/game/_form.rhtml | 5 ++- app/views/rentable/_form.rhtml | 2 +- app/views/rentable/list.rhtml | 12 ------- app/views/video/_form.rhtml | 9 +++-- db/development.sqlite3 | Bin 9216 -> 9216 bytes db/migrate/009_rentable_inheritance.rb | 46 +++++++++++++++++++++++++ db/schema.rb | 24 +++++-------- 12 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 db/migrate/009_rentable_inheritance.rb 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 c2859db854667304d3817fad232331698bb776a1..1c7c3258f3d8b1febf9ae970d5afd7461e968567 100644 GIT binary patch delta 512 zcmYjM&r1SP5Pq|7-CZTIh}st0eQGBoK_!a7OOVv1I)p-}*m4b_GHeL~1MSDTu|2{ z2WBX_LdjaQizWVLXr4_gS1{dZ9Vy)ge!&baij0m42roiGNC*#}^GO>+Ao`(=)ixn* zp)S;G)tBMwz9g%f28S8jT-~r-%AJmv_>wWj%WMS2KNfAItU)TZTK02wf^j^2%S0<`w#Rl zXcrG1MY`0ew|9s@*k=24;A?_jof*H{Z?QhW*#%e{J0*57z(}4lCng(6~V= z;ZXQSkcYA$$P~u}=Qw`w3nM&M1d#y5Vw7gnX+4$6q_UY6(iL2)fGh4#&H3yVfO9*| z&U(d!0uq6Z0BBG#Ox__ywPH5aBEKy2cH3~=6sH8=accXYFzKfvi2y)}*UE;vmm>rj zh(xogLZ{A~>WR^;nyx^H3J6XD!h}#A6EGPxM&LC?iAAH(OIi0p243rV`!u5U>Vm{V zA?P2pjk6lRs+PIgT27DkrS4n1!MIh5RV+QW8P<9QkHi>*zQ=W+8yCFg4$(T=d0hA5 iCqbMMoZ%aO*oVJhTm~e}4U{O7fa8|%7dUUXTlfJ_uw04& 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 -- 2.25.1
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) %>