This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module TemplateView | |
| def self.home_view | |
| home_view = <<-HTML | |
| <html> | |
| <head> <title> Home </title> </head> | |
| <body> | |
| <form method=”post” action=”save_task”> | |
| Task Name: <input type=”text” name=”task”> | |
| <input type=”submit” value=”save” > | |
| </form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Todo | |
| class Application | |
| include TemplateView | |
| class << self | |
| def call(env) | |
| if(env[‘PATH_INFO’] == ‘/’) | |
| view = TemplateView.home_view | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require ‘erb’ | |
| require “sqlite3” | |
| DB = SQLite3::Database.new “test.db” | |
| rows = db.execute <<-SQL | |
| create table tasks ( name varchar(30), finished int ); | |
| SQL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| elsif(env[‘PATH_INFO’] == ‘/save_task’ && env[‘REQUEST_METHOD’] == “POST”) | |
| req = Rack::Request.new(env) | |
| task_name = req.params[“task”] | |
| DB.execute(“INSERT INTO tasks (name, finished) VALUES (?, ?)”, [task_name,0]) | |
| return [ 302, {‘Location’ =>”/”}, [] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <% DB.execute( “select rowid, * from tasks” ) do |row| %> | |
| <li><%= row[1] %> <% if(row[2] == 1) %>(finished) <% end %><a href=”/edit?id=<%= row[0]%>”>edit </a> <a href=”/delete?id=<%= row[0]%>”> del</a></li> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| elsif(env[‘PATH_INFO’] ==’/delete’) | |
| req = Rack::Request.new(env) id = req.params[“id”] DB.execute(“delete from tasks where rowid=’#{id}’”) | |
| return [ 302, {‘Location’ =>”/”}, [] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| elsif(env[‘PATH_INFO’] == ‘/edit’) | |
| req = Rack::Request.new(env) | |
| id = req.params[‘id’] | |
| view = TemplateView.edit_view(id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <% DB.execute(“select rowid, * from tasks where rowid = #{id}”) do |row| %> | |
| <form method=”post” action=”/update?id=<%= row[0]%>”> | |
| Task Name: <input type=”text” name=”task” value=”<%= row[1] %>”> | |
| Finished?: <input type=”checkbox” name=”finished” value=”1" <% if(row[2] == 1) %> checked <% end%>> | |
| <input type=”submit” value=”Update” > | |
| </form> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| elsif(env[‘PATH_INFO’] ==’/update’ && env[‘REQUEST_METHOD’] == “POST”) | |
| req = Rack::Request.new(env) | |
| id = req.params[“id”] | |
| task = req.params[“task”] | |
| finish = 0 | |
| if(req.params.include? ‘finished’) | |
| finish = 1 | |
| end | |
| DB.execute(“UPDATE tasks set name = ‘#{task}’, finished = ‘#{finish}’ where rowid=’#{id}’”) | |
| return [ 302, {‘Location’ =>”/”}, [] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source 'https://rubygems.org' | |
| gem 'rack' | |
| gem 'pg' | |
| gem 'puma' | |
| gem 'tilt' |
OlderNewer