From: Ira W. Snyder Date: Wed, 21 Nov 2007 16:09:08 +0000 (-0800) Subject: Add rentable model-view-controller X-Git-Tag: turned-in~86 X-Git-Url: https://www.irasnyder.com/gitweb/?a=commitdiff_plain;h=7ab58ec3c4bc5d29a77db40341840ad7eac97be0;p=cs356-p2-videostore.git Add rentable model-view-controller Signed-off-by: Ira W. Snyder --- diff --git a/app/controllers/rentable_controller.rb b/app/controllers/rentable_controller.rb new file mode 100644 index 0000000..f8c6f51 --- /dev/null +++ b/app/controllers/rentable_controller.rb @@ -0,0 +1,51 @@ +class RentableController < 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 + @rentable_pages, @rentables = paginate :rentables, :per_page => 10 + end + + def show + @rentable = Rentable.find(params[:id]) + end + + def new + @rentable = Rentable.new + end + + def create + @rentable = Rentable.new(params[:rentable]) + if @rentable.save + flash[:notice] = 'Rentable was successfully created.' + redirect_to :action => 'list' + else + render :action => 'new' + end + end + + def edit + @rentable = Rentable.find(params[:id]) + end + + def update + @rentable = Rentable.find(params[:id]) + if @rentable.update_attributes(params[:rentable]) + flash[:notice] = 'Rentable was successfully updated.' + redirect_to :action => 'show', :id => @rentable + else + render :action => 'edit' + end + end + + def destroy + Rentable.find(params[:id]).destroy + redirect_to :action => 'list' + end +end diff --git a/app/helpers/rentable_helper.rb b/app/helpers/rentable_helper.rb new file mode 100644 index 0000000..9a653f2 --- /dev/null +++ b/app/helpers/rentable_helper.rb @@ -0,0 +1,2 @@ +module RentableHelper +end diff --git a/app/models/rentable.rb b/app/models/rentable.rb new file mode 100644 index 0000000..d7ec5f9 --- /dev/null +++ b/app/models/rentable.rb @@ -0,0 +1,2 @@ +class Rentable < ActiveRecord::Base +end diff --git a/app/views/layouts/rentable.rhtml b/app/views/layouts/rentable.rhtml new file mode 100644 index 0000000..b019ebb --- /dev/null +++ b/app/views/layouts/rentable.rhtml @@ -0,0 +1,17 @@ + + + + + + Rentable: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/rentable/_form.rhtml b/app/views/rentable/_form.rhtml new file mode 100644 index 0000000..ecfbfae --- /dev/null +++ b/app/views/rentable/_form.rhtml @@ -0,0 +1,10 @@ +<%= error_messages_for 'rentable' %> + + +


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

+ +


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

+ + diff --git a/app/views/rentable/edit.rhtml b/app/views/rentable/edit.rhtml new file mode 100644 index 0000000..7c00c32 --- /dev/null +++ b/app/views/rentable/edit.rhtml @@ -0,0 +1,9 @@ +

Editing rentable

+ +<% form_tag :action => 'update', :id => @rentable do %> + <%= render :partial => 'form' %> + <%= submit_tag 'Edit' %> +<% end %> + +<%= link_to 'Show', :action => 'show', :id => @rentable %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/rentable/list.rhtml b/app/views/rentable/list.rhtml new file mode 100644 index 0000000..74c1cbc --- /dev/null +++ b/app/views/rentable/list.rhtml @@ -0,0 +1,27 @@ +

Listing rentables

+ + + + <% for column in Rentable.content_columns %> + + <% end %> + + +<% for rentable in @rentables %> + + <% for column in Rentable.content_columns %> + + <% end %> + + + + +<% end %> +
<%= column.human_name %>
<%=h rentable.send(column.name) %><%= link_to 'Show', :action => 'show', :id => rentable %><%= link_to 'Edit', :action => 'edit', :id => rentable %><%= link_to 'Destroy', { :action => 'destroy', :id => rentable }, :confirm => 'Are you sure?', :method => :post %>
+ +<%= link_to 'Previous page', { :page => @rentable_pages.current.previous } if @rentable_pages.current.previous %> +<%= link_to 'Next page', { :page => @rentable_pages.current.next } if @rentable_pages.current.next %> + +
+ +<%= link_to 'New rentable', :action => 'new' %> diff --git a/app/views/rentable/new.rhtml b/app/views/rentable/new.rhtml new file mode 100644 index 0000000..71b1788 --- /dev/null +++ b/app/views/rentable/new.rhtml @@ -0,0 +1,8 @@ +

