Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down