Skip to content

Instantly share code, notes, and snippets.

@komagata
Last active August 19, 2019 08:55
Show Gist options
  • Save komagata/f12131de58a2e2f7aa189c7599dcce47 to your computer and use it in GitHub Desktop.
Save komagata/f12131de58a2e2f7aa189c7599dcce47 to your computer and use it in GitHub Desktop.
Memoアプリ
require "sinatra"
require_relative "memo"
Memo.data_dir = "memos"
get "/" do
@memos = Memo.all
erb :index, layout: :layout
end
get "/new" do
erb :new, layout: :layout
end
get "/:id" do
@memo = Memo.find(params[:id])
erb :show, layout: :layout
end
get "/:id/edit" do
@memo = Memo.find(params[:id])
erb :edit, layout: :layout
end
post "/" do
@memo = Memo.new
@memo.body = params[:body]
@memo.create
redirect "/"
end
patch "/:id" do
@memo = Memo.find(params[:id])
@memo.body = params[:body]
@memo.save
redirect "/#{params[:id]}"
end
delete "/:id" do
@memo = Memo.find(params[:id])
@memo.destroy
redirect "/"
end
helpers do
def nl2br(text)
text.gsub("\n", "<br>")
end
end
<form action="/<%= @memo.id %>" method="post">
<input type="hidden" name="_method" value="patch">
<textarea name="body" cols="60" rows="20"><%= @memo.body %></textarea>
<p><input type="submit" value="変更"></p>
</form>
<ul>
<% @memos.each do |memo| %>
<li>
<a href="<%= memo.id %>"><%= memo.title %></a>
</li>
<% end %>
</ul>
<p><a href="/new">追加</a></p>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>メモアプリ</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" media="all" href="" />
</head>
</body>
<h1><a href="/">メモアプリ</a></h1>
<%= yield %>
</body>
</html>
require "pathname"
require "securerandom"
class Memo
attr_accessor :id, :title, :body, :created_at, :updated_at
def self.data_dir=(data_dir)
@@data_dir = data_dir
end
def self.data_dir
@@data_dir
end
def self.new_from_path(path)
memo = Memo.new
File.open(path) do |f|
memo.id = File.basename(f, ".txt")
memo.body = f.read
memo.created_at = f.birthtime
memo.updated_at = f.mtime
end
memo
end
def self.find(id)
self.new_from_path("#{self.data_dir}/#{id}.txt")
end
def self.all
Pathname.glob("#{self.data_dir}/*.txt").map do |f|
self.new_from_path(f)
end
end
def self.titles
self.all.map(&:title)
end
def create
@id = generate_id
save
end
def save
File.open("#{@@data_dir}/#{@id}.txt", "w") do |f|
f.write @body
end
end
def destroy
File.delete "#{@@data_dir}/#{@id}.txt"
end
def generate_id
titles = Memo.titles
id = SecureRandom.urlsafe_base64
while titles.include?(id) do
id = SecureRandom.urlsafe_base64
end
id
end
def title
@body.chomp.split("\n")&.first || "タイトルなし"
end
end
<form action="/" method="post">
<textarea name="body" cols="60" rows="20"></textarea>
<p><input type="submit" value="追加"></p>
</form>
<%= nl2br(@memo.body) %>
<p><a href="/<%= @memo.id %>/edit">変更</a></p>
<p>
<form action="/<%= @memo.id %>" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="削除">
</form>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment