Browse Source

Generated layout and scaffold for photos

master
jimi 15 years ago
parent
commit
e515ae6d49
  1. 44
      app/controllers/photos_controller.rb
  2. 22
      app/helpers/layout_helper.rb
  3. 2
      app/helpers/photos_helper.rb
  4. 3
      app/models/photo.rb
  5. 22
      app/views/layouts/application.html.erb
  6. 12
      app/views/photos/_form.html.erb
  7. 8
      app/views/photos/edit.html.erb
  8. 19
      app/views/photos/index.html.erb
  9. 5
      app/views/photos/new.html.erb
  10. 16
      app/views/photos/show.html.erb
  11. 2
      config/routes.rb
  12. 13
      db/migrate/20091119050145_create_photos.rb
  13. 21
      db/schema.rb
  14. 81
      public/stylesheets/application.css
  15. 7
      test/fixtures/photos.yml
  16. 54
      test/functional/photos_controller_test.rb
  17. 7
      test/unit/photo_test.rb

44
app/controllers/photos_controller.rb

@ -0,0 +1,44 @@
class PhotosController < ApplicationController
def index
@photos = Photo.all
end
def show
@photo = Photo.find(params[:id])
end
def new
@photo = Photo.new
end
def create
@photo = Photo.new(params[:photo])
if @photo.save
flash[:notice] = "Successfully created photo."
redirect_to @photo
else
render :action => 'new'
end
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(params[:photo])
flash[:notice] = "Successfully updated photo."
redirect_to @photo
else
render :action => 'edit'
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
flash[:notice] = "Successfully destroyed photo."
redirect_to photos_url
end
end

22
app/helpers/layout_helper.rb

@ -0,0 +1,22 @@
# These helper methods can be called in your template to set variables to be used in the layout
# This module should be included in all views globally,
# to do so you may need to add this line to your ApplicationController
# helper :layout
module LayoutHelper
def title(page_title, show_title = true)
@content_for_title = page_title.to_s
@show_title = show_title
end
def show_title?
@show_title
end
def stylesheet(*args)
content_for(:head) { stylesheet_link_tag(*args) }
end
def javascript(*args)
content_for(:head) { javascript_include_tag(*args) }
end
end

2
app/helpers/photos_helper.rb

@ -0,0 +1,2 @@
module PhotosHelper
end

3
app/models/photo.rb

@ -0,0 +1,3 @@
class Photo < ActiveRecord::Base
attr_accessible :path, :url
end

22
app/views/layouts/application.html.erb

@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><%= h(yield(:title) || "Untitled") %></title>
<%= stylesheet_link_tag 'application' %>
<%= yield(:head) %>
</head>
<body>
<div id="container">
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<%- end -%>
<%- if show_title? -%>
<h1><%=h yield(:title) %></h1>
<%- end -%>
<%= yield %>
</div>
</body>
</html>

12
app/views/photos/_form.html.erb

@ -0,0 +1,12 @@
<% form_for @photo do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :path %><br />
<%= f.text_field :path %>
</p>
<p>
<%= f.label :url %><br />
<%= f.text_field :url %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>

8
app/views/photos/edit.html.erb

@ -0,0 +1,8 @@
<% title "Edit Photo" %>
<%= render :partial => 'form' %>
<p>
<%= link_to "Show", @photo %> |
<%= link_to "View All", photos_path %>
</p>

19
app/views/photos/index.html.erb

@ -0,0 +1,19 @@
<% title "Photos" %>
<table>
<tr>
<th>Path</th>
<th>Url</th>
</tr>
<% for photo in @photos %>
<tr>
<td><%=h photo.path %></td>
<td><%=h photo.url %></td>
<td><%= link_to "Show", photo %></td>
<td><%= link_to "Edit", edit_photo_path(photo) %></td>
<td><%= link_to "Destroy", photo, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<p><%= link_to "New Photo", new_photo_path %></p>

