Permalink
Browse files

adding fancy-buttons

  • Loading branch information...
igorgue committed Nov 21, 2009
1 parent 3e252e6 commit 2ab699813891dee9a0d4aef333b8b91fd04907ba
Showing with 151 additions and 111 deletions.
  1. +94 −18 app.rb
  2. +2 −0 config.ru
  3. +0 −72 controllers/entries.rb
  4. +36 −0 media/css/buttons.css
  5. BIN media/images/button_bg.png
  6. +4 −3 views/edit.erb
  7. +5 −5 views/index.erb
  8. +3 −4 views/new.erb
  9. +4 −4 views/show.erb
  10. +1 −2 views/status_404.erb
  11. +2 −3 views/status_500.erb
View
112 app.rb
@@ -3,26 +3,102 @@
require 'sinatra'
require 'sequel'
-configure do
- DB = Sequel.sqlite('blog.db')
+module SinatraBlog
+ class App < Sinatra::Default
+ dir = File.dirname(File.expand_path(__FILE__))
- DB.create_table? :posts do
- primary_key :id
- varchar :title
- varchar :body
- end
+ set :public, "#{dir}/media"
+ set :static, true
- DB.create_table? :tags do
- primary_key :id
- varchar :title
- end
+ configure do
+ DB = Sequel.sqlite('blog.db')
- load "models/blog.rb"
-end
+ DB.create_table? :posts do
+ primary_key :id
+ varchar :title
+ varchar :body
+ end
-helpers do
- include Rack::Utils
- alias :unsafe :escape_html
-end
+ DB.create_table? :tags do
+ primary_key :id
+ varchar :title
+ end
+
+ load "models/blog.rb"
+ end
+
+ helpers do
+ include Rack::Utils
+ alias :unsafe :escape_html
+ end
+
+ # index
+ get '/' do
+ @posts = Post.all
+
+ erb :index
+ end
+
+ # new post form
+ get '/posts/new' do
+ erb :new
+ end
+
+ get '/posts/:id?' do
+ @post = Post.find :id => params[:id]
+
+ if @post
+ if params[:edit] == "true"
+ erb :edit
+ else
+ erb :show
+ end
+ else
+ redirect "/"
+ end
+ end
+
+ # create a new post
+ post '/posts' do
+ post = Post.find_or_create :title => params[:title], :body => params[:body]
-load 'controllers/entries.rb'
+ if post
+ redirect "/posts/#{post.id}"
+ else
+ redirect "/"
+ end
+ end
+
+ put '/posts/:id' do
+ post = Post.find :id => params[:id]
+
+ if post
+ post.title = params[:title]
+ post.body = params[:body]
+
+ post.save
+ end
+
+ redirect "/posts/#{params[:id]}"
+ end
+
+ delete '/posts/:id' do
+ post = Post.find :id => params[:id]
+
+ if post
+ post.destroy
+ end
+
+ redirect "/"
+ end
+
+ # error handling
+ error do
+ erb :status_500
+ end
+
+ not_found do
+ erb :status_404
+ end
+ end
+end
View
@@ -0,0 +1,2 @@
+require "app"
+run SinatraBlog::App
View
@@ -1,72 +0,0 @@
-# controllers/entries.rb
-
-# index
-get '/' do
- @posts = Post.all
-
- erb :index
-end
-
-# new post form
-get '/posts/new' do
- erb :new
-end
-
-# edit post form
-get '/posts/edit/:id' do
- @post = Post.find :id => params[:id]
-
- if @post
- erb :edit
- else
- redirect "/"
- end
-end
-
-get '/posts/:id' do
- @post = Post.find :id => params[:id]
-
- if @post
- erb :show
- else
- redirect "/"
- end
-end
-
-put '/posts' do
- Post.find_or_create :title => params[:title], :body => params[:body]
-
- redirect "/"
-end
-
-post '/posts/:id' do
- post = Post.find :id => params[:id]
-
- if post
- post.title = params[:title]
- post.body = params[:body]
-
- post.save
- end
-
- redirect "/"
-end
-
-delete '/posts/:id' do
- post = Post.find :id => params[:id]
-
- if post
- post.destroy
- end
-
- redirect "/"
-end
-
-# error handling
-error do
- erb :status_500
-end
-
-not_found do
- erb :status_404
-end
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
Binary file not shown.
View
@@ -1,16 +1,17 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
-"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+ <link rel="stylesheet" href="/css/buttons.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
<h1>Edit <%= @post.title %></h1>
<form action="/posts/<%= @post.id %>" method="post" accept-charset="utf-8">
+ <input type="hidden" name="_method" value="put">
<p>Title: <input name="title" type="text" value="<%= @post.title %>"></p>
<p>Body: <textarea name="body"><%= @post.body %></textarea></p>
- <p><input type="submit" value="Save"></p>
+ <button type="submit" class="green">save</button>
</form>
</body>
</html>
View
@@ -1,8 +1,8 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
-"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+ <link rel="stylesheet" href="/css/buttons.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
<h1>Superblog!</h1>
@@ -12,14 +12,14 @@
<h2><a href="/posts/<%= post.id %>"><%= post.title %></a></h2>
<p><%= unsafe post.body %></p>
- <form name="delete_<%= post.id %>" action="/posts/<%= post.id %>" method="post" accept-charset="utf-8">
+ <form name="delete" action="/posts/<%= post.id %>" method="post" accept-charset="utf-8">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="id" value="<%= post.id %>">
- <p><a href="javascript:document.delete_<%= post.id %>.submit()">✖ delete</a>?</p>
+ <button type="submit" value="delete">✖ delete</button>
</form>
<% end %>
<hr>
- <p>Add new entries <a href="/posts/new">here</a>.</p>
+ <p><a class="button green" href="/posts/new">+ new post</a></p>
</body>
</html>
View
@@ -1,17 +1,16 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
-"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+ <link rel="stylesheet" href="/css/buttons.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
<h1>Add new entry</h1>
<form action="/posts" method="post" accept-charset="utf-8">
- <input type="hidden" name="_method" value="put">
<p>Title: <input name="title" type="text" value="">
<p>Body: <textarea name="body" value=""></textarea>
- <p><input type="submit" value="Add"></p>
+ <p><button type="submit" class="green">save</button></p>
</form>
</body>
</html>
View
@@ -1,8 +1,8 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
-"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+ <link rel="stylesheet" href="/css/buttons.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
<h1><%= @post.title %></h1>
@@ -12,8 +12,8 @@
<form name="delete_<%= @post.id %>" action="/posts/<%= @post.id %>" method="post" accept-charset="utf-8">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="id" value="<%= @post.id %>">
- <p><a href="javascript:document.delete_<%= @post.id %>.submit()">✖ delete</a>?</p>
+ <button type="submit" value="delete">✖ delete</button>
</form>
- <p><a href="/posts/edit/<%= @post.id %>">edit</a>?</p>
+ <p><a class="button yellow" href="/posts/<%= @post.id %>?edit=true">edit</a></p>
</body>
</html>
View
@@ -1,5 +1,4 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
-"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
View
@@ -1,11 +1,10 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
-"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
- <h1>Upps!</h1>
+ <h1>Uups!</h1>
<em><%= env['sinatra.error'].name %></em>
</body>
</html>

0 comments on commit 2ab6998

Please sign in to comment.