[Groonga-commit] groonga/heroku-sample-rroonga-blog at ba7a79e [master] Generate posts resource

Back to archive index

Kouhei Sutou null+****@clear*****
Sun May 25 12:39:05 JST 2014


Kouhei Sutou	2014-05-25 12:39:05 +0900 (Sun, 25 May 2014)

  New Revision: ba7a79e75ef60d6abf1fedd59c1fe8ee8bdfa2c6
  https://github.com/groonga/heroku-sample-rroonga-blog/commit/ba7a79e75ef60d6abf1fedd59c1fe8ee8bdfa2c6

  Message:
    Generate posts resource
    
        % rails generate scaffold posts title:string content:text

  Added files:
    app/assets/javascripts/posts.js.coffee
    app/assets/stylesheets/posts.css.scss
    app/assets/stylesheets/scaffolds.css.scss
    app/controllers/posts_controller.rb
    app/helpers/posts_helper.rb
    app/models/post.rb
    app/views/posts/_form.html.erb
    app/views/posts/edit.html.erb
    app/views/posts/index.html.erb
    app/views/posts/index.json.jbuilder
    app/views/posts/new.html.erb
    app/views/posts/show.html.erb
    app/views/posts/show.json.jbuilder
    db/migrate/20140525033137_create_posts.rb
    db/schema.rb
    test/controllers/posts_controller_test.rb
    test/fixtures/posts.yml
    test/helpers/posts_helper_test.rb
    test/models/post_test.rb
  Modified files:
    config/routes.rb

  Added: app/assets/javascripts/posts.js.coffee (+3 -0) 100644
===================================================================
--- /dev/null
+++ app/assets/javascripts/posts.js.coffee    2014-05-25 12:39:05 +0900 (24f83d1)
@@ -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/

  Added: app/assets/stylesheets/posts.css.scss (+3 -0) 100644
===================================================================
--- /dev/null
+++ app/assets/stylesheets/posts.css.scss    2014-05-25 12:39:05 +0900 (1a7e153)
@@ -0,0 +1,3 @@
+// Place all the styles related to the posts controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/

  Added: app/assets/stylesheets/scaffolds.css.scss (+69 -0) 100644
===================================================================
--- /dev/null
+++ app/assets/stylesheets/scaffolds.css.scss    2014-05-25 12:39:05 +0900 (6ec6a8f)
@@ -0,0 +1,69 @@
+body {
+  background-color: #fff;
+  color: #333;
+  font-family: verdana, arial, helvetica, sans-serif;
+  font-size: 13px;
+  line-height: 18px;
+}
+
+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;
+  &:visited {
+    color: #666;
+  }
+  &:hover {
+    color: #fff;
+    background-color: #000;
+  }
+}
+
+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: 0px;
+    background-color: #c00;
+    color: #fff;
+  }
+  ul li {
+    font-size: 12px;
+    list-style: square;
+  }
+}

  Added: app/controllers/posts_controller.rb (+74 -0) 100644
