Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Finish demo app

  • Loading branch information...
commit 4f7c1dc47eb5b01a67424afe78fe716a580c562f 1 parent 08b12ff
itsjan authored

Showing 33 changed files with 607 additions and 0 deletions. Show diff stats Hide diff stats

  1. +3 0  app/assets/javascripts/microposts.js.coffee
  2. +3 0  app/assets/javascripts/users.js.coffee
  3. +3 0  app/assets/stylesheets/microposts.css.scss
  4. +69 0 app/assets/stylesheets/scaffolds.css.scss
  5. +3 0  app/assets/stylesheets/users.css.scss
  6. +83 0 app/controllers/microposts_controller.rb
  7. +83 0 app/controllers/users_controller.rb
  8. +2 0  app/helpers/microposts_helper.rb
  9. +2 0  app/helpers/users_helper.rb
  10. +8 0 app/models/micropost.rb
  11. +4 0 app/models/user.rb
  12. +25 0 app/views/microposts/_form.html.erb
  13. +6 0 app/views/microposts/edit.html.erb
  14. +25 0 app/views/microposts/index.html.erb
  15. +5 0 app/views/microposts/new.html.erb
  16. +15 0 app/views/microposts/show.html.erb
  17. +25 0 app/views/users/_form.html.erb
  18. +6 0 app/views/users/edit.html.erb
  19. +25 0 app/views/users/index.html.erb
  20. +5 0 app/views/users/new.html.erb
  21. +15 0 app/views/users/show.html.erb
  22. +4 0 config/routes.rb
  23. +10 0 db/migrate/20121007091247_create_users.rb
  24. +10 0 db/migrate/20121007093741_create_microposts.rb
  25. +30 0 db/schema.rb
  26. +9 0 test/fixtures/microposts.yml
  27. +9 0 test/fixtures/users.yml
  28. +49 0 test/functional/microposts_controller_test.rb
  29. +49 0 test/functional/users_controller_test.rb
  30. +4 0 test/unit/helpers/microposts_helper_test.rb
  31. +4 0 test/unit/helpers/users_helper_test.rb
  32. +7 0 test/unit/micropost_test.rb
  33. +7 0 test/unit/user_test.rb
3  app/assets/javascripts/microposts.js.coffee
... ... @@ -0,0 +1,3 @@
  1 +# Place all the behaviors and hooks related to the matching controller here.
  2 +# All this logic will automatically be available in application.js.
  3 +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3  app/assets/javascripts/users.js.coffee
... ... @@ -0,0 +1,3 @@
  1 +# Place all the behaviors and hooks related to the matching controller here.
  2 +# All this logic will automatically be available in application.js.
  3 +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3  app/assets/stylesheets/microposts.css.scss
... ... @@ -0,0 +1,3 @@
  1 +// Place all the styles related to the Microposts controller here.
  2 +// They will automatically be included in application.css.
  3 +// You can use Sass (SCSS) here: http://sass-lang.com/
