diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb new file mode 100644 index 000000000..96476dfa3 --- /dev/null +++ b/app/controllers/books_controller.rb @@ -0,0 +1,22 @@ +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 + 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_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 diff --git a/app/models/book.rb b/app/models/book.rb new file mode 100644 index 000000000..ff28872ae --- /dev/null +++ b/app/models/book.rb @@ -0,0 +1,35 @@ +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 |page| + self.new(title, File.basename(page, '.*')) + end + end + + def exist?(title, page) + page.nil? ? + self.find(title).any? : + self.find(title).map(&:filename).include?(page + ".html") + end + end + + def path + "#{DIR_PATH}/#{self.title}/#{self.filename}" + end + + def exist? + Book.find(self.title).map(&:filename).include?(self.filename) + end +end 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 diff --git a/config/routes.rb b/config/routes.rb index 86cb4a422..a999d47cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -96,12 +96,12 @@ 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 "/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" # Check development sent emails mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?