From e515ae6d491fa5c2fba0d412c5da7030b1417950 Mon Sep 17 00:00:00 2001 From: jimi Date: Wed, 18 Nov 2009 23:06:50 -0600 Subject: [PATCH] Generated layout and scaffold for photos --- app/controllers/photos_controller.rb | 44 ++++++++++++ app/helpers/layout_helper.rb | 22 ++++++ app/helpers/photos_helper.rb | 2 + app/models/photo.rb | 3 + app/views/layouts/application.html.erb | 22 ++++++ app/views/photos/_form.html.erb | 12 ++++ app/views/photos/edit.html.erb | 8 +++ app/views/photos/index.html.erb | 19 +++++ app/views/photos/new.html.erb | 5 ++ app/views/photos/show.html.erb | 16 +++++ config/routes.rb | 2 + db/migrate/20091119050145_create_photos.rb | 13 ++++ db/schema.rb | 21 ++++++ public/stylesheets/application.css | 81 ++++++++++++++++++++++ test/fixtures/photos.yml | 7 ++ test/functional/photos_controller_test.rb | 54 +++++++++++++++ test/unit/photo_test.rb | 7 ++ 17 files changed, 338 insertions(+) create mode 100644 app/controllers/photos_controller.rb create mode 100644 app/helpers/layout_helper.rb create mode 100644 app/helpers/photos_helper.rb create mode 100644 app/models/photo.rb create mode 100644 app/views/layouts/application.html.erb create mode 100644 app/views/photos/_form.html.erb create mode 100644 app/views/photos/edit.html.erb create mode 100644 app/views/photos/index.html.erb create mode 100644 app/views/photos/new.html.erb create mode 100644 app/views/photos/show.html.erb create mode 100644 db/migrate/20091119050145_create_photos.rb create mode 100644 db/schema.rb create mode 100644 public/stylesheets/application.css create mode 100644 test/fixtures/photos.yml create mode 100644 test/functional/photos_controller_test.rb create mode 100644 test/unit/photo_test.rb diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb new file mode 100644 index 0000000..9358c47 --- /dev/null +++ b/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 diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb new file mode 100644 index 0000000..5ee345e --- /dev/null +++ b/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 diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb new file mode 100644 index 0000000..0a10d47 --- /dev/null +++ b/app/helpers/photos_helper.rb @@ -0,0 +1,2 @@ +module PhotosHelper +end diff --git a/app/models/photo.rb b/app/models/photo.rb new file mode 100644 index 0000000..22dd3d6 --- /dev/null +++ b/app/models/photo.rb @@ -0,0 +1,3 @@ +class Photo < ActiveRecord::Base + attr_accessible :path, :url +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb new file mode 100644 index 0000000..d546973 --- /dev/null +++ b/app/views/layouts/application.html.erb @@ -0,0 +1,22 @@ + + + + <%= h(yield(:title) || "Untitled") %> + <%= stylesheet_link_tag 'application' %> + <%= yield(:head) %> + + +
+ <%- flash.each do |name, msg| -%> + <%= content_tag :div, msg, :id => "flash_#{name}" %> + <%- end -%> + + <%- if show_title? -%> +

<%=h yield(:title) %>

+ <%- end -%> + + <%= yield %> +
+ + diff --git a/app/views/photos/_form.html.erb b/app/views/photos/_form.html.erb new file mode 100644 index 0000000..65168d3 --- /dev/null +++ b/app/views/photos/_form.html.erb @@ -0,0 +1,12 @@ +<% form_for @photo do |f| %> + <%= f.error_messages %> +

+ <%= f.label :path %>
+ <%= f.text_field :path %> +

+

+ <%= f.label :url %>
+ <%= f.text_field :url %> +

+

<%= f.submit "Submit" %>

+<% end %> diff --git a/app/views/photos/edit.html.erb b/app/views/photos/edit.html.erb new file mode 100644 index 0000000..2a92917 --- /dev/null +++ b/app/views/photos/edit.html.erb @@ -0,0 +1,8 @@ +<% title "Edit Photo" %> + +<%= render :partial => 'form' %> + +

+ <%= link_to "Show", @photo %> | + <%= link_to "View All", photos_path %> +

diff --git a/app/views/photos/index.html.erb b/app/views/photos/index.html.erb new file mode 100644 index 0000000..8cc3b28 --- /dev/null +++ b/app/views/photos/index.html.erb @@ -0,0 +1,19 @@ +<% title "Photos" %> + + + + + + + <% for photo in @photos %> + + + + + + + + <% end %> +
PathUrl
<%=h photo.path %><%=h photo.url %><%= link_to "Show", photo %><%= link_to "Edit", edit_photo_path(photo) %><%= link_to "Destroy", photo, :confirm => 'Are you sure?', :method => :delete %>
+ +

<%= link_to "New Photo", new_photo_path %>

diff --git a/app/views/photos/new.html.erb b/app/views/photos/new.html.erb new file mode 100644 index 0000000..3fbdbe8 --- /dev/null +++ b/app/views/photos/new.html.erb @@ -0,0 +1,5 @@ +<% title "New Photo" %> + +<%= render :partial => 'form' %> + +

<%= link_to "Back to List", photos_path %>

diff --git a/app/views/photos/show.html.erb b/app/views/photos/show.html.erb new file mode 100644 index 0000000..20e38a0 --- /dev/null +++ b/app/views/photos/show.html.erb @@ -0,0 +1,16 @@ +<% title "Photo" %> + +

+ Path: + <%=h @photo.path %> +

+

+ Url: + <%=h @photo.url %> +

+ +

+ <%= link_to "Edit", edit_photo_path(@photo) %> | + <%= link_to "Destroy", @photo, :confirm => 'Are you sure?', :method => :delete %> | + <%= link_to "View All", photos_path %> +

diff --git a/config/routes.rb b/config/routes.rb index ea14ce1..bfac975 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw do |map| + map.resources :photos + # The priority is based upon order of creation: first created -> highest priority. # Sample of regular route: diff --git a/db/migrate/20091119050145_create_photos.rb b/db/migrate/20091119050145_create_photos.rb new file mode 100644 index 0000000..229c70e --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..e42876b --- /dev/null +++ b/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 diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css new file mode 100644 index 0000000..a462119 --- /dev/null +++ b/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; +} diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml new file mode 100644 index 0000000..0b9505b --- /dev/null +++ b/test/fixtures/photos.yml @@ -0,0 +1,7 @@ +one: + path: MyString + url: MyString + +two: + path: MyString + url: MyString diff --git a/test/functional/photos_controller_test.rb b/test/functional/photos_controller_test.rb new file mode 100644 index 0000000..3c41dce --- /dev/null +++ b/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 diff --git a/test/unit/photo_test.rb b/test/unit/photo_test.rb new file mode 100644 index 0000000..2c415c2 --- /dev/null +++ b/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