From 22b1e769b238b16a0e5a677049637be69e63c8a6 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Mon, 30 Dec 2024 00:09:54 +0800 Subject: [PATCH] finish --- Gemfile | 2 + Gemfile.lock | 43 +++++++++++ Guardfile | 73 +++++++++++++++++++ app/controllers/static_pages_controller.rb | 3 + app/views/layouts/application.html.erb | 3 +- app/views/static_pages/about.html.erb | 13 +++- app/views/static_pages/contact.html.erb | 8 ++ app/views/static_pages/help.html.erb | 11 ++- app/views/static_pages/home.html.erb | 10 ++- config/routes.rb | 3 +- .../static_pages_controller_test.rb | 21 ++++++ test/test_helper.rb | 2 + 12 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 Guardfile create mode 100644 app/views/static_pages/contact.html.erb diff --git a/Gemfile b/Gemfile index 25c0933..e19aa18 100644 --- a/Gemfile +++ b/Gemfile @@ -54,10 +54,12 @@ end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] gem "web-console" + gem 'guard', '~> 2.19' end group :test do # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing] gem "capybara" gem "selenium-webdriver" + gem "minitest-reporters" end diff --git a/Gemfile.lock b/Gemfile.lock index 4b458fd..7a7f50d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -74,6 +74,7 @@ GEM uri (>= 0.13.1) addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) + ansi (1.5.0) ast (2.4.2) base64 (0.2.0) bcrypt_pbkdf (1.1.1) @@ -96,6 +97,7 @@ GEM rack-test (>= 0.6.3) regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) + coderay (1.1.3) concurrent-ruby (1.3.4) connection_pool (2.4.1) crass (1.0.6) @@ -109,11 +111,29 @@ GEM erubi (1.13.1) et-orbi (1.2.11) tzinfo + ffi (1.17.0-aarch64-linux-gnu) + ffi (1.17.0-aarch64-linux-musl) + ffi (1.17.0-arm-linux-gnu) + ffi (1.17.0-arm-linux-musl) + ffi (1.17.0-arm64-darwin) + ffi (1.17.0-x86_64-darwin) + ffi (1.17.0-x86_64-linux-gnu) + ffi (1.17.0-x86_64-linux-musl) + formatador (1.1.0) fugit (1.11.1) et-orbi (~> 1, >= 1.2.11) raabro (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) + guard (2.19.0) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + lumberjack (>= 1.0.12, < 2.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.13.0) + shellany (~> 0.0) + thor (>= 0.18.1) i18n (1.14.6) concurrent-ruby (~> 1.0) importmap-rails (2.1.0) @@ -140,10 +160,14 @@ GEM thor (~> 1.3) zeitwerk (>= 2.6.18, < 3.0) language_server-protocol (3.17.0.3) + listen (3.9.0) + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) logger (1.6.4) loofah (2.23.1) crass (~> 1.0.2) nokogiri (>= 1.12.0) + lumberjack (1.2.10) mail (2.8.1) mini_mime (>= 0.1.1) net-imap @@ -151,9 +175,16 @@ GEM net-smtp marcel (1.0.4) matrix (0.4.2) + method_source (1.1.0) mini_mime (1.1.5) minitest (5.25.4) + minitest-reporters (1.7.1) + ansi + builder + minitest (>= 5.0) + ruby-progressbar msgpack (1.7.5) + nenv (0.3.0) net-imap (0.5.4) date net-protocol @@ -185,6 +216,9 @@ GEM racc (~> 1.4) nokogiri (1.18.0-x86_64-linux-musl) racc (~> 1.4) + notiffany (0.1.3) + nenv (~> 0.1) + shellany (~> 0.0) ostruct (0.6.1) parallel (1.26.3) parser (3.3.6.0) @@ -195,6 +229,9 @@ GEM activesupport (>= 7.0.0) rack railties (>= 7.0.0) + pry (0.15.2) + coderay (~> 1.1) + method_source (~> 1.0) psych (5.2.2) date stringio @@ -241,6 +278,9 @@ GEM zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.2.1) + rb-fsevent (0.11.2) + rb-inotify (0.11.1) + ffi (~> 1.0) rdoc (6.10.0) psych (>= 4.0.0) regexp_parser (2.10.0) @@ -284,6 +324,7 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) + shellany (0.0.1) solid_cable (3.0.5) actioncable (>= 7.2) activejob (>= 7.2) @@ -364,9 +405,11 @@ DEPENDENCIES brakeman capybara debug + guard (~> 2.19) importmap-rails jbuilder kamal + minitest-reporters propshaft puma (>= 5.0) rails (~> 8.0.1) diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..2bd369a --- /dev/null +++ b/Guardfile @@ -0,0 +1,73 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +## Uncomment and set this to only include directories you want to watch +# directories %w(app lib config test spec features) \ +# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")} + +## Note: if you are using the `directories` clause above and you are not +## watching the project directory ('.'), then you will want to move +## the Guardfile to a watched dir and symlink it back, e.g. +# +# $ mkdir config +# $ mv Guardfile config/ +# $ ln -s config/Guardfile . +# +# and, you'll have to watch "config/Guardfile" instead of "Guardfile" + +require 'active_support/core_ext/string' # Defines the matching rules for Guard. +guard :minitest, spring: "bin/rails test", all_on_start: false do + watch(%r{^test/(.*)/?(.*)_test\.rb$}) + watch('test/test_helper.rb') { 'test' } + watch('config/routes.rb') { interface_tests } + watch(%r{app/views/layouts/*}) { interface_tests } + watch(%r{âpp/models/(.*?)\.rb$}) do |matches| + ["test/models/#{matches[1]}_test.rb", "test/integration/microposts_interface_test.rb"] + end + watch(%r{^test/fixtures/(.*?)\.yml$}) do |matches| + "test/models/#{matches[1].singularize}_test.rb" + end + watch(%r{âpp/mailers/(.*?)\.rb$}) do |matches| + "test/mailers/#{matches[1]}_test.rb" + end + watch(%r{âpp/views/(.*)_mailer/.*$}) do |matches| + "test/mailers/#{matches[1]}_mailer_test.rb" + end + watch(%r{âpp/controllers/(.*?)_controller\.rb$}) do |matches| + resource_tests(matches[1]) + end + watch(%r{âpp/views/([^/]*?)/.*\.html\.erb$}) do |matches| + ["test/controllers/#{matches[1]}_controller_test.rb"] + integration_tests(matches[1]) + end + watch(%r{âpp/helpers/(.*?)_helper\.rb$}) do |matches| integration_tests(matches[1]) + end + watch('app/views/layouts/application.html.erb') do 'test/integration/site_layout_test.rb' + end + watch('app/helpers/sessions_helper.rb') do integration_tests << 'test/helpers/sessions_helper_test.rb' + end + watch('app/controllers/sessions_controller.rb') do ['test/controllers/sessions_controller_test.rb', 'test/integration/users_login_test.rb'] + end + watch('app/controllers/account_activations_controller.rb') do 'test/integration/users_signup_test.rb' + end + watch(%r{app/views/users/*}) do resource_tests('users') + ['test/integration/microposts_interface_test.rb'] + end +end # Returns the integration tests corresponding to the given resource. + + +def integration_tests(resource = :all) + if resource == :all + Dir["test/integration/*"] + else + Dir["test/integration/#{resource}_*.rb"] + end +end # Returns all tests that hit the interface. +def + interface_tests + integration_tests << "test/controllers" +end # Returns the controller tests corresponding to the given resource. +def + controller_test(resource) "test/controllers/#{resource}_controller_test.rb" +end # Returns all tests for the given resource. +def + resource_tests(resource) integration_tests(resource) << controller_test(resource) +end \ No newline at end of file diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 19f79a9..d304760 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -7,4 +7,7 @@ class StaticPagesController < ApplicationController def about end + + def contact + end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 628fe35..2b1b993 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,8 @@ - <%= content_for(:title) || "Sample App" %> + + <%= yield(:title) %> | Ruby on Rails Tutorial Sample App diff --git a/app/views/static_pages/about.html.erb b/app/views/static_pages/about.html.erb index be7cbc1..daced2b 100644 --- a/app/views/static_pages/about.html.erb +++ b/app/views/static_pages/about.html.erb @@ -1,2 +1,11 @@ -

