Thursday, April 27, 2017

I learned the basics of Ruby language

Today, I finished up the chapter 4 of the book: Rails-flavored Ruby https://www.railstutorial.org/book/rails_flavored_ruby#cha-rails_flavored_ruby.

In refer to the instructions from the book:

* I created a separate topic branch "rails-flavored-ruby"
$ git checkout -b rails-flavored-ruby


 * I defined a custom helper called full_title, to solve the problem of a missing page title.
app/helpers/application_helper.rb
module ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title = '')
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      page_title + " | " + base_title
    end
  end
end

* I simplified my application layout
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' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

* I eliminated the code repeatation in Home, About and Contact for the test cases in the static pages controller:
test/controllers/static_pages_controller_test.rb
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  def setup
    @base_title = "Ruby on Rails Tutorial Sample App"
  end
   
  test "should get root" do
    get root_url
    assert_response :success
  end
   
    test "should get home" do
    get static_pages_home_url
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"       
    end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
    assert_select "title", "Help | #{@base_title}"
  end

  test "should get about" do
    get static_pages_about_url
    assert_response :success
    assert_select "title", "About | #{@base_title}"
  end
   
  test "should get contact" do
      get static_pages_contact_url
      assert_response :success
      assert_select "title", "Contact | #{@base_title}"
  end   
   
end

* And then, I read the introduction of Ruby programming language including methods, objects, class, def keyword, class keyword, HTML embedded Ruby ERB, data structures include arrays, ranges, hashes, inheritances, superclass, and etc. I did not do the Ruby programming exercises.

* After that, I commited the changes, and I merged the rails-flavoured-ruby branch to master:
$ git commit -am "Add a full_title helper"
$ git checkout master
$ git merge rails-flavored-ruby

* I did a sanity check before push:
$ rails test
5 runs, 9 assertions, 0 failures, 0 errors, 0 skips

* I pushed it up to Github - https://github.com/jimmy2046/sample_app:
$ git push

3 comments:

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 ...