69 app/assets/stylesheets/scaffolds.css.scss
... ... @@ -0,0 +1,69 @@
  1 +body {
  2 + background-color: #fff;
  3 + color: #333;
  4 + font-family: verdana, arial, helvetica, sans-serif;
  5 + font-size: 13px;
  6 + line-height: 18px;
  7 +}
  8 +
  9 +p, ol, ul, td {
  10 + font-family: verdana, arial, helvetica, sans-serif;
  11 + font-size: 13px;
  12 + line-height: 18px;
  13 +}
  14 +
  15 +pre {
  16 + background-color: #eee;
  17 + padding: 10px;
  18 + font-size: 11px;
  19 +}
  20 +
  21 +a {
  22 + color: #000;
  23 + &:visited {
  24 + color: #666;
  25 + }
  26 + &:hover {
  27 + color: #fff;
  28 + background-color: #000;
  29 + }
  30 +}
  31 +
  32 +div {
  33 + &.field, &.actions {
  34 + margin-bottom: 10px;
  35 + }
  36 +}
  37 +
  38 +#notice {
  39 + color: green;
  40 +}
  41 +
  42 +.field_with_errors {
  43 + padding: 2px;
  44 + background-color: red;
  45 + display: table;
  46 +}
  47 +
  48 +#error_explanation {
  49 + width: 450px;
  50 + border: 2px solid red;
  51 + padding: 7px;
  52 + padding-bottom: 0;
  53 + margin-bottom: 20px;
  54 + background-color: #f0f0f0;
  55 + h2 {
  56 + text-align: left;
  57 + font-weight: bold;
  58 + padding: 5px 5px 5px 15px;
  59 + font-size: 12px;
  60 + margin: -7px;
  61 + margin-bottom: 0px;
  62 + background-color: #c00;
  63 + color: #fff;
  64 + }
  65 + ul li {
  66 + font-size: 12px;
  67 + list-style: square;
  68 + }
  69 +}
3  app/assets/stylesheets/users.css.scss
... ... @@ -0,0 +1,3 @@
  1 +// Place all the styles related to the Users controller here.
  2 +// They will automatically be included in application.css.
  3 +// You can use Sass (SCSS) here: http://sass-lang.com/
83 app/controllers/microposts_controller.rb
... ... @@ -0,0 +1,83 @@
  1 +class MicropostsController < ApplicationController
  2 + # GET /microposts
  3 + # GET /microposts.json
  4 + def index
  5 + @microposts = Micropost.all
  6 +
  7 + respond_to do |format|
  8 + format.html # index.html.erb
  9 + format.json { render json: @microposts }
  10 + end
  11 + end
  12 +
  13 + # GET /microposts/1
  14 + # GET /microposts/1.json
  15 + def show
  16 + @micropost = Micropost.find(params[:id])
  17 +
  18 + respond_to do |format|
  19 + format.html # show.html.erb
  20 + format.json { render json: @micropost }
  21 + end
  22 + end
  23 +
  24 + # GET /microposts/new
  25 + # GET /microposts/new.json
  26 + def new
  27 + @micropost = Micropost.new
  28 +
  29 + respond_to do |format|
  30 + format.html # new.html.erb
  31 + format.json { render json: @micropost }
  32 + end
  33 + end
  34 +
  35 + # GET /microposts/1/edit
  36 + def edit
  37 + @micropost = Micropost.find(params[:id])
  38 + end
  39 +
  40 + # POST /microposts
  41 + # POST /microposts.json
  42 + def create
  43 + @micropost = Micropost.new(params[:micropost])
  44 +
  45 + respond_to do |format|
  46 + if @micropost.save
  47 + format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
  48 + format.json { render json: @micropost, status: :created, location: @micropost }
  49 + else
  50 + format.html { render action: "new" }
  51 + format.json { render json: @micropost.errors, status: :unprocessable_entity }
  52 + end
  53 + end
  54 + end
  55 +
  56 + # PUT /microposts/1
  57 + # PUT /microposts/1.json
  58 + def update
  59 + @micropost = Micropost.find(params[:id])
  60 +
  61 + respond_to do |format|
  62 + if @micropost.update_attributes(params[:micropost])
  63 + format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
  64 + format.json { head :no_content }
  65 + else
  66 + format.html { render action: "edit" }
  67 + format.json { render json: @micropost.errors, status: :unprocessable_entity }
  68 + end
  69 + end
  70 + end
  71 +
  72 + # DELETE /microposts/1
  73 + # DELETE /microposts/1.json
  74 + def destroy
  75 + @micropost = Micropost.find(params[:id])
  76 + @micropost.destroy
  77 +
  78 + respond_to do |format|
  79 + format.html { redirect_to microposts_url }
  80 + format.json { head :no_content }
  81 + end
  82 + end
  83 +end
