Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added rails Todo scaffold

  • Loading branch information...
commit 06f1b613df17947d711b21462291a429cdf2d02d 1 parent 8aa5044
@froots authored
View
87 app/controllers/todos_controller.rb
@@ -0,0 +1,87 @@
+class TodosController < ApplicationController
+ # GET /todos
+ # GET /todos.xml
+ def index
+ @todos = Todo.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render :json => @todos }
+ format.xml { render :xml => @todos }
+ end
+ end
+
+ # GET /todos/1
+ # GET /todos/1.xml
+ def show
+ @todo = Todo.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render :json => @todo }
+ format.xml { render :xml => @todo }
+ end
+ end
+
+ # GET /todos/new
+ # GET /todos/new.xml
+ def new
+ @todo = Todo.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @todo }
+ end
+ end
+
+ # GET /todos/1/edit
+ def edit
+ @todo = Todo.find(params[:id])
+ end
+
+ # POST /todos
+ # POST /todos.xml
+ def create
+ @todo = Todo.new(params[:todo])
+
+ respond_to do |format|
+ if @todo.save
+ format.html { redirect_to(@todo, :notice => 'Todo was successfully created.') }
+ format.json { render :json => @todo, :status => :created, :location => @todo }
+ format.xml { render :xml => @todo, :status => :created, :location => @todo }
+ else
+ format.html { render :action => "new" }
+ format.json { render :json => @todo.errors, :status => :unprocessable_entity }
+ format.xml { render :xml => @todo.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /todos/1
+ # PUT /todos/1.xml
+ def update
+ @todo = Todo.find(params[:id])
+
+ respond_to do |format|
+ if @todo.update_attributes(params[:todo])
+ format.html { redirect_to(@todo, :notice => 'Todo was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @todo.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /todos/1
+ # DELETE /todos/1.xml
+ def destroy
+ @todo = Todo.find(params[:id])
+ @todo.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(todos_url) }
+ format.xml { head :ok }
+ end
+ end
+end
View
2  app/helpers/todos_helper.rb
@@ -0,0 +1,2 @@
+module TodosHelper
+end
View
2  app/models/todo.rb
@@ -0,0 +1,2 @@
+class Todo < ActiveRecord::Base
+end
View
33 app/views/todos/_form.html.erb
@@ -0,0 +1,33 @@
+<%= form_for(@todo) do |f| %>
+ <% if @todo.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@todo.errors.count, "error") %> prohibited this todo from being saved:</h2>
+
+ <ul>
+ <% @todo.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </div>
+ <div class="field">
+ <%= f.label :description %><br />
+ <%= f.text_area :description %>
+ </div>
+ <div class="field">
+ <%= f.label :priority %><br />
+ <%= f.text_field :priority %>
+ </div>
+ <div class="field">
+ <%= f.label :position %><br />
+ <%= f.text_field :position %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
6 app/views/todos/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing todo</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @todo %> |
+<%= link_to 'Back', todos_path %>
View
29 app/views/todos/index.html.erb
@@ -0,0 +1,29 @@
+<h1>Listing todos</h1>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Description</th>
+ <th>Priority</th>
+ <th>Position</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @todos.each do |todo| %>
+ <tr>
+ <td><%= todo.title %></td>
+ <td><%= todo.description %></td>
+ <td><%= todo.priority %></td>
+ <td><%= todo.position %></td>
+ <td><%= link_to 'Show', todo %></td>
+ <td><%= link_to 'Edit', edit_todo_path(todo) %></td>
+ <td><%= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Todo', new_todo_path %>
View
5 app/views/todos/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New todo</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', todos_path %>
View
25 app/views/todos/show.html.erb
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Title:</b>
+ <%= @todo.title %>
+</p>
+
+<p>
+ <b>Description:</b>
+ <%= @todo.description %>
+</p>
+
+<p>
+ <b>Priority:</b>
+ <%= @todo.priority %>
+</p>
+
+<p>
+ <b>Position:</b>
+ <%= @todo.position %>
+</p>
+
+
+<%= link_to 'Edit', edit_todo_path(@todo) %> |
+<%= link_to 'Back', todos_path %>
View
2  config/application.rb
@@ -38,5 +38,7 @@ class Application < Rails::Application
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
+
+ ActiveRecord::Base.include_root_in_json = false
end
end
View
2  config/routes.rb
@@ -1,4 +1,6 @@
BackboneJasmineExamples::Application.routes.draw do
+ resources :todos
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
16 db/migrate/20110315073827_create_todos.rb
@@ -0,0 +1,16 @@
+class CreateTodos < ActiveRecord::Migration
+ def self.up
+ create_table :todos do |t|
+ t.string :title
+ t.text :description
+ t.integer :priority
+ t.integer :position
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :todos
+ end
+end
View
24 db/schema.rb
@@ -0,0 +1,24 @@
+# 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 => 20110315073827) do
+
+ create_table "todos", :force => true do |t|
+ t.string "title"
+ t.text "description"
+ t.integer "priority"
+ t.integer "position"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
244 public/index.html
@@ -1,239 +1,15 @@
-<!DOCTYPE html>
+<!doctype html>
<html>
<head>
- <title>Ruby on Rails: Welcome aboard</title>
- <style type="text/css" media="screen">
- body {
- margin: 0;
- margin-bottom: 25px;
- padding: 0;
- background-color: #f0f0f0;
- font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana";
- font-size: 13px;
- color: #333;
- }
-
- h1 {
- font-size: 28px;
- color: #000;
- }
-
- a {color: #03c}
- a:hover {
- background-color: #03c;
- color: white;
- text-decoration: none;
- }
-
-
- #page {
- background-color: #f0f0f0;
- width: 750px;
- margin: 0;
- margin-left: auto;
- margin-right: auto;
- }
-
- #content {
- float: left;
- background-color: white;
- border: 3px solid #aaa;
- border-top: none;
- padding: 25px;
- width: 500px;
- }
-
- #sidebar {
- float: right;
- width: 175px;
- }
-
- #footer {
- clear: both;
- }
-
-
- #header, #about, #getting-started {
- padding-left: 75px;
- padding-right: 30px;
- }
-
-
- #header {
- background-image: url("images/rails.png");
- background-repeat: no-repeat;
- background-position: top left;
- height: 64px;
- }
- #header h1, #header h2 {margin: 0}
- #header h2 {
- color: #888;
- font-weight: normal;
- font-size: 16px;
- }
-
-
- #about h3 {
- margin: 0;
- margin-bottom: 10px;
- font-size: 14px;
- }
-
- #about-content {
- background-color: #ffd;
- border: 1px solid #fc0;
- margin-left: -55px;
- margin-right: -10px;
- }
- #about-content table {
- margin-top: 10px;
- margin-bottom: 10px;
- font-size: 11px;
- border-collapse: collapse;
- }
- #about-content td {
- padding: 10px;
- padding-top: 3px;
- padding-bottom: 3px;
- }
- #about-content td.name {color: #555}
- #about-content td.value {color: #000}
-
- #about-content ul {
- padding: 0;
- list-style-type: none;
- }
-
- #about-content.failure {
- background-color: #fcc;
- border: 1px solid #f00;
- }
- #about-content.failure p {
- margin: 0;
- padding: 10px;
- }
-
-
- #getting-started {
- border-top: 1px solid #ccc;
- margin-top: 25px;
- padding-top: 15px;
- }
- #getting-started h1 {
- margin: 0;
- font-size: 20px;
- }
- #getting-started h2 {
- margin: 0;
- font-size: 14px;
- font-weight: normal;
- color: #333;
- margin-bottom: 25px;
- }
- #getting-started ol {
- margin-left: 0;
- padding-left: 0;
- }
- #getting-started li {
- font-size: 18px;
- color: #888;
- margin-bottom: 25px;
- }
- #getting-started li h2 {
- margin: 0;
- font-weight: normal;
- font-size: 18px;
- color: #333;
- }
- #getting-started li p {
- color: #555;
- font-size: 13px;
- }
-
-
- #sidebar ul {
- margin-left: 0;
- padding-left: 0;
- }
- #sidebar ul h3 {
- margin-top: 25px;
- font-size: 16px;
- padding-bottom: 10px;
- border-bottom: 1px solid #ccc;
- }
- #sidebar li {
- list-style-type: none;
- }
- #sidebar ul.links li {
- margin-bottom: 5px;
- }
-
- </style>
- <script type="text/javascript">
- function about() {
- info = document.getElementById('about-content');
- if (window.XMLHttpRequest)
- { xhr = new XMLHttpRequest(); }
- else
- { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
- xhr.open("GET","rails/info/properties",false);
- xhr.send("");
- info.innerHTML = xhr.responseText;
- info.style.display = 'block'
- }
- </script>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>ToDoobly</title>
</head>
<body>
- <div id="page">
- <div id="sidebar">
- <ul id="sidebar-items">
- <li>
- <h3>Browse the documentation</h3>
- <ul class="links">
- <li><a href="http://api.rubyonrails.org/">Rails API</a></li>
- <li><a href="http://stdlib.rubyonrails.org/">Ruby standard library</a></li>
- <li><a href="http://corelib.rubyonrails.org/">Ruby core</a></li>
- <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>
- </ul>
- </li>
- </ul>
- </div>
-
- <div id="content">
- <div id="header">
- <h1>Welcome aboard</h1>
- <h2>You&rsquo;re riding Ruby on Rails!</h2>
- </div>
-
- <div id="about">
- <h3><a href="rails/info/properties" onclick="about(); return false">About your application&rsquo;s environment</a></h3>
- <div id="about-content" style="display: none"></div>
- </div>
-
- <div id="getting-started">
- <h1>Getting started</h1>
- <h2>Here&rsquo;s how to get rolling:</h2>
-
- <ol>
- <li>
- <h2>Use <code>rails generate</code> to create your models and controllers</h2>
- <p>To see all available options, run it without parameters.</p>
- </li>
-
- <li>
- <h2>Set up a default route and remove or rename this file</h2>
- <p>Routes are set up in config/routes.rb.</p>
- </li>
-
- <li>
- <h2>Create your database</h2>
- <p>Run <code>rake db:migrate</code> to create your database. If you're not using SQLite (the default), edit <code>config/database.yml</code> with your username and password.</p>
- </li>
- </ol>
- </div>
- </div>
-
- <div id="footer">&nbsp;</div>
- </div>
+ <script src="javascripts/lib/jquery-1.5.1.min.js"></script>
+ <script src="javascripts/lib/underscore-min.js"></script>
+ <script src="javascripts/lib/backbone-min.js"></script>
+
+ <!-- Models -->
+ <script src="javascripts/models/Todo.js"></script>
</body>
-</html>
+</html>
View
5 public/javascripts/ToDo.js
@@ -1,5 +0,0 @@
-var ToDo = Backbone.Model.extend({
- defaults: {
- 'priority': 3
- }
-});
View
5 public/javascripts/models/Todo.js
@@ -0,0 +1,5 @@
+var Todo = Backbone.Model.extend({
+ defaults: {
+ 'priority': 3
+ }
+});
View
56 public/stylesheets/scaffold.css
@@ -0,0 +1,56 @@
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+div.field, div.actions {
+ margin-bottom: 10px;
+}
+
+#notice {
+ color: green;
+}
+
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+#error_explanation {
+ width: 450px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 0;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+}
+
+#error_explanation h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ margin-bottom: 0px;
+ background-color: #c00;
+ color: #fff;
+}
+
+#error_explanation ul li {
+ font-size: 12px;
+ list-style: square;
+}
View
4 spec/javascripts/ToDo.spec.js
@@ -1,9 +1,9 @@
-describe('ToDo model', function() {
+describe('Todo model', function() {
describe('when instantiated', function() {
beforeEach(function() {
- this.todo = new ToDo({
+ this.todo = new Todo({
title: 'Rake leaves',
tags: ['garden', 'weekend']
});
View
13 test/fixtures/todos.yml
@@ -0,0 +1,13 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ title: MyString
+ description: MyText
+ priority: 1
+ position: 1
+
+two:
+ title: MyString
+ description: MyText
+ priority: 1
+ position: 1
View
49 test/functional/todos_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class TodosControllerTest < ActionController::TestCase
+ setup do
+ @todo = todos(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:todos)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create todo" do
+ assert_difference('Todo.count') do
+ post :create, :todo => @todo.attributes
+ end
+
+ assert_redirected_to todo_path(assigns(:todo))
+ end
+
+ test "should show todo" do
+ get :show, :id => @todo.to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => @todo.to_param
+ assert_response :success
+ end
+
+ test "should update todo" do
+ put :update, :id => @todo.to_param, :todo => @todo.attributes
+ assert_redirected_to todo_path(assigns(:todo))
+ end
+
+ test "should destroy todo" do
+ assert_difference('Todo.count', -1) do
+ delete :destroy, :id => @todo.to_param
+ end
+
+ assert_redirected_to todos_path
+ end
+end
View
4 test/unit/helpers/todos_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class TodosHelperTest < ActionView::TestCase
+end
View
8 test/unit/todo_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class TodoTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.