===================================================================
--- /dev/null
+++ app/controllers/posts_controller.rb    2014-05-25 12:39:05 +0900 (d566eaf)
@@ -0,0 +1,74 @@
+class PostsController < ApplicationController
+  before_action :set_post, only: [:show, :edit, :update, :destroy]
+
+  # GET /posts
+  # GET /posts.json
+  def index
+    @posts = Post.all
+  end
+
+  # GET /posts/1
+  # GET /posts/1.json
+  def show
+  end
+
+  # GET /posts/new
+  def new
+    @post = Post.new
+  end
+
+  # GET /posts/1/edit
+  def edit
+  end
+
+  # POST /posts
+  # POST /posts.json
+  def create
+    @post = Post.new(post_params)
+
+    respond_to do |format|
+      if****@post*****
+        format.html { redirect_to @post, notice: 'Post was successfully created.' }
+        format.json { render :show, status: :created, location: @post }
+      else
+        format.html { render :new }
+        format.json { render json: @post.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # PATCH/PUT /posts/1
+  # PATCH/PUT /posts/1.json
+  def update
+    respond_to do |format|
+      if****@post*****(post_params)
+        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
+        format.json { render :show, status: :ok, location: @post }
+      else
+        format.html { render :edit }
+        format.json { render json: @post.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /posts/1
+  # DELETE /posts/1.json
+  def destroy
+    @post.destroy
+    respond_to do |format|
+      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
+      format.json { head :no_content }
+    end
+  end
+
+  private
+    # Use callbacks to share common setup or constraints between actions.
+    def set_post
+      @post = Post.find(params[:id])
+    end
+
+    # Never trust parameters from the scary internet, only allow the white list through.
+    def post_params
+      params.require(:post).permit(:title, :content)
+    end
+end

  Added: app/helpers/posts_helper.rb (+2 -0) 100644
===================================================================
--- /dev/null
+++ app/helpers/posts_helper.rb    2014-05-25 12:39:05 +0900 (a7b8cec)
@@ -0,0 +1,2 @@
+module PostsHelper
+end

  Added: app/models/post.rb (+2 -0) 100644
===================================================================
--- /dev/null
+++ app/models/post.rb    2014-05-25 12:39:05 +0900 (791dcb5)
@@ -0,0 +1,2 @@
+class Post < ActiveRecord::Base
+end

  Added: app/views/posts/_form.html.erb (+25 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/_form.html.erb    2014-05-25 12:39:05 +0900 (d4faf01)
@@ -0,0 +1,25 @@
+<%= form_for(@post) do |f| %>
+  <% if****@post*****? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
+
+      <ul>
+      <% @post.errors.full_messages.each do |message| %>
+        <li><%= message %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :title %><br>
+    <%= f.text_field :title %>
+  </div>
+  <div class="field">
+    <%= f.label :content %><br>
+    <%= f.text_area :content %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>

  Added: app/views/posts/edit.html.erb (+6 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/edit.html.erb    2014-05-25 12:39:05 +0900 (7205802)
@@ -0,0 +1,6 @@
+<h1>Editing post</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @post %> |
+<%= link_to 'Back', posts_path %>

  Added: app/views/posts/index.html.erb (+27 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/index.html.erb    2014-05-25 12:39:05 +0900 (e29a7e3)
@@ -0,0 +1,27 @@
+<h1>Listing posts</h1>
+
+<table>
+  <thead>
+    <tr>
+      <th>Title</th>
+      <th>Content</th>
+      <th colspan="3"></th>
+    </tr>
+  </thead>
+
+  <tbody>
+    <% @posts.each do |post| %>
+      <tr>
+        <td><%= post.title %></td>
+        <td><%= post.content %></td>
+        <td><%= link_to 'Show', post %></td>
+        <td><%= link_to 'Edit', edit_post_path(post) %></td>
+        <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
+
+<br>
+
+<%= link_to 'New Post', new_post_path %>

  Added: app/views/posts/index.json.jbuilder (+4 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/index.json.jbuilder    2014-05-25 12:39:05 +0900 (0557ac5)
@@ -0,0 +1,4 @@
+json.array!(@posts) do |post|
+  json.extract! post, :id, :title, :content
+  json.url post_url(post, format: :json)
+end

  Added: app/views/posts/new.html.erb (+5 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/new.html.erb    2014-05-25 12:39:05 +0900 (36ad742)
@@ -0,0 +1,5 @@
+<h1>New post</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', posts_path %>

  Added: app/views/posts/show.html.erb (+14 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/show.html.erb    2014-05-25 12:39:05 +0900 (e03ead5)
@@ -0,0 +1,14 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <strong>Title:</strong>
+  <%=****@post***** %>
+</p>
+
+<p>
+  <strong>Content:</strong>
+  <%=****@post***** %>
+</p>
+
+<%= link_to 'Edit', edit_post_path(@post) %> |
+<%= link_to 'Back', posts_path %>

  Added: app/views/posts/show.json.jbuilder (+1 -0) 100644
===================================================================
--- /dev/null
+++ app/views/posts/show.json.jbuilder    2014-05-25 12:39:05 +0900 (c730693)
@@ -0,0 +1 @@
+json.extract! @post, :id, :title, :content, :created_at, :updated_at

  Modified: config/routes.rb (+3 -1)
===================================================================
--- config/routes.rb    2014-05-25 12:30:57 +0900 (3f66539)
+++ config/routes.rb    2014-05-25 12:39:05 +0900 (f4140ae)
@@ -1,9 +1,11 @@
 Rails.application.routes.draw do
+  resources :posts
+
   # The priority is based upon order of creation: first created -> highest priority.
   # See how all your routes lay out with "rake routes".
 
   # You can have the root of your site routed with "root"
-  # root 'welcome#index'
+  root 'posts#index'
 
   # Example of regular route:
   #   get 'products/:id' => 'catalog#view'

  Added: db/migrate/20140525033137_create_posts.rb (+10 -0) 100644
===================================================================
--- /dev/null
+++ db/migrate/20140525033137_create_posts.rb    2014-05-25 12:39:05 +0900 (e25e2cc)
@@ -0,0 +1,10 @@
+class CreatePosts < ActiveRecord::Migration
+  def change
+    create_table :posts do |t|
+      t.string :title
+      t.text :content
+
+      t.timestamps
+    end
+  end
+end

  Added: db/schema.rb (+26 -0) 100644
===================================================================
--- /dev/null
+++ db/schema.rb    2014-05-25 12:39:05 +0900 (f8d8d52)
@@ -0,0 +1,26 @@
+# encoding: UTF-8
+# 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: 20140525033137) do
+
+  # These are extensions that must be enabled in order to support this database
+  enable_extension "plpgsql"
+
+  create_table "posts", force: true do |t|
+    t.string   "title"
+    t.text     "content"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
+
+end

  Added: test/controllers/posts_controller_test.rb (+49 -0) 100644
===================================================================
--- /dev/null
+++ test/controllers/posts_controller_test.rb    2014-05-25 12:39:05 +0900 (f27997e)
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class PostsControllerTest < ActionController::TestCase
+  setup do
+    @post = posts(:one)
+  end
+
+  test "should get index" do
+    get :index
+    assert_response :success
+    assert_not_nil assigns(:posts)
+  end
+
+  test "should get new" do
+    get :new
+    assert_response :success
+  end
+
+  test "should create post" do
+    assert_difference('Post.count') do
+      post :create, post: { content: @post.content, title: @post.title }
+    end
+
+    assert_redirected_to post_path(assigns(:post))
+  end
+
+  test "should show post" do
+    get :show, id: @post
+    assert_response :success
+  end
+
+  test "should get edit" do
+    get :edit, id: @post
+    assert_response :success
+  end
+
+  test "should update post" do
+    patch :update, id: @post, post: { content: @post.content, title: @post.title }
+    assert_redirected_to post_path(assigns(:post))
+  end
+
+  test "should destroy post" do
+    assert_difference('Post.count', -1) do
+      delete :destroy, id: @post
+    end
+
+    assert_redirected_to posts_path
+  end
+end

  Added: test/fixtures/posts.yml (+9 -0) 100644
===================================================================
--- /dev/null
+++ test/fixtures/posts.yml    2014-05-25 12:39:05 +0900 (19db450)
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  title: MyString
+  content: MyText
+
+two:
+  title: MyString
+  content: MyText

  Added: test/helpers/posts_helper_test.rb (+4 -0) 100644
===================================================================
--- /dev/null
+++ test/helpers/posts_helper_test.rb    2014-05-25 12:39:05 +0900 (48549c2)
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class PostsHelperTest < ActionView::TestCase
+end

  Added: test/models/post_test.rb (+7 -0) 100644
===================================================================
--- /dev/null
+++ test/models/post_test.rb    2014-05-25 12:39:05 +0900 (6d9d463)
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class PostTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index