5
app/views/photos/new.html.erb

@ -0,0 +1,5 @@
<% title "New Photo" %>
<%= render :partial => 'form' %>
<p><%= link_to "Back to List", photos_path %></p>

16
app/views/photos/show.html.erb

@ -0,0 +1,16 @@
<% title "Photo" %>
<p>
<strong>Path:</strong>
<%=h @photo.path %>
</p>
<p>
<strong>Url:</strong>
<%=h @photo.url %>
</p>
<p>
<%= link_to "Edit", edit_photo_path(@photo) %> |
<%= link_to "Destroy", @photo, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", photos_path %>
</p>

2
config/routes.rb

@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :photos
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route: # Sample of regular route:

13
db/migrate/20091119050145_create_photos.rb

@ -0,0 +1,13 @@
class CreatePhotos < ActiveRecord::Migration
def self.up
create_table :photos do |t|
t.string :path
t.string :url
t.timestamps
end
end
def self.down
drop_table :photos
end
end

21
db/schema.rb

@ -0,0 +1,21 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20091119050145) do
create_table "photos", :force => true do |t|
t.string "path"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end
end

81
public/stylesheets/application.css

@ -0,0 +1,81 @@
body {
background-color: #4B7399;
font-family: Verdana, Helvetica, Arial;
font-size: 14px;
}
a img {
border: none;
}
a {
color: #0000FF;
}
.clear {
clear: both;
height: 0;
overflow: hidden;
}
#container {
width: 75%;
margin: 0 auto;
background-color: #FFF;
padding: 20px 40px;
border: solid 1px black;
margin-top: 20px;
}
#flash_notice, #flash_error {
padding: 5px 8px;
margin: 10px 0;
}
#flash_notice {
background-color: #CFC;
border: solid 1px #6C6;
}
#flash_error {
background-color: #FCC;
border: solid 1px #C66;
}
.fieldWithErrors {
display: inline;
}
#errorExplanation {
width: 400px;
border: 2px solid #CF0000;
padding: 0px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}
#errorExplanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: 0;
background-color: #c00;
color: #fff;
}
#errorExplanation p {
color: #333;
margin-bottom: 0;
padding: 8px;
}
#errorExplanation ul {
margin: 2px 24px;
}
#errorExplanation ul li {
font-size: 12px;
list-style: disc;
}

7
test/fixtures/photos.yml

@ -0,0 +1,7 @@
one:
path: MyString
url: MyString
two:
path: MyString
url: MyString

54
test/functional/photos_controller_test.rb

@ -0,0 +1,54 @@
require 'test_helper'
class PhotosControllerTest < ActionController::TestCase
def test_index
get :index
assert_template 'index'
end
def test_show
get :show, :id => Photo.first
assert_template 'show'
end
def test_new
get :new
assert_template 'new'
end
def test_create_invalid
Photo.any_instance.stubs(:valid?).returns(false)
post :create
assert_template 'new'
end
def test_create_valid
Photo.any_instance.stubs(:valid?).returns(true)
post :create
assert_redirected_to photo_url(assigns(:photo))
end
def test_edit
get :edit, :id => Photo.first
assert_template 'edit'
end
def test_update_invalid
Photo.any_instance.stubs(:valid?).returns(false)
put :update, :id => Photo.first
assert_template 'edit'
end
def test_update_valid
Photo.any_instance.stubs(:valid?).returns(true)
put :update, :id => Photo.first
assert_redirected_to photo_url(assigns(:photo))
end
def test_destroy
photo = Photo.first
delete :destroy, :id => photo
assert_redirected_to photos_url
assert !Photo.exists?(photo.id)
end
end

7
test/unit/photo_test.rb

@ -0,0 +1,7 @@
require 'test_helper'
class PhotoTest < ActiveSupport::TestCase
def test_should_be_valid
assert Photo.new.valid?
end
end