From be61cae9031c89d0daba48c240b0b0be657c628c Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 14:42:03 +0900 Subject: [PATCH 1/7] Add Book model as PORO --- app/models/book.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/models/book.rb diff --git a/app/models/book.rb b/app/models/book.rb new file mode 100644 index 000000000..57f39c25e --- /dev/null +++ b/app/models/book.rb @@ -0,0 +1,33 @@ +class Book + attr_reader :title, :filename + DIR_PATH = 'app/views/books' + + def initialize(title, filename) + @title = title + @filename = filename + end + + class << self + def all + Dir.glob("#{DIR_PATH}/*").sort + end + + def find(title) + Dir.glob("#{DIR_PATH}/#{title}/*.html.erb").sort.map do |filename| + self.new(title, File.basename(filename, '.*')) + end + end + + def exist?(title, filename) + self.find(title).map(&:filename).include?(filename) + end + end + + def path + "#{DIR_PATH}/#{self.title}/#{self.filename}" + end + + def exist? + Book.find(self.title).map(&:filename).include?(self.filename) + end +end From 89a5a285e9ac9cf956cfdbaf2c8c30bd690f603b Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 14:42:49 +0900 Subject: [PATCH 2/7] Rename sotechsha[2] resources as Books in View --- app/views/{sotechsha_pages => books/sotechsha1}/0.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/1.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/2.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/3.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/4.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/5.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/6.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/gazou.html.erb | 0 app/views/{sotechsha_pages => books/sotechsha1}/index.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/0.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/1.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/2.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/3.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/4.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/5.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/6.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/gazou.html.erb | 0 app/views/{sotechsha2_pages => books/sotechsha2}/index.html.erb | 0 18 files changed, 0 insertions(+), 0 deletions(-) rename app/views/{sotechsha_pages => books/sotechsha1}/0.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/1.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/2.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/3.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/4.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/5.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/6.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/gazou.html.erb (100%) rename app/views/{sotechsha_pages => books/sotechsha1}/index.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/0.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/1.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/2.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/3.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/4.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/5.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/6.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/gazou.html.erb (100%) rename app/views/{sotechsha2_pages => books/sotechsha2}/index.html.erb (100%) diff --git a/app/views/sotechsha_pages/0.html.erb b/app/views/books/sotechsha1/0.html.erb similarity index 100% rename from app/views/sotechsha_pages/0.html.erb rename to app/views/books/sotechsha1/0.html.erb diff --git a/app/views/sotechsha_pages/1.html.erb b/app/views/books/sotechsha1/1.html.erb similarity index 100% rename from app/views/sotechsha_pages/1.html.erb rename to app/views/books/sotechsha1/1.html.erb diff --git a/app/views/sotechsha_pages/2.html.erb b/app/views/books/sotechsha1/2.html.erb similarity index 100% rename from app/views/sotechsha_pages/2.html.erb rename to app/views/books/sotechsha1/2.html.erb diff --git a/app/views/sotechsha_pages/3.html.erb b/app/views/books/sotechsha1/3.html.erb similarity index 100% rename from app/views/sotechsha_pages/3.html.erb rename to app/views/books/sotechsha1/3.html.erb diff --git a/app/views/sotechsha_pages/4.html.erb b/app/views/books/sotechsha1/4.html.erb similarity index 100% rename from app/views/sotechsha_pages/4.html.erb rename to app/views/books/sotechsha1/4.html.erb diff --git a/app/views/sotechsha_pages/5.html.erb b/app/views/books/sotechsha1/5.html.erb similarity index 100% rename from app/views/sotechsha_pages/5.html.erb rename to app/views/books/sotechsha1/5.html.erb diff --git a/app/views/sotechsha_pages/6.html.erb b/app/views/books/sotechsha1/6.html.erb similarity index 100% rename from app/views/sotechsha_pages/6.html.erb rename to app/views/books/sotechsha1/6.html.erb diff --git a/app/views/sotechsha_pages/gazou.html.erb b/app/views/books/sotechsha1/gazou.html.erb similarity index 100% rename from app/views/sotechsha_pages/gazou.html.erb rename to app/views/books/sotechsha1/gazou.html.erb diff --git a/app/views/sotechsha_pages/index.html.erb b/app/views/books/sotechsha1/index.html.erb similarity index 100% rename from app/views/sotechsha_pages/index.html.erb rename to app/views/books/sotechsha1/index.html.erb diff --git a/app/views/sotechsha2_pages/0.html.erb b/app/views/books/sotechsha2/0.html.erb similarity index 100% rename from app/views/sotechsha2_pages/0.html.erb rename to app/views/books/sotechsha2/0.html.erb diff --git a/app/views/sotechsha2_pages/1.html.erb b/app/views/books/sotechsha2/1.html.erb similarity index 100% rename from app/views/sotechsha2_pages/1.html.erb rename to app/views/books/sotechsha2/1.html.erb diff --git a/app/views/sotechsha2_pages/2.html.erb b/app/views/books/sotechsha2/2.html.erb similarity index 100% rename from app/views/sotechsha2_pages/2.html.erb rename to app/views/books/sotechsha2/2.html.erb diff --git a/app/views/sotechsha2_pages/3.html.erb b/app/views/books/sotechsha2/3.html.erb similarity index 100% rename from app/views/sotechsha2_pages/3.html.erb rename to app/views/books/sotechsha2/3.html.erb diff --git a/app/views/sotechsha2_pages/4.html.erb b/app/views/books/sotechsha2/4.html.erb similarity index 100% rename from app/views/sotechsha2_pages/4.html.erb rename to app/views/books/sotechsha2/4.html.erb diff --git a/app/views/sotechsha2_pages/5.html.erb b/app/views/books/sotechsha2/5.html.erb similarity index 100% rename from app/views/sotechsha2_pages/5.html.erb rename to app/views/books/sotechsha2/5.html.erb diff --git a/app/views/sotechsha2_pages/6.html.erb b/app/views/books/sotechsha2/6.html.erb similarity index 100% rename from app/views/sotechsha2_pages/6.html.erb rename to app/views/books/sotechsha2/6.html.erb diff --git a/app/views/sotechsha2_pages/gazou.html.erb b/app/views/books/sotechsha2/gazou.html.erb similarity index 100% rename from app/views/sotechsha2_pages/gazou.html.erb rename to app/views/books/sotechsha2/gazou.html.erb diff --git a/app/views/sotechsha2_pages/index.html.erb b/app/views/books/sotechsha2/index.html.erb similarity index 100% rename from app/views/sotechsha2_pages/index.html.erb rename to app/views/books/sotechsha2/index.html.erb From 36f29e63e7fdc5dd15f3dab85da53bf95d670099 Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 15:40:51 +0900 Subject: [PATCH 3/7] Refactor: Merge sotechsha[2] into Book resources --- app/controllers/books_controller.rb | 21 +++++++++++++++++++++ app/models/book.rb | 4 +++- config/routes.rb | 15 +++++++++------ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 app/controllers/books_controller.rb diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb new file mode 100644 index 000000000..79b9f767f --- /dev/null +++ b/app/controllers/books_controller.rb @@ -0,0 +1,21 @@ +class BooksController < ApplicationController + # NOTE: The following URLs are hard-coded by published books. + # And the books are just a few, so not a big problem for now. + + # GET /sotechsha[2] + def sotechsha1_index; render("books/sotechsha1/index"); end + def sotechsha2_index; render("books/sotechsha2/index"); end + + # GET /sotechsha[2]/:page + def sotechsha1_show; render_book_page(params); end + def sotechsha2_show; render_book_page(params); end + + private + + def render_book_page(params) + book_name = params[:action].split('_').first + Book.exist?(book_name, params[:page]) ? + render("books/#{book_name}/#{params[:page]}") : + redirect_to('/' + book_name) # TODO: Show flash + end +end diff --git a/app/models/book.rb b/app/models/book.rb index 57f39c25e..17dd6c574 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -19,7 +19,9 @@ def find(title) end def exist?(title, filename) - self.find(title).map(&:filename).include?(filename) + filename.nil? ? + self.find(title).any? : + self.find(title).map(&:filename).include?(filename + ".html") end end diff --git a/config/routes.rb b/config/routes.rb index 86cb4a422..4b8da8825 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -96,12 +96,15 @@ get "/.well-known/acme-challenge/:id" => "static_pages#lets_encrypt" get "/.well-known/security.txt" => "static_pages#security" - # CoderDojo Books from Sotechsha - get "/sotechsha" => "sotechsha_pages#index" - get "/sotechsha/:page" => "sotechsha_pages#show" - - get "/sotechsha2" => "sotechsha2_pages#index" - get "/sotechsha2/:page" => "sotechsha2_pages#show" + # CoderDojo Books such as published from ソーテック社 + #get "/sotechsha" => "sotechsha_pages#index" + #get "/sotechsha/:page" => "sotechsha_pages#show" + get "/sotechsha1", to: redirect('/sotechsha') + get "/sotechsha" => "books#sotechsha1_index" + get "/sotechsha/:page" => "books#sotechsha1_show" + get "/sotechsha2" => "books#sotechsha2_index" + get "/sotechsha2/:page" => "books#sotechsha2_show" + #get "/sotechsha/sozai", to: redirect('/sotechsha/gazou') # Check development sent emails mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development? From 4cb34aeac0349b817231df60b9966188ba35740e Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 15:46:07 +0900 Subject: [PATCH 4/7] Add link to the PR on why Books resources created as a comment --- app/controllers/books_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 79b9f767f..6d4e53cf0 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -1,6 +1,7 @@ class BooksController < ApplicationController # NOTE: The following URLs are hard-coded by published books. # And the books are just a few, so not a big problem for now. + # https://github.com/coderdojo-japan/coderdojo.jp/pull/1696 # GET /sotechsha[2] def sotechsha1_index; render("books/sotechsha1/index"); end From 51d5723e1aa870566c1c5f1a6bb78344fac6c0a1 Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 15:52:29 +0900 Subject: [PATCH 5/7] Use more familiar var name in this context: filename -> page --- app/models/book.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/book.rb b/app/models/book.rb index 17dd6c574..ff28872ae 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -13,15 +13,15 @@ def all end def find(title) - Dir.glob("#{DIR_PATH}/#{title}/*.html.erb").sort.map do |filename| - self.new(title, File.basename(filename, '.*')) + Dir.glob("#{DIR_PATH}/#{title}/*.html.erb").sort.map do |page| + self.new(title, File.basename(page, '.*')) end end - def exist?(title, filename) - filename.nil? ? + def exist?(title, page) + page.nil? ? self.find(title).any? : - self.find(title).map(&:filename).include?(filename + ".html") + self.find(title).map(&:filename).include?(page + ".html") end end From 6fcc7a6ffd4ef2f038000ea1d7cdeec3aabeb9b3 Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 15:52:48 +0900 Subject: [PATCH 6/7] Remove no-longer needed routings for /sotechsha[2] --- config/routes.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 4b8da8825..a999d47cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -97,14 +97,11 @@ get "/.well-known/security.txt" => "static_pages#security" # CoderDojo Books such as published from ソーテック社 - #get "/sotechsha" => "sotechsha_pages#index" - #get "/sotechsha/:page" => "sotechsha_pages#show" get "/sotechsha1", to: redirect('/sotechsha') get "/sotechsha" => "books#sotechsha1_index" get "/sotechsha/:page" => "books#sotechsha1_show" get "/sotechsha2" => "books#sotechsha2_index" get "/sotechsha2/:page" => "books#sotechsha2_show" - #get "/sotechsha/sozai", to: redirect('/sotechsha/gazou') # Check development sent emails mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development? From f3fa6d9743cc5e6791d2933030651ce645e39f0d Mon Sep 17 00:00:00 2001 From: Yohei Yasukawa Date: Sun, 25 May 2025 15:53:34 +0900 Subject: [PATCH 7/7] Show flash with friendly redirect for exceptioinal Book pages --- app/controllers/books_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 6d4e53cf0..96476dfa3 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -14,9 +14,9 @@ def sotechsha2_show; render_book_page(params); end private def render_book_page(params) - book_name = params[:action].split('_').first - Book.exist?(book_name, params[:page]) ? - render("books/#{book_name}/#{params[:page]}") : - redirect_to('/' + book_name) # TODO: Show flash + book_title = params[:action].split('_').first + Book.exist?(book_title, params[:page]) ? + render("books/#{book_title}/#{params[:page]}") : + redirect_to("/#{book_title}", flash: { warning: '該当するページが見つかりませんでした 💦'} ) end end