jimi
15 years ago
17 changed files with 338 additions and 0 deletions
-
44app/controllers/photos_controller.rb
-
22app/helpers/layout_helper.rb
-
2app/helpers/photos_helper.rb
-
3app/models/photo.rb
-
22app/views/layouts/application.html.erb
-
12app/views/photos/_form.html.erb
-
8app/views/photos/edit.html.erb
-
19app/views/photos/index.html.erb
-
5app/views/photos/new.html.erb
-
16app/views/photos/show.html.erb
-
2config/routes.rb
-
13db/migrate/20091119050145_create_photos.rb
-
21db/schema.rb
-
81public/stylesheets/application.css
-
7test/fixtures/photos.yml
-
54test/functional/photos_controller_test.rb
-
7test/unit/photo_test.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 |
@ -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 |
@ -0,0 +1,2 @@ |
|||
module PhotosHelper |
|||
end |
@ -0,0 +1,3 @@ |
|||
class Photo < ActiveRecord::Base |
|||
attr_accessible :path, :url |
|||
end |
@ -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> |
@ -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 %> |
@ -0,0 +1,8 @@ |
|||
<% title "Edit Photo" %> |
|||
|
|||
<%= render :partial => 'form' %> |
|||
|
|||
<p> |
|||
<%= link_to "Show", @photo %> | |
|||
<%= link_to "View All", photos_path %> |
|||
</p> |
@ -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> |
@ -0,0 +1,5 @@ |
|||
<% title "New Photo" %> |
|||
|
|||
<%= render :partial => 'form' %> |
|||
|
|||
<p><%= link_to "Back to List", photos_path %></p> |
@ -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> |
@ -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 |
@ -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 |
@ -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; |
|||
} |
@ -0,0 +1,7 @@ |
|||
one: |
|||
path: MyString |
|||
url: MyString |
|||
|
|||
two: |
|||
path: MyString |
|||
url: MyString |
@ -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 |
@ -0,0 +1,7 @@ |
|||
require 'test_helper' |
|||
|
|||
class PhotoTest < ActiveSupport::TestCase |
|||
def test_should_be_valid |
|||
assert Photo.new.valid? |
|||
end |
|||
end |
Reference in new issue