83 app/controllers/users_controller.rb
... ... @@ -0,0 +1,83 @@
  1 +class UsersController < ApplicationController
  2 + # GET /users
  3 + # GET /users.json
  4 + def index
  5 + @users = User.all
  6 +
  7 + respond_to do |format|
  8 + format.html # index.html.erb
  9 + format.json { render json: @users }
  10 + end
  11 + end
  12 +
  13 + # GET /users/1
  14 + # GET /users/1.json
  15 + def show
  16 + @user = User.find(params[:id])
  17 +
  18 + respond_to do |format|
  19 + format.html # show.html.erb
  20 + format.json { render json: @user }
  21 + end
  22 + end
  23 +
  24 + # GET /users/new
  25 + # GET /users/new.json
  26 + def new
  27 + @user = User.new
  28 +
  29 + respond_to do |format|
  30 + format.html # new.html.erb
  31 + format.json { render json: @user }
  32 + end
  33 + end
  34 +
  35 + # GET /users/1/edit
  36 + def edit
  37 + @user = User.find(params[:id])
  38 + end
  39 +
  40 + # POST /users
  41 + # POST /users.json
  42 + def create
  43 + @user = User.new(params[:user])
  44 +
  45 + respond_to do |format|
  46 + if @user.save
  47 + format.html { redirect_to @user, notice: 'User was successfully created.' }
  48 + format.json { render json: @user, status: :created, location: @user }
  49 + else
  50 + format.html { render action: "new" }
  51 + format.json { render json: @user.errors, status: :unprocessable_entity }
  52 + end
  53 + end
  54 + end
  55 +
  56 + # PUT /users/1
  57 + # PUT /users/1.json
  58 + def update
  59 + @user = User.find(params[:id])
  60 +
  61 + respond_to do |format|
  62 + if @user.update_attributes(params[:user])
  63 + format.html { redirect_to @user, notice: 'User was successfully updated.' }
  64 + format.json { head :no_content }
  65 + else
  66 + format.html { render action: "edit" }
  67 + format.json { render json: @user.errors, status: :unprocessable_entity }
  68 + end
  69 + end
  70 + end
  71 +
  72 + # DELETE /users/1
  73 + # DELETE /users/1.json
  74 + def destroy
  75 + @user = User.find(params[:id])
  76 + @user.destroy
  77 +
  78 + respond_to do |format|
  79 + format.html { redirect_to users_url }
  80 + format.json { head :no_content }
  81 + end
  82 + end
  83 +end
2  app/helpers/microposts_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +module MicropostsHelper
  2 +end
2  app/helpers/users_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +module UsersHelper
  2 +end
8 app/models/micropost.rb
... ... @@ -0,0 +1,8 @@
  1 +class Micropost < ActiveRecord::Base
  2 + attr_accessible :content, :user_id
  3 +
  4 + belongs_to :user
  5 +
  6 + validates :content, :length => { :maximum => 140}
  7 +
  8 +end
4 app/models/user.rb
... ... @@ -0,0 +1,4 @@
  1 +class User < ActiveRecord::Base
  2 + attr_accessible :email, :name
  3 + has_many :microposts
  4 +end
25 app/views/microposts/_form.html.erb
... ... @@ -0,0 +1,25 @@
  1 +<%= form_for(@micropost) do |f| %>
  2 + <% if @micropost.errors.any? %>
  3 + <div id="error_explanation">
  4 + <h2><%= pluralize(@micropost.errors.count, "error") %> prohibited this micropost from being saved:</h2>
  5 +
  6 + <ul>
  7 + <% @micropost.errors.full_messages.each do |msg| %>
  8 + <li><%= msg %></li>
  9 + <% end %>
  10 + </ul>
  11 + </div>
  12 + <% end %>
  13 +
  14 + <div class="field">
  15 + <%= f.label :content %><br />
  16 + <%= f.text_field :content %>
  17 + </div>
  18 + <div class="field">
  19 + <%= f.label :user_id %><br />
  20 + <%= f.number_field :user_id %>
  21 + </div>
  22 + <div class="actions">
  23 + <%= f.submit %>
  24 + </div>
  25 +<% end %>