New rentable

+ +<% form_tag :action => 'create' do %> + <%= render :partial => 'form' %> + <%= submit_tag "Create" %> +<% end %> + +<%= link_to 'Back', :action => 'list' %> diff --git a/app/views/rentable/show.rhtml b/app/views/rentable/show.rhtml new file mode 100644 index 0000000..49b75fd --- /dev/null +++ b/app/views/rentable/show.rhtml @@ -0,0 +1,8 @@ +<% for column in Rentable.content_columns %> +

+ <%= column.human_name %>: <%=h @rentable.send(column.name) %> +

+<% end %> + +<%= link_to 'Edit', :action => 'edit', :id => @rentable %> | +<%= link_to 'Back', :action => 'list' %> diff --git a/db/development.sqlite3 b/db/development.sqlite3 index cd6d367..cb4040e 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ diff --git a/db/migrate/002_create_rentables.rb b/db/migrate/002_create_rentables.rb new file mode 100644 index 0000000..65a05a5 --- /dev/null +++ b/db/migrate/002_create_rentables.rb @@ -0,0 +1,12 @@ +class CreateRentables < ActiveRecord::Migration + def self.up + create_table :rentables do |t| + t.column :title, :string + t.column :genre, :string + end + end + + def self.down + drop_table :rentables + end +end diff --git a/db/schema.rb b/db/schema.rb index f117726..dbb0282 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 => 1) do +ActiveRecord::Schema.define(:version => 2) do create_table "customers", :force => true do |t| t.column "name", :string @@ -12,4 +12,9 @@ ActiveRecord::Schema.define(:version => 1) do t.column "debt", :decimal, :precision => 8, :scale => 2, :default => 0.0 end + create_table "rentables", :force => true do |t| + t.column "title", :string + t.column "genre", :string + end + end diff --git a/test/fixtures/rentables.yml b/test/fixtures/rentables.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/rentables.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/rentable_controller_test.rb b/test/functional/rentable_controller_test.rb new file mode 100644 index 0000000..de3fe7b --- /dev/null +++ b/test/functional/rentable_controller_test.rb @@ -0,0 +1,92 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'rentable_controller' + +# Re-raise errors caught by the controller. +class RentableController; def rescue_action(e) raise e end; end + +class RentableControllerTest < Test::Unit::TestCase + fixtures :rentables + + def setup + @controller = RentableController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @first_id = rentables(: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(:rentables) + end + + def test_show + get :show, :id => @first_id + + assert_response :success + assert_template 'show' + + assert_not_nil assigns(:rentable) + assert assigns(:rentable).valid? + end + + def test_new + get :new + + assert_response :success + assert_template 'new' + + assert_not_nil assigns(:rentable) + end + + def test_create + num_rentables = Rentable.count + + post :create, :rentable => {} + + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_equal num_rentables + 1, Rentable.count + end + + def test_edit + get :edit, :id => @first_id + + assert_response :success + assert_template 'edit' + + assert_not_nil assigns(:rentable) + assert assigns(:rentable).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 { + Rentable.find(@first_id) + } + + post :destroy, :id => @first_id + assert_response :redirect + assert_redirected_to :action => 'list' + + assert_raise(ActiveRecord::RecordNotFound) { + Rentable.find(@first_id) + } + end +end diff --git a/test/unit/rentable_test.rb b/test/unit/rentable_test.rb new file mode 100644 index 0000000..39eb039 --- /dev/null +++ b/test/unit/rentable_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class RentableTest < Test::Unit::TestCase + fixtures :rentables + + # Replace this with your real tests. + def test_truth + assert true + end +end