Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TaskList Sinatra - KED & HRW #1

Open
wants to merge 26 commits into
base: KED-HRW/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
df2a90c
created file structure
kedevlin Nov 4, 2015
7d31e0b
Baseline requirements
kedevlin Nov 4, 2015
532ca18
add methods to tasksite.rb
yourFriendWes Nov 5, 2015
0728fc9
add create task methods
yourFriendWes Nov 5, 2015
65dd3e2
add redirect method and display tasks on homepage
kedevlin Nov 5, 2015
bc1ddc5
db change
kedevlin Nov 5, 2015
47284f6
experimented with ids
kedevlin Nov 5, 2015
4cd6780
add css styling - non-functional at this point
yourFriendWes Nov 5, 2015
0c03cdb
add more styling to mystie.css
yourFriendWes Nov 5, 2015
6d85aff
add code for first optional
kedevlin Nov 5, 2015
3b699b0
add complete task function via id variable
kedevlin Nov 5, 2015
379afa2
add currrent date to complete_task method
yourFriendWes Nov 5, 2015
2fff8bd
add bug fix to complete task methods
yourFriendWes Nov 6, 2015
8d80911
Added back Time.now for date in complete_task
kedevlin Nov 6, 2015
85057b2
added Delete button on home page
kedevlin Nov 6, 2015
01b1e4d
Added delete function
kedevlin Nov 6, 2015
b8e0036
Added confirm warning for delete
kedevlin Nov 6, 2015
7f4d7b7
Use Postgres in production (on Heroku)
Hamled Nov 6, 2015
e406851
add nonworking edit function
yourFriendWes Nov 6, 2015
2dbf99d
Fix autoincrement columns for Postgres
Hamled Nov 6, 2015
585cec6
Delete schema functionality
Hamled Nov 6, 2015
4c45623
Add edit functionality
kedevlin Nov 6, 2015
c2414d4
Merge in postgres branch
kedevlin Nov 10, 2015
04717c8
Added pg to gemfile
kedevlin Nov 10, 2015
f44f4f6
removed binding.pry
kedevlin Nov 10, 2015
12debdf
actually removed all binding.pry's
kedevlin Nov 10, 2015
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
16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# A sample Gemfile
source "https://rubygems.org"
ruby "2.2.3"
gem 'sinatra'
group :production do
gem 'pg'
end
group :development do
gem "pry"
gem 'sqlite3'
end

group :test do
gem "rspec"
end
# gem "rails"
47 changes: 47 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
diff-lcs (1.2.5)
method_source (0.8.2)
pg (0.18.3)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
rack-protection (1.5.3)
rack
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
slop (3.6.0)
sqlite3 (1.3.11)
tilt (2.0.1)

PLATFORMS
ruby

DEPENDENCIES
pg
pry
rspec
sinatra
sqlite3

BUNDLED WITH
1.10.6
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './tasks_site'
run Tasks
4 changes: 4 additions & 0 deletions create_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "./lib/database"

db = TaskList::Database.new("todo_list.db")
db.create_schema
74 changes: 70 additions & 4 deletions lib/database.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,81 @@
require "sqlite3"
require_relative "db_driver"

module TaskList
class Database

def initialize(db_name)
@db = SQLite3::Database.new(db_name)
@db = DBDriver.new(db_name)
end

def delete_schema
@db.delete_schema
end

def create_schema
# Put your ruby code here to use the @db variable
# to setup your schema in the databas.
@db.execute ('
CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NULL,
comp_date TEXT NULL
);')
end