6 app/views/microposts/edit.html.erb
... ... @@ -0,0 +1,6 @@
  1 +<h1>Editing micropost</h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Show', @micropost %> |
  6 +<%= link_to 'Back', microposts_path %>
25 app/views/microposts/index.html.erb
... ... @@ -0,0 +1,25 @@
  1 +<h1>Listing microposts</h1>
  2 +
  3 +<table>
  4 + <tr>
  5 + <th>Content</th>
  6 + <th>User</th>
  7 + <th></th>
  8 + <th></th>
  9 + <th></th>
  10 + </tr>
  11 +
  12 +<% @microposts.each do |micropost| %>
  13 + <tr>
  14 + <td><%= micropost.content %></td>
  15 + <td><%= micropost.user_id %></td>
  16 + <td><%= link_to 'Show', micropost %></td>
  17 + <td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
  18 + <td><%= link_to 'Destroy', micropost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  19 + </tr>
  20 +<% end %>
  21 +</table>
  22 +
  23 +<br />
  24 +
  25 +<%= link_to 'New Micropost', new_micropost_path %>
5 app/views/microposts/new.html.erb
... ... @@ -0,0 +1,5 @@
  1 +<h1>New micropost</h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Back', microposts_path %>
15 app/views/microposts/show.html.erb
... ... @@ -0,0 +1,15 @@
  1 +<p id="notice"><%= notice %></p>
  2 +
  3 +<p>
  4 + <b>Content:</b>
  5 + <%= @micropost.content %>
  6 +</p>
  7 +
  8 +<p>
  9 + <b>User:</b>
  10 + <%= @micropost.user_id %>
  11 +</p>
  12 +
  13 +
  14 +<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
  15 +<%= link_to 'Back', microposts_path %>
25 app/views/users/_form.html.erb
... ... @@ -0,0 +1,25 @@
  1 +<%= form_for(@user) do |f| %>
  2 + <% if @user.errors.any? %>
  3 + <div id="error_explanation">
  4 + <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
  5 +
  6 + <ul>
  7 + <% @user.errors.full_messages.each do |msg| %>
  8 + <li><%= msg %></li>
  9 + <% end %>
  10 + </ul>
  11 + </div>
  12 + <% end %>
  13 +
  14 + <div class="field">
  15 + <%= f.label :name %><br />
  16 + <%= f.text_field :name %>
  17 + </div>
  18 + <div class="field">
  19 + <%= f.label :email %><br />
  20 + <%= f.text_field :email %>
  21 + </div>
  22 + <div class="actions">
  23 + <%= f.submit %>
  24 + </div>
  25 +<% end %>
6 app/views/users/edit.html.erb
... ... @@ -0,0 +1,6 @@
  1 +<h1>Editing user</h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Show', @user %> |
  6 +<%= link_to 'Back', users_path %>
25 app/views/users/index.html.erb
... ... @@ -0,0 +1,25 @@
  1 +<h1>Listing users</h1>
  2 +
  3 +<table border=1>
  4 + <tr>
  5 + <th>Name</th>
  6 + <th>Email</th>
  7 + <th></th>
  8 + <th></th>
  9 + <th></th>
  10 + </tr>
  11 +
  12 +<% @users.each do |user| %>
  13 + <tr>
  14 + <td><%= user.name %></td>
  15 + <td><%= user.email %></td>
  16 + <td><%= link_to 'Show', user %></td>
  17 + <td><%= link_to 'Edit', edit_user_path(user) %></td>
  18 + <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  19 + </tr>
  20 +<% end %>
  21 +</table>
  22 +
  23 +<br />
  24 +
  25 +<%= link_to 'New User', new_user_path %>
