Wednesday, May 10, 2017

Ch 5: Filling in the Layout

Today, I have completed the Chapter 5 of Michael Hartl's Ruby on Rails Tutorial book.

* I created a new branch: filling-in-layout
$ git checkout -b filling-in-layout

* I updated application layouts file:
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

* I editted home.html.erb
app/views/static_pages/home.html.erb
<div class="center jumbotron">
  <h1>Welcome to the Sample App</h1>

  <h2>
    This is the home page for the
    <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>

  <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails logo"),
            'http://rubyonrails.org/' %>

* I downloaded a image file rails.png and put it on app/assets/images/

* I added bootstrap-sass gem to the Gemfile
gem 'bootstrap-sass', '3.3.6'

* I ran bundle install
$ bundle install

* I created a new custom.scss file to put all of the CSS needed for this tutorial in a single file.
$ touch app/assets/stylesheets/custom.scss

* I coded the custom.scss file:
app/assets/stylesheets/custom.scss

@import "bootstrap-sprockets";
@import "bootstrap";

/* mixins, variables, etc. */

$gray-medium-light: #eaeaea;

/* universal */

body {
  padding-top: 60px;
}

section {
  overflow: auto;
}

textarea {
  resize: vertical;
}

.center {
  text-align: center;
  h1 {
    margin-bottom: 10px;
  }
}

/* typography */

h1, h2, h3, h4, h5, h6 {
  line-height: 1;
}

h1 {
  font-size: 3em;
  letter-spacing: -2px;
  margin-bottom: 30px;
  text-align: center;
}

h2 {
  font-size: 1.2em;
  letter-spacing: -1px;
  margin-bottom: 30px;
  text-align: center;
  font-weight: normal;
  color: $gray-light;
}

p {
  font-size: 1.1em;
  line-height: 1.7em;
}


/* header */

#logo {
  float: left;
  margin-right: 10px;
  font-size: 1.7em;
  color: white;
  text-transform: uppercase;
  letter-spacing: -1px;
  padding-top: 9px;
  font-weight: bold;
  &:hover {
    color: white;
    text-decoration: none;
  }
}

/* footer */

footer {
  margin-top: 45px;
  padding-top: 5px;
  border-top: 1px solid $gray-medium-light;
  color: $gray-light;
  a {
    color: $gray;
    &:hover {
      color: $gray-darker;
    }
  }
  small {
    float: left;
  }
  ul {
    float: right;
    list-style: none;
    li {
      float: left;
      margin-left: 15px;
    }
  }
}

* I defined partial for the stylesheets and header.
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

* I wrote shim partial:
app/views/layouts/_shim.html.erb
<!--[if lt IE 9]>
  <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
  </script>
<![endif]-->

* I wrote header partial:
app/views/layouts/_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home",   root_path %></li>
        <li><%= link_to "Help",   help_path %></li>
        <li><%= link_to "Log in", '#' %></li>
      </ul>
    </nav>
  </div>
</header>

* I wrote footer partial:
app/views/layouts/_footer.html.erb
<footer class="footer">
  <small>
    The <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a>
    by <a href="http://www.michaelhartl.com/">Michael Hartl</a>
  </small>
  <nav>
    <ul>
      <li><%= link_to "About",   about_path %></li>
      <li><%= link_to "Contact", contact_path %></li>
      <li><a href="http://news.railstutorial.org/">News</a></li>
    </ul>
  </nav>
</footer>

* I added a test case for Contact page:
test/controllers/static_pages_controller_test.rb
   test "should get contact" do
      get contact_path
      assert_response :success
      assert_select "title", "Contact | #{@base_title}"
  end   

* I added a route for Contact page:
config/routes.rb
Rails.application.routes.draw do
  get 'users/new'

  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
  get  '/signup',  to: 'users#new'   
end

* I added an action (a method) to for Contact page in the Static Pages Controller:
app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
  def home
  end

  def help
  end
   
  def about
  end
   
  def contact
  end
   
end

* I created a new Contact Page in html.erb:
app/views/static_pages/contact.html.erb
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
  Contact the Ruby on Rails Tutorial about the sample app at the
  <a href="http://www.railstutorial.org/contact">contact page</a>.
</p>

* I ran the test and it was passed:
$ rails test

* I re-wrote the routing rules in config/routes.rb

* I updated the routing path in static pages test cases:
test/controllers/static_pages_controller_test.rb
From: get static_pages_help_url
To: get help_path


* I coded the paths for link_to function in header layout:
app/views/layouts/_header.html.erb
<li><%= link_to "Home",    root_path %></li>
<li><%= link_to "Help",    help_path %></li>

* I coded the path in footer layout too: app/views/layouts/_footer.html.erb

* I wrote an integration test "site layout" for testing of layout links
$ rails generate integration_test site_layout

* The codes for integration test:
test/integration/site_layout_test.rb
require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
    get contact_path
    assert_select "title", full_title("Contact")
  end

end

* I added gem 'rails-controller-testing' in my Gemfile.

* I ran bundle install
$ bundle install

* I executed a Rake task for the integration test:
$ rails test:integration

* I ran a normal test to make sure it is pass.
$ rails test

* I added 'include ApplicationHelper'
in test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
  include ApplicationHelper
   
  # Add more helper methods to be used by all tests here...
end

* I added a line to test the title of the Contact page in
test/integration/site_layout_test.rb
assert_select "title", full_title("Contact")

* I wrote application_helper_test.rb to test the title is spelled correctly :
test/helpers/application_helper_test.rb
require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase
  test "full title helper" do
    assert_equal full_title,         "Ruby on Rails Tutorial Sample App"
    assert_equal full_title("Help"), "Help | Ruby on Rails Tutorial Sample App"
  end
end

* I created a Users controller with action 'new'
$ rails generate controller Users new

* I created a signup route:
config/routes.rb
get  '/signup',  to: 'users#new'

* I updated the signup_path for user controller test case:
test/controllers/users_controller_test.rb
get signup_path

* I created the link for Sign Up in home.html.erb:
app/views/static_pages/home.html.erb
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>

* I created a temporary stub view for the signup page:
app/views/users/new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<p>This will be a signup page for new users.</p>

* I merged my changes back into the master branch.
$ git add -A
$ git commit -m "Finish layout and routes"
$ git checkout master
$ git merge filling-in-layout

* I ran a test and made that sure the test is pass.
$ rails test

* I pushed the project to Github:
$ git push

* The result of the development: Home Page:

* The result of the developtment: Sign Up Page:

No comments:

Post a Comment

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again. In case of a Linux/Unit process hang, I have to figure out ...