StaticPages#about

-

Find me in app/views/static_pages/about.html.erb

+<% provide(:title, "About") %> +

About

+

+ The Ruby on Rails Tutorial + , part of the Learn Enough family of + tutorials, is a book and + screencast series + to teach web development with Ruby on Rails. + This is the sample app for the tutorial. +

+ diff --git a/app/views/static_pages/contact.html.erb b/app/views/static_pages/contact.html.erb new file mode 100644 index 0000000..8518592 --- /dev/null +++ b/app/views/static_pages/contact.html.erb @@ -0,0 +1,8 @@ +<% provide(:title, "Contact") %> +

Contact

+

+ Contact the Ruby on Rails Tutorial about the sample app at the + contact page. + +

+ diff --git a/app/views/static_pages/help.html.erb b/app/views/static_pages/help.html.erb index 61896f5..33a5cfe 100644 --- a/app/views/static_pages/help.html.erb +++ b/app/views/static_pages/help.html.erb @@ -1,2 +1,9 @@ -

StaticPages#help

-

Find me in app/views/static_pages/help.html.erb

+<% provide(:title, "Help") %> +

Help

+

+ Get help on the Ruby on Rails Tutorial at the + Rails Tutorial help page. + To get help on this sample app, see the + Ruby on Rails Tutorial book. +

+ diff --git a/app/views/static_pages/home.html.erb b/app/views/static_pages/home.html.erb index af94c7f..5a4284e 100644 --- a/app/views/static_pages/home.html.erb +++ b/app/views/static_pages/home.html.erb @@ -1,2 +1,8 @@ -

StaticPages#home

-

Find me in app/views/static_pages/home.html.erb

+<% provide(:title, "Home") %> +

Sample App

+

+ This is the home page for the + Ruby on Rails Tutorial + sample application. +

+ diff --git a/config/routes.rb b/config/routes.rb index 43610ec..c44de3a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,8 +2,9 @@ Rails.application.routes.draw do get "static_pages/home" get "static_pages/help" get "static_pages/about" + get "static_pages/contact" - root "application#hello" + root "static_pages#home" # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/test/controllers/static_pages_controller_test.rb b/test/controllers/static_pages_controller_test.rb index b336317..6d5eeb0 100644 --- a/test/controllers/static_pages_controller_test.rb +++ b/test/controllers/static_pages_controller_test.rb @@ -1,19 +1,40 @@ 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 + assert_select "title", "Home | #{@base_title}" + end + test "should get home" do get static_pages_home_url assert_response :success + assert_select "title", "Home | #{@base_title}" + assert_select "h1", "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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 0c22470..3daa94e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,8 @@ ENV["RAILS_ENV"] ||= "test" require_relative "../config/environment" require "rails/test_help" +require "minitest/reporters" +Minitest::Reporters.use! module ActiveSupport class TestCase