5 app/views/users/new.html.erb
... ... @@ -0,0 +1,5 @@
  1 +<h1>New user</h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Back', users_path %>
15 app/views/users/show.html.erb
... ... @@ -0,0 +1,15 @@
  1 +<p id="notice"><%= notice %></p>
  2 +
  3 +<p>
  4 + <b>Name:</b>
  5 + <%= @user.name %>
  6 +</p>
  7 +
  8 +<p>
  9 + <b>Email:</b>
  10 + <%= @user.email %>
  11 +</p>
  12 +
  13 +
  14 +<%= link_to 'Edit', edit_user_path(@user) %> |
  15 +<%= link_to 'Back', users_path %>
4 config/routes.rb
... ... @@ -1,4 +1,8 @@
1 1 DemoApp::Application.routes.draw do
  2 + resources :microposts
  3 +
  4 + resources :users
  5 +
2 6 # The priority is based upon order of creation:
3 7 # first created -> highest priority.
4 8
10 db/migrate/20121007091247_create_users.rb
... ... @@ -0,0 +1,10 @@
  1 +class CreateUsers < ActiveRecord::Migration
  2 + def change
  3 + create_table :users do |t|
  4 + t.string :name
  5 + t.string :email
  6 +
  7 + t.timestamps
  8 + end
  9 + end
  10 +end
10 db/migrate/20121007093741_create_microposts.rb
... ... @@ -0,0 +1,10 @@
  1 +class CreateMicroposts < ActiveRecord::Migration
  2 + def change
  3 + create_table :microposts do |t|
  4 + t.string :content
  5 + t.integer :user_id
  6 +
  7 + t.timestamps
  8 + end
  9 + end
  10 +end
30 db/schema.rb
... ... @@ -0,0 +1,30 @@
  1 +# encoding: UTF-8
  2 +# This file is auto-generated from the current state of the database. Instead
  3 +# of editing this file, please use the migrations feature of Active Record to
  4 +# incrementally modify your database, and then regenerate this schema definition.
  5 +#
  6 +# Note that this schema.rb definition is the authoritative source for your
  7 +# database schema. If you need to create the application database on another
  8 +# system, you should be using db:schema:load, not running all the migrations
  9 +# from scratch. The latter is a flawed and unsustainable approach (the more migrations
  10 +# you'll amass, the slower it'll run and the greater likelihood for issues).
  11 +#
  12 +# It's strongly recommended to check this file into your version control system.
  13 +
  14 +ActiveRecord::Schema.define(:version => 20121007093741) do
  15 +
  16 + create_table "microposts", :force => true do |t|
  17 + t.string "content"
  18 + t.integer "user_id"
  19 + t.datetime "created_at", :null => false
  20 + t.datetime "updated_at", :null => false
  21 + end
  22 +
  23 + create_table "users", :force => true do |t|
  24 + t.string "name"
  25 + t.string "email"
  26 + t.datetime "created_at", :null => false
  27 + t.datetime "updated_at", :null => false
  28 + end
  29 +
  30 +end
9 test/fixtures/microposts.yml
... ... @@ -0,0 +1,9 @@
  1 +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
  2 +
  3 +one:
  4 + content: MyString
  5 + user_id: 1
  6 +
  7 +two:
  8 + content: MyString
  9 + user_id: 1
9 test/fixtures/users.yml
... ... @@ -0,0 +1,9 @@
  1 +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
  2 +
  3 +one:
  4 + name: MyString
  5 + email: MyString
  6 +
  7 +two:
  8 + name: MyString
  9 + email: MyString
