From 42bfb060c3d3d661486f94ce7a8f8c12cf452399 Mon Sep 17 00:00:00 2001
From: "Ira W. Snyder"
Date: Thu, 22 Nov 2007 00:09:05 -0800
Subject: [PATCH] Add media MVC
This adds the MVC for the video's media types
Signed-off-by: Ira W. Snyder
---
app/controllers/media_controller.rb | 51 +++++++++++++
app/helpers/media_helper.rb | 2 +
app/models/media.rb | 5 ++
app/models/video.rb | 2 +
app/views/layouts/media.rhtml | 17 +++++
app/views/media/_form.rhtml | 7 ++
app/views/media/edit.rhtml | 9 +++
app/views/media/list.rhtml | 27 +++++++
app/views/media/new.rhtml | 8 ++
app/views/media/show.rhtml | 8 ++
app/views/video/_form.rhtml | 2 +-
app/views/video/list.rhtml | 2 +-
db/development.sqlite3 | Bin 9216 -> 9216 bytes
db/migrate/013_create_medias.rb | 11 +++
db/schema.rb | 6 +-
test/fixtures/medias.yml | 5 ++
test/functional/media_controller_test.rb | 92 +++++++++++++++++++++++
test/unit/media_test.rb | 10 +++
18 files changed, 261 insertions(+), 3 deletions(-)
create mode 100644 app/controllers/media_controller.rb
create mode 100644 app/helpers/media_helper.rb
create mode 100644 app/models/media.rb
create mode 100644 app/views/layouts/media.rhtml
create mode 100644 app/views/media/_form.rhtml
create mode 100644 app/views/media/edit.rhtml
create mode 100644 app/views/media/list.rhtml
create mode 100644 app/views/media/new.rhtml
create mode 100644 app/views/media/show.rhtml
create mode 100644 db/migrate/013_create_medias.rb
create mode 100644 test/fixtures/medias.yml
create mode 100644 test/functional/media_controller_test.rb
create mode 100644 test/unit/media_test.rb
diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb
new file mode 100644
index 0000000..312a204
--- /dev/null
+++ b/app/controllers/media_controller.rb
@@ -0,0 +1,51 @@
+class MediaController < ApplicationController
+ def index
+ list
+ render :action => 'list'
+ end
+
+ # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
+ verify :method => :post, :only => [ :destroy, :create, :update ],
+ :redirect_to => { :action => :list }
+
+ def list
+ @media_pages, @medias = paginate :medias, :per_page => 10
+ end
+
+ def show
+ @media = Media.find(params[:id])
+ end
+
+ def new
+ @media = Media.new
+ end
+
+ def create
+ @media = Media.new(params[:media])
+ if @media.save
+ flash[:notice] = 'Media was successfully created.'
+ redirect_to :action => 'list'
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @media = Media.find(params[:id])
+ end
+
+ def update
+ @media = Media.find(params[:id])
+ if @media.update_attributes(params[:media])
+ flash[:notice] = 'Media was successfully updated.'
+ redirect_to :action => 'show', :id => @media
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ Media.find(params[:id]).destroy
+ redirect_to :action => 'list'
+ end
+end
diff --git a/app/helpers/media_helper.rb b/app/helpers/media_helper.rb
new file mode 100644
index 0000000..d08298b
--- /dev/null
+++ b/app/helpers/media_helper.rb
@@ -0,0 +1,2 @@
+module MediaHelper
+end
diff --git a/app/models/media.rb b/app/models/media.rb
new file mode 100644
index 0000000..fc20733
--- /dev/null
+++ b/app/models/media.rb
@@ -0,0 +1,5 @@
+class Media < ActiveRecord::Base
+ belongs_to :video
+
+ validates_presence_of :name
+end
diff --git a/app/models/video.rb b/app/models/video.rb
index abf0de3..411c978 100644
--- a/app/models/video.rb
+++ b/app/models/video.rb
@@ -1,5 +1,6 @@
class Video < Rentable
has_many :video_genres
+ has_many :medias
validates_presence_of :director
validates_presence_of :video_genre
@@ -8,5 +9,6 @@ class Video < Rentable
protected
def validate
errors.add(:video_genre, "does not exist in the database") if video_genre.nil?
+ errors.add(:media, "does not exist in the database") if media.nil?
end
end
diff --git a/app/views/layouts/media.rhtml b/app/views/layouts/media.rhtml
new file mode 100644
index 0000000..6fefa03
--- /dev/null
+++ b/app/views/layouts/media.rhtml
@@ -0,0 +1,17 @@
+
+
+
+
+
+ Media: <%= controller.action_name %>
+ <%= stylesheet_link_tag 'scaffold' %>
+
+
+
+<%= flash[:notice] %>
+
+<%= yield %>
+
+
+
diff --git a/app/views/media/_form.rhtml b/app/views/media/_form.rhtml
new file mode 100644
index 0000000..d29cadb
--- /dev/null
+++ b/app/views/media/_form.rhtml
@@ -0,0 +1,7 @@
+<%= error_messages_for 'media' %>
+
+
+
+<%= text_field 'media', 'name' %>
+
+
diff --git a/app/views/media/edit.rhtml b/app/views/media/edit.rhtml
new file mode 100644
index 0000000..6a99fbc
--- /dev/null
+++ b/app/views/media/edit.rhtml
@@ -0,0 +1,9 @@
+Editing media
+
+<% form_tag :action => 'update', :id => @media do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag 'Edit' %>
+<% end %>
+
+<%= link_to 'Show', :action => 'show', :id => @media %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/media/list.rhtml b/app/views/media/list.rhtml
new file mode 100644
index 0000000..020d74f
--- /dev/null
+++ b/app/views/media/list.rhtml
@@ -0,0 +1,27 @@
+Listing medias
+
+
+
+ <% for column in Media.content_columns %>
+ | <%= column.human_name %> |
+ <% end %>
+
+
+<% for media in @medias %>
+
+ <% for column in Media.content_columns %>
+ | <%=h media.send(column.name) %> |
+ <% end %>
+ <%= link_to 'Show', :action => 'show', :id => media %> |
+ <%= link_to 'Edit', :action => 'edit', :id => media %> |
+ <%= link_to 'Destroy', { :action => 'destroy', :id => media }, :confirm => 'Are you sure?', :method => :post %> |
+
+<% end %>
+
+
+<%= link_to 'Previous page', { :page => @media_pages.current.previous } if @media_pages.current.previous %>
+<%= link_to 'Next page', { :page => @media_pages.current.next } if @media_pages.current.next %>
+
+
+
+<%= link_to 'New media', :action => 'new' %>
diff --git a/app/views/media/new.rhtml b/app/views/media/new.rhtml
new file mode 100644
index 0000000..ee6bf50
--- /dev/null
+++ b/app/views/media/new.rhtml
@@ -0,0 +1,8 @@
+New media
+
+<% form_tag :action => 'create' do %>
+ <%= render :partial => 'form' %>
+ <%= submit_tag "Create" %>
+<% end %>
+
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/media/show.rhtml b/app/views/media/show.rhtml
new file mode 100644
index 0000000..695e6b0
--- /dev/null
+++ b/app/views/media/show.rhtml
@@ -0,0 +1,8 @@
+<% for column in Media.content_columns %>
+
+ <%= column.human_name %>: <%=h @media.send(column.name) %>
+
+<% end %>
+
+<%= link_to 'Edit', :action => 'edit', :id => @media %> |
+<%= link_to 'Back', :action => 'list' %>
diff --git a/app/views/video/_form.rhtml b/app/views/video/_form.rhtml
index c990254..51b9628 100644
--- a/app/views/video/_form.rhtml
+++ b/app/views/video/_form.rhtml
@@ -14,6 +14,6 @@
<%= text_field 'video', 'director' %>
-<%= text_field 'video', 'media' %>
+<%= select 'video', 'media', Media.find(:all).collect {|c| [c.name.to_s, c.id] } %>
diff --git a/app/views/video/list.rhtml b/app/views/video/list.rhtml
index 27bd318..d74ec25 100644
--- a/app/views/video/list.rhtml
+++ b/app/views/video/list.rhtml
@@ -19,7 +19,7 @@
<%=h video.newrelease %> |
<%=h Videogenre.find(video.video_genre).name %> |
<%=h video.director %> |
- <%=h video.media %> |
+ <%=h Media.find(video.media).name %> |
<%= link_to 'Show', :action => 'show', :id => video %> |
<%= link_to 'Edit', :action => 'edit', :id => video %> |
<%= link_to 'Destroy', { :action => 'destroy', :id => video }, :confirm => 'Are you sure?', :method => :post %> |
diff --git a/db/development.sqlite3 b/db/development.sqlite3
index 50680b2ab8df79b3503de38839d1ab7deddfe304..0e9ea78f87475648e7e39235f2c09fbcc7404d95 100644
GIT binary patch
delta 239
zcmZqhXz-XIEqImz6&P*Qxy;VUyNQE|L*x(xJJV?f=BG@bn3pkkF`Z_1V3yw4xPpn7
z`4EGNxFSEJJR<``*yMdeYK)ANp9*OJNoipfMyAOQ!k&zbo4bU|SonB>#xpZNVqpHk
z{DJw=WYtKCCp`$3=0bvGvgu#CLyMY
zjEgolb}(AjD=RZLI+i3R<)oJ7aj=U^N-{Q@gSok>DVd4I5R%Fs$$7Jo(mO@~d|W$!
delta 153
zcmZqhXz-XIEx4Zn0@#5RBM|Fv)Va*g$g_!qi9>{sft_g|1M^d+r_9TkyO{PdJ1|Rc
zY+S)K`LvK4Bg5oRLK=Jw%!e3w#TEG(`GK0lCQA#e03{rRJsBA`cL|rVNI*>oQJg@`
l2E?o&yjhUt1@k5u78VXBCLsnUA*P8N8`+pP3n{&01OVx^90>pb
diff --git a/db/migrate/013_create_medias.rb b/db/migrate/013_create_medias.rb
new file mode 100644
index 0000000..0f7ff17
--- /dev/null
+++ b/db/migrate/013_create_medias.rb
@@ -0,0 +1,11 @@
+class CreateMedias < ActiveRecord::Migration
+ def self.up
+ create_table :medias do |t|
+ t.column :name, :string, :null => false
+ end
+ end
+
+ def self.down
+ drop_table :medias
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index f0435a6..77281f8 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 => 12) do
+ActiveRecord::Schema.define(:version => 13) do
create_table "coitems", :force => true do |t|
t.column "customer_id", :integer
@@ -19,6 +19,10 @@ ActiveRecord::Schema.define(:version => 12) do
t.column "debt", :decimal, :default => 0.0
end
+ create_table "medias", :force => true do |t|
+ t.column "name", :string, :null => false
+ end
+
create_table "rentables", :force => true do |t|
t.column "type", :string
t.column "title", :string
diff --git a/test/fixtures/medias.yml b/test/fixtures/medias.yml
new file mode 100644
index 0000000..b49c4eb
--- /dev/null
+++ b/test/fixtures/medias.yml
@@ -0,0 +1,5 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+two:
+ id: 2
diff --git a/test/functional/media_controller_test.rb b/test/functional/media_controller_test.rb
new file mode 100644
index 0000000..b2824ad
--- /dev/null
+++ b/test/functional/media_controller_test.rb
@@ -0,0 +1,92 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'media_controller'
+
+# Re-raise errors caught by the controller.
+class MediaController; def rescue_action(e) raise e end; end
+
+class MediaControllerTest < Test::Unit::TestCase
+ fixtures :medias
+
+ def setup
+ @controller = MediaController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @first_id = medias(:first).id
+ end
+
+ def test_index
+ get :index
+ assert_response :success
+ assert_template 'list'
+ end
+
+ def test_list
+ get :list
+
+ assert_response :success
+ assert_template 'list'
+
+ assert_not_nil assigns(:medias)
+ end
+
+ def test_show
+ get :show, :id => @first_id
+
+ assert_response :success
+ assert_template 'show'
+
+ assert_not_nil assigns(:media)
+ assert assigns(:media).valid?
+ end
+
+ def test_new
+ get :new
+
+ assert_response :success
+ assert_template 'new'
+
+ assert_not_nil assigns(:media)
+ end
+
+ def test_create
+ num_medias = Media.count
+
+ post :create, :media => {}
+
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_equal num_medias + 1, Media.count
+ end
+
+ def test_edit
+ get :edit, :id => @first_id
+
+ assert_response :success
+ assert_template 'edit'
+
+ assert_not_nil assigns(:media)
+ assert assigns(:media).valid?
+ end
+
+ def test_update
+ post :update, :id => @first_id
+ assert_response :redirect
+ assert_redirected_to :action => 'show', :id => @first_id
+ end
+
+ def test_destroy
+ assert_nothing_raised {
+ Media.find(@first_id)
+ }
+
+ post :destroy, :id => @first_id
+ assert_response :redirect
+ assert_redirected_to :action => 'list'
+
+ assert_raise(ActiveRecord::RecordNotFound) {
+ Media.find(@first_id)
+ }
+ end
+end
diff --git a/test/unit/media_test.rb b/test/unit/media_test.rb
new file mode 100644
index 0000000..3883727
--- /dev/null
+++ b/test/unit/media_test.rb
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class MediaTest < Test::Unit::TestCase
+ fixtures :medias
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end
--
2.34.1