Skip to content

Commit c5b2b76

Browse files
authored
Merge pull request #1696 from coderdojo-japan/refactor-sotechsha-as-books-resources
Refactor: `Sotechsha[2]` as `Books` resources
2 parents e2ed3be + f3fa6d9 commit c5b2b76

File tree

21 files changed

+63
-6
lines changed

21 files changed

+63
-6
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class BooksController < ApplicationController
2+
# NOTE: The following URLs are hard-coded by published books.
3+
# And the books are just a few, so not a big problem for now.
4+
# https://github.com/coderdojo-japan/coderdojo.jp/pull/1696
5+
6+
# GET /sotechsha[2]
7+
def sotechsha1_index; render("books/sotechsha1/index"); end
8+
def sotechsha2_index; render("books/sotechsha2/index"); end
9+
10+
# GET /sotechsha[2]/:page
11+
def sotechsha1_show; render_book_page(params); end
12+
def sotechsha2_show; render_book_page(params); end
13+
14+
private
15+
16+
def render_book_page(params)
17+
book_title = params[:action].split('_').first
18+
Book.exist?(book_title, params[:page]) ?
19+
render("books/#{book_title}/#{params[:page]}") :
20+
redirect_to("/#{book_title}", flash: { warning: '該当するページが見つかりませんでした 💦'} )
21+
end
22+
end

app/models/book.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Book
2+
attr_reader :title, :filename
3+
DIR_PATH = 'app/views/books'
4+
5+
def initialize(title, filename)
6+
@title = title
7+
@filename = filename
8+
end
9+
10+
class << self
11+
def all
12+
Dir.glob("#{DIR_PATH}/*").sort
13+
end
14+
15+
def find(title)
16+
Dir.glob("#{DIR_PATH}/#{title}/*.html.erb").sort.map do |page|
17+
self.new(title, File.basename(page, '.*'))
18+
end
19+
end
20+
21+
def exist?(title, page)
22+
page.nil? ?
23+
self.find(title).any? :
24+
self.find(title).map(&:filename).include?(page + ".html")
25+
end
26+
end
27+
28+
def path
29+
"#{DIR_PATH}/#{self.title}/#{self.filename}"
30+
end
31+
32+
def exist?
33+
Book.find(self.title).map(&:filename).include?(self.filename)
34+
end
35+
end

0 commit comments

Comments
 (0)