49 test/functional/microposts_controller_test.rb
... ... @@ -0,0 +1,49 @@
  1 +require 'test_helper'
  2 +
  3 +class MicropostsControllerTest < ActionController::TestCase
  4 + setup do
  5 + @micropost = microposts(:one)
  6 + end
  7 +
  8 + test "should get index" do
  9 + get :index
  10 + assert_response :success
  11 + assert_not_nil assigns(:microposts)
  12 + end
  13 +
  14 + test "should get new" do
  15 + get :new
  16 + assert_response :success
  17 + end
  18 +
  19 + test "should create micropost" do
  20 + assert_difference('Micropost.count') do
  21 + post :create, micropost: { content: @micropost.content, user_id: @micropost.user_id }
  22 + end
  23 +
  24 + assert_redirected_to micropost_path(assigns(:micropost))
  25 + end
  26 +
  27 + test "should show micropost" do
  28 + get :show, id: @micropost
  29 + assert_response :success
  30 + end
  31 +
  32 + test "should get edit" do
  33 + get :edit, id: @micropost
  34 + assert_response :success
  35 + end
  36 +
  37 + test "should update micropost" do
  38 + put :update, id: @micropost, micropost: { content: @micropost.content, user_id: @micropost.user_id }
  39 + assert_redirected_to micropost_path(assigns(:micropost))
  40 + end
  41 +
  42 + test "should destroy micropost" do
  43 + assert_difference('Micropost.count', -1) do
  44 + delete :destroy, id: @micropost
  45 + end
  46 +
  47 + assert_redirected_to microposts_path
  48 + end
  49 +end
49 test/functional/users_controller_test.rb
... ... @@ -0,0 +1,49 @@
  1 +require 'test_helper'
  2 +
  3 +class UsersControllerTest < ActionController::TestCase
  4 + setup do
  5 + @user = users(:one)
  6 + end
  7 +
  8 + test "should get index" do
  9 + get :index
  10 + assert_response :success
  11 + assert_not_nil assigns(:users)
  12 + end
  13 +
  14 + test "should get new" do
  15 + get :new
  16 + assert_response :success
  17 + end
  18 +
  19 + test "should create user" do
  20 + assert_difference('User.count') do
  21 + post :create, user: { email: @user.email, name: @user.name }
  22 + end
  23 +
  24 + assert_redirected_to user_path(assigns(:user))
  25 + end
  26 +
  27 + test "should show user" do
  28 + get :show, id: @user
  29 + assert_response :success
  30 + end
  31 +
  32 + test "should get edit" do
  33 + get :edit, id: @user
  34 + assert_response :success
  35 + end
  36 +
  37 + test "should update user" do
  38 + put :update, id: @user, user: { email: @user.email, name: @user.name }
  39 + assert_redirected_to user_path(assigns(:user))
  40 + end
  41 +
  42 + test "should destroy user" do
  43 + assert_difference('User.count', -1) do
  44 + delete :destroy, id: @user
  45 + end
  46 +
  47 + assert_redirected_to users_path
  48 + end
  49 +end
4 test/unit/helpers/microposts_helper_test.rb
... ... @@ -0,0 +1,4 @@
  1 +require 'test_helper'
  2 +
  3 +class MicropostsHelperTest < ActionView::TestCase
  4 +end
4 test/unit/helpers/users_helper_test.rb
... ... @@ -0,0 +1,4 @@
  1 +require 'test_helper'
  2 +
  3 +class UsersHelperTest < ActionView::TestCase
  4 +end
7 test/unit/micropost_test.rb
... ... @@ -0,0 +1,7 @@
  1 +require 'test_helper'
  2 +
  3 +class MicropostTest < ActiveSupport::TestCase
  4 + # test "the truth" do
  5 + # assert true
  6 + # end
  7 +end
7 test/unit/user_test.rb
... ... @@ -0,0 +1,7 @@
  1 +require 'test_helper'
  2 +
  3 +class UserTest < ActiveSupport::TestCase
  4 + # test "the truth" do
  5 + # assert true
  6 + # end
  7 +end

0 comments on commit 4f7c1dc

Please sign in to comment.
Something went wrong with that request. Please try again.