Skip to content

Commit

Permalink
Scaffold document
Browse files Browse the repository at this point in the history
    % bin/rails generate scaffold document title:text content:text
  • Loading branch information
kou committed Dec 22, 2016
1 parent c3c6274 commit 7339d22
Show file tree
Hide file tree
Showing 20 changed files with 355 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/documents.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/documents.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the documents controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
89 changes: 89 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
@@ -0,0 +1,89 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
margin: 33px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
margin: 33px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding-bottom: 7px;
padding-left: 5px;
padding-right: 5px;
}

div {
&.field, &.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;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
74 changes: 74 additions & 0 deletions app/controllers/documents_controller.rb
@@ -0,0 +1,74 @@
class DocumentsController < ApplicationController
before_action :set_document, only: [:show, :edit, :update, :destroy]

# GET /documents
# GET /documents.json
def index
@documents = Document.all
end

# GET /documents/1
# GET /documents/1.json
def show
end

# GET /documents/new
def new
@document = Document.new
end

# GET /documents/1/edit
def edit
end

# POST /documents
# POST /documents.json
def create
@document = Document.new(document_params)

respond_to do |format|
if @document.save
format.html { redirect_to @document, notice: 'Document was successfully created.' }
format.json { render :show, status: :created, location: @document }
else
format.html { render :new }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /documents/1
# PATCH/PUT /documents/1.json
def update
respond_to do |format|
if @document.update(document_params)
format.html { redirect_to @document, notice: 'Document was successfully updated.' }
format.json { render :show, status: :ok, location: @document }
else
format.html { render :edit }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end

# DELETE /documents/1
# DELETE /documents/1.json
def destroy
@document.destroy
respond_to do |format|
format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_document
@document = Document.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def document_params
params.require(:document).permit(:title, :content)
end
end
2 changes: 2 additions & 0 deletions app/helpers/documents_helper.rb
@@ -0,0 +1,2 @@
module DocumentsHelper
end
2 changes: 2 additions & 0 deletions app/models/document.rb
@@ -0,0 +1,2 @@
class Document < ApplicationRecord
end
2 changes: 2 additions & 0 deletions app/views/documents/_document.json.jbuilder
@@ -0,0 +1,2 @@
json.extract! document, :id, :title, :content, :created_at, :updated_at
json.url document_url(document, format: :json)
27 changes: 27 additions & 0 deletions app/views/documents/_form.html.erb
@@ -0,0 +1,27 @@
<%= form_for(document) do |f| %>
<% if document.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(document.errors.count, "error") %> prohibited this document from being saved:</h2>

<ul>
<% document.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %>
<%= f.text_area :title %>
</div>

<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/documents/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing Document</h1>

<%= render 'form', document: @document %>
<%= link_to 'Show', @document %> |
<%= link_to 'Back', documents_path %>
29 changes: 29 additions & 0 deletions app/views/documents/index.html.erb
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Documents</h1>

<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @documents.each do |document| %>
<tr>
<td><%= document.title %></td>
<td><%= document.content %></td>
<td><%= link_to 'Show', document %></td>
<td><%= link_to 'Edit', edit_document_path(document) %></td>
<td><%= link_to 'Destroy', document, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Document', new_document_path %>
1 change: 1 addition & 0 deletions app/views/documents/index.json.jbuilder
@@ -0,0 +1 @@
json.array! @documents, partial: 'documents/document', as: :document
5 changes: 5 additions & 0 deletions app/views/documents/new.html.erb
@@ -0,0 +1,5 @@
<h1>New Document</h1>

<%= render 'form', document: @document %>
<%= link_to 'Back', documents_path %>
14 changes: 14 additions & 0 deletions app/views/documents/show.html.erb
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @document.title %>
</p>

<p>
<strong>Content:</strong>
<%= @document.content %>
</p>

<%= link_to 'Edit', edit_document_path(@document) %> |
<%= link_to 'Back', documents_path %>
1 change: 1 addition & 0 deletions app/views/documents/show.json.jbuilder
@@ -0,0 +1 @@
json.partial! "documents/document", document: @document
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
resources :documents
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
10 changes: 10 additions & 0 deletions db/migrate/20161222023536_create_documents.rb
@@ -0,0 +1,10 @@
class CreateDocuments < ActiveRecord::Migration[5.0]
def change
create_table :documents do |t|
t.text :title
t.text :content

t.timestamps
end
end
end
22 changes: 22 additions & 0 deletions db/schema.rb
@@ -0,0 +1,22 @@
# 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 that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161222023536) do

create_table "documents", force: :cascade do |t|
t.text "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
48 changes: 48 additions & 0 deletions test/controllers/documents_controller_test.rb
@@ -0,0 +1,48 @@
require 'test_helper'

class DocumentsControllerTest < ActionDispatch::IntegrationTest
setup do
@document = documents(:one)
end

test "should get index" do
get documents_url
assert_response :success
end

test "should get new" do
get new_document_url
assert_response :success
end

test "should create document" do
assert_difference('Document.count') do
post documents_url, params: { document: { content: @document.content, title: @document.title } }
end

assert_redirected_to document_url(Document.last)
end

test "should show document" do
get document_url(@document)
assert_response :success
end

test "should get edit" do
get edit_document_url(@document)
assert_response :success
end

test "should update document" do
patch document_url(@document), params: { document: { content: @document.content, title: @document.title } }
assert_redirected_to document_url(@document)
end

test "should destroy document" do
assert_difference('Document.count', -1) do
delete document_url(@document)
end

assert_redirected_to documents_url
end
end
9 changes: 9 additions & 0 deletions test/fixtures/documents.yml
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyText
content: MyText

two:
title: MyText
content: MyText
7 changes: 7 additions & 0 deletions test/models/document_test.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class DocumentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 7339d22

Please sign in to comment.