def create_task(task, descr)
@db.execute('
INSERT INTO tasks (name, description)
VALUES(?, ?)
;', task, descr)
end

def get_current_tasks
@db.execute('
SELECT name, description, id, comp_date
FROM tasks
WHERE comp_date is NULL;
')
end

def get_completed_tasks
@db.execute('
SELECT name, description, id, comp_date
FROM tasks
WHERE comp_date IS NOT NULL;
')
end

def return_date(id)
@db.execute('
SELECT comp_date
FROM tasks
WHERE id= ?;
',id)
end

def complete_task(id)
if return_date(id) == [[nil]]
date = Time.now.strftime("%m/%d/%Y")
@db.execute('
UPDATE tasks SET comp_date=
?
WHERE id= ?
;', date, id)
end
end

def delete_task(id)
@db.execute('
DELETE FROM tasks
WHERE id= ?
;', id)
end

def edit_task(id, name, description)
@db.execute('
UPDATE tasks
SET name = ?, description = ?
WHERE id= ?
;', name,description,id)
end
end
end
60 changes: 60 additions & 0 deletions lib/db_driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Figure out which DB we're using
DB_DRIVER = (ENV["RACK_ENV"] == "production") ? :pg : :sqlite3

require DB_DRIVER.to_s

class DBDriver
def initialize(db_name)
case DB_DRIVER
when :sqlite3
@db = SQLite3::Database.new(db_name)
when :pg
@db = PG::Connection.new(ENV["DATABASE_URL"])
end
end

def execute(query, *args)
case DB_DRIVER
when :sqlite3
return @db.execute(query, *args)
when :pg
# First we need to transform the bound variable placeholders
# because SQLite uses ? and PG uses $1, $2, $3, etc.
# HOWEVER
# We can't actually parse this SQL query! So I'm just going to
# split on each ? and combine them again with the PG style.
# WARNING
# Because I can't parse the SQL query, we might end up replacing
# literal question marks with the variable bind code. :( :( :(
var_num = 0
query = query.split("?").reduce("") do |fixed, orig|
var_str = var_num > 0 ? "$#{var_num}" : ""
var_num += 1

next fixed + var_str + orig
end

# We also need to make sure that we use PG's sequence data type
# because unlike SQLite, it does not integer make primary keys
# autoincrement by default.
query.gsub!(/integer primary key/i, "SERIAL PRIMARY KEY")

return @db.exec_params(query, args).values
end
end

def delete_schema
case DB_DRIVER
when :sqlite3
db_file = @db.database_list[0][2] # maybe there's a better way?
@db.close unless @db.closed?

File.delete(db_file)
when :pg
@db.exec('
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
')
end
end
end
42 changes: 42 additions & 0 deletions public/css/mysite.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
body {
background-color: white;
width: 960px;
margin: 0 auto;
text-align: left;
font-family: "Raleway",serif;
}

header { background-color: #EB1D34;
color: white;
text-align: center;
font-weight: 600;
font-family: "Raleway",serif;
font-size: 30pt
}

h1 {
padding: 10px;
margin: 10px;}

fieldset {
padding: 50px;
margin: 10px;
}

fieldset input {
padding: 0 40px;
margin: 20px;
}

ul {
list-style: none;
padding: 10px;
margin: 10px;
text-align: left;
font-size: 20pt;
font-weight: 600;
}

li {
padding: 10px 0;
}
67 changes: 67 additions & 0 deletions tasks_site.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "sinatra"
require "./lib/database"

def current_db
@curr_db ||= TaskList::Database.new("todo_list.db")
end

class Tasks < Sinatra::Base
get "/" do
@title = "Your Task List"
@current_tasks = current_db.get_current_tasks
@completed_tasks = current_db.get_completed_tasks
erb :index
end

post "/" do
@submit = params[:submit]
if @submit == "Complete Task(s)"
puts "the complete part of the if"
complete_ids = params[:checked]
complete_ids.each do |id|
current_db.complete_task(id)
end
redirect to('/')
elsif @submit == "Delete Task(s)"
puts "the delete part of the if"
delete_ids = params[:checked]
delete_ids.each do |id|
current_db.delete_task(id)
end
redirect to('/')
else
puts "first part of edit"
@edit_id = params[:checked]
puts "second part of edit"
redirect to('/edit')
end
end

get "/edit" do
@title = "Edit task"
erb :edit
end

post "/edit" do
@title = "Add a new task"
id = params[:task_id]
name = params[:task]
description = params[:description]
current_db.edit_task(id, name, description)
redirect to('/')
end

get "/new" do
@title = "Add a new task"
erb :new
end

post "/new" do
@title = "Add a new task"
@new_task = params[:task]
@descr = params[:description]
@date = params[:date]
current_db.create_task(@new_task, @descr)
redirect to('/')
end
end
Binary file added todo_list.db
Binary file not shown.
11 changes: 11 additions & 0 deletions views/edit.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form method=post >
<fieldset>
<legend> Edit task</legend>
<label for="task">Task name: </label>
<input type="text" id="task" name="task" required >
<label for= "description">Description:</label>
<input type= "text" id = "description" name = "description">
<input type="hidden" name="task_id" id="task_id" value="<%= @edit_id%>">
<input type="submit" name="submit" id="submit" value="Edit Task">
</fieldset>
</form>
28 changes: 28 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Current Tasks</h1>
<form method="post">
<% @current_tasks.each do |task| %>
<input type="checkbox" name="checked[]" value="<%= task[2] %>">
<%= task[0] %>: <%= task[1] %> <%= task[3] %>
<br>
<% end %>
<h1>Completed Tasks</h1>
<% @completed_tasks.each do |task| %>
<input type="checkbox" name="checked[]" value="<%= task[2] %>">
<%= task[0] %>: <%= task[1] %> <%= task[3] %>
<br>
<% end %>
<br>
<input type="submit" name="submit" value="Complete Task(s)">
<br>
<br>
<input type="submit" onclick="return confirm('Are you sure?')" name="submit" value="Delete Task(s)">
<br>
<br>
<input type="submit" onclick="return confirm('Make sure you are only editing one thing at a time!')" name="submit" value="Edit Task">
</form>
<br>




<a href="/new" >Add Task </a>
20 changes: 20 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<title><%=@title%></title>
<link href="css/mysite.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Raleway:600,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
<meta charset = "utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<header>
<h1 class="page_title"><%=@title%></h1>

</header>

<body>
<%= yield %>
</body>

</html>
Loading