<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,36 +1,46 @@
 #!/usr/bin/env ruby
 
-$:.unshift File.dirname(__FILE__) + &quot;/../lib&quot;
+$:.unshift File.dirname(__FILE__) + '/../lib'
 require 'camping'
 require 'camping/ar'
 require 'camping/session'
-  
+require 'redcloth'
+
 Camping.goes :Blog
 
 module Blog
-    include Camping::Session
-end
+  include Camping::Session
 
-module Blog::Models
-    class Post &lt; Base; belongs_to :user; end
+  module Models
+    class Post &lt; Base
+      belongs_to :user
+      
+      before_save do |record|
+        cloth = RedCloth.new(record.body)
+        cloth.hard_breaks = false
+        record.html_body = cloth.to_html
+      end
+    end
+    
     class Comment &lt; Base; belongs_to :user; end
     class User &lt; Base; end
 
-    class CreateTheBasics &lt; V 1.0
+    class BasicFields &lt; V 1.1
       def self.up
-        create_table :blog_posts do |t|
-          t.column :user_id,  :integer, :null =&gt; false
-          t.column :title,    :string,  :limit =&gt; 255
-          t.column :body,     :text
+        create_table :blog_posts, :force =&gt; true do |t|
+          t.integer :user_id,          :null =&gt; false
+          t.string  :title,            :limit =&gt; 255
+          t.text    :body, :html_body
+          t.timestamps 
         end
-        create_table :blog_users do |t|
-          t.column :username, :string
-          t.column :password, :string
+        create_table :blog_users, :force =&gt; true do |t|
+          t.string  :username, :password
         end
-        create_table :blog_comments do |t|
-          t.column :post_id,  :integer, :null =&gt; false
-          t.column :username, :string
-          t.column :body,     :text
+        create_table :blog_comments, :force =&gt; true do |t|
+          t.integer :post_id,          :null =&gt; false
+          t.string  :username
+          t.text    :body, :html_body
+          t.timestamps
         end
         User.create :username =&gt; 'admin', :password =&gt; 'camping'
       end
@@ -40,130 +50,133 @@ module Blog::Models
         drop_table :blog_comments
       end
     end
-end
+  end
 
-module Blog::Controllers
-    class Index &lt; R '/'
-        def get
-            @posts = Post.find :all
-            render :index
-        end
-    end
-     
-    class Add
-        def get
-            unless @state.user_id.blank?
-                @user = User.find @state.user_id
-                @post = Post.new
-            end
-            render :add
-        end
-        def post
-            unless @state.user_id.blank?
-                post = Post.create :title =&gt; input.post_title, :body =&gt; input.post_body,
-                                   :user_id =&gt; @state.user_id
-                redirect View, post
-            end
-        end
+  module Controllers
+    class Index
+      def get
+        @posts = Post.all(:order =&gt; 'updated_at DESC')
+        render :index
+      end
     end
 
-    class Info &lt; R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
-        def get(*args)
-            div do
-                code args.inspect; br; br
-                code @env.inspect; br
-                code &quot;Link: #{R(Info, 1, 2)}&quot;
-            end
-        end
+    class PostNew
+      def get
+        require_login!
+        @post = Post.new
+        render :add
+      end
+      
+      def post
+        require_login!
+        post = Post.create(:title =&gt; input.post_title, :body =&gt; input.post_body,
+          :user_id =&gt; @state.user_id)
+        redirect PostN, post
+      end
     end
 
-    class View &lt; R '/view/(\d+)'
-        def get post_id 
-            @post = Post.find post_id
-            @comments = Models::Comment.find :all, :conditions =&gt; ['post_id = ?', post_id]
-            render :view
-        end
-    end
-     
-    class Edit &lt; R '/edit/(\d+)', '/edit'
-        def get post_id 
-            unless @state.user_id.blank?
-                @user = User.find @state.user_id
-            end
-            @post = Post.find post_id
-            render :edit
-        end
-     
-        def post
-            unless @state.user_id.blank?
-                @post = Post.find input.post_id
-                @post.update_attributes :title =&gt; input.post_title, :body =&gt; input.post_body
-                redirect View, @post
-            end
-        end
+    class PostN
+      def get(post_id) 
+        @post = Post.find(post_id)
+        render :view
+      end
     end
-     
-    class Comment
-        def post
-            Models::Comment.create(:username =&gt; input.post_username,
-                       :body =&gt; input.post_body, :post_id =&gt; input.post_id)
-            redirect View, input.post_id
-        end
+
+    class Edit &lt; R '/post/(\d+)/edit'
+      def get(post_id)
+        require_login! 
+        @post = Post.find(post_id)
+        render :edit
+      end
+
+      def post(post_id)
+        require_login!
+        @post = Post.find(post_id)
+        @post.update_attributes :title =&gt; input.post_title, :body =&gt; input.post_body
+        redirect PostN, @post  
+      end
     end
-     
+
     class Login
-        def post
-            @user = User.find :first, :conditions =&gt; ['username = ? AND password = ?', input.username, input.password]
-     
-            if @user
-                @login = 'login success !'
-                @state.user_id = @user.id
-            else
-                @login = 'wrong user name or password'
-            end
-            render :login
+      def get
+        @to = input.to
+        render :login
+      end
+      
+      def post
+        @user = User.find_by_username_and_password(input.username, input.password)
+        @to = input.to
+
+        if @user
+          @state.user_id = @user.id
+          if @to
+            redirect @to
+          else
+            redirect R(Index)
+          end
+        else
+          @info = 'Wrong username or password'
         end
+        render :login
+      end
     end
-     
+
     class Logout
-        def get
-            @state.user_id = nil
-            render :logout
-        end
-    end
-     
-    class Style &lt; R '/styles.css'
-        def get
-            @headers[&quot;Content-Type&quot;] = &quot;text/css; charset=utf-8&quot;
-            @body = %{
-                body {
-                    font-family: Utopia, Georga, serif;
-                }
-                h1.header {
-                    background-color: #fef;
-                    margin: 0; padding: 10px;
-                }
-                div.content {
-                    padding: 10px;
-                }
-            }
-        end
+      def get
+        @state.user_id = nil
+        redirect Index
+      end
     end
-end
 
-module Blog::Views
+    class Style &lt; R '/styles\.css'
+      STYLE = File.read(__FILE__).gsub(/.*__END__/m, '')
 
+      def get
+        @headers['Content-Type'] = 'text/css; charset=utf-8'
+        STYLE
+      end
+    end
+  end
+  
+  module Helpers
+    def logged_in?
+      !!@state.user_id
+    end
+    
+    def require_login!
+      unless logged_in?
+        redirect X::Login, :to =&gt; @env.REQUEST_URI
+        throw :halt
+      end
+    end
+  end
+
+  module Views
     def layout
       html do
         head do
-          title 'blog'
+          title 'My Blog'
           link :rel =&gt; 'stylesheet', :type =&gt; 'text/css', 
-               :href =&gt; '/styles.css', :media =&gt; 'screen'
+          :href =&gt; '/styles.css', :media =&gt; 'screen'
         end
         body do
-          h1.header { a 'blog', :href =&gt; R(Index) }
-          div.content do
-            self &lt;&lt; yield
+          h1 { a 'My Blog', :href =&gt; R(Index) }
+          
+          div.wrapper! do
+            text yield
+          end
+          
+          hr
+          
+          p.footer! do
+            if logged_in?
+              _admin_menu
+            else
+              a 'Login', :href =&gt; R(Login, :to =&gt; @env.REQUEST_URI)
+              text ' to the adminpanel'
+            end
+            text ' &amp;ndash; Powered by '
+            a 'Camping', :href =&gt; 'http://code.whytheluckystiff.net/camping'
           end
         end
       end
@@ -171,98 +184,192 @@ module Blog::Views
 
     def index
       if @posts.empty?
-        p 'No posts found.'
-        p { a 'Add', :href =&gt; R(Add) }
+        h2 'No posts'
+        p do
+          text 'Could not find any posts. Feel free to '
+          a 'add one', :href =&gt; R(PostNew)
+          text ' yourself'
+        end
       else
-        for post in @posts
+        @posts.each do |post|
           _post(post)
         end
       end
     end
 
     def login
-      p { b @login }
-      p { a 'Continue', :href =&gt; R(Add) }
-    end
+      h2 'Login'
+      p.info @info if @info
+      
+      form :action =&gt; R(Login), :method =&gt; 'post' do
+        input :name =&gt; 'to', :type =&gt; 'hidden', :value =&gt; @to if @to
+        
+        label 'Username', :for =&gt; 'username'
+        input :name =&gt; 'username', :id =&gt; 'username', :type =&gt; 'text'
 
-    def logout
-      p &quot;You have been logged out.&quot;
-      p { a 'Continue', :href =&gt; R(Index) }
+        label 'Password', :for =&gt; 'password'
+        input :name =&gt; 'password', :id =&gt; 'password', :type =&gt; 'password'
+
+        input :type =&gt; 'submit', :class =&gt; 'submit', :value =&gt; 'Login'
+      end
     end
 
     def add
-      if @user
-        _form(@post, :action =&gt; R(Add))
-      else
-        _login
-      end
+      _form(@post, :action =&gt; R(PostNew))
     end
 
     def edit
-      if @user
-        _form(@post, :action =&gt; R(Edit))
-      else
-        _login
-      end
+      _form(@post, :action =&gt; R(Edit, @post))
     end
 
     def view
-        _post(@post)
-
-        p &quot;Comment for this post:&quot;
-        for c in @comments
-          h1 c.username
-          p c.body
-        end
-
-        form :action =&gt; R(Comment), :method =&gt; 'post' do
-          label 'Name', :for =&gt; 'post_username'; br
-          input :name =&gt; 'post_username', :type =&gt; 'text'; br
-          label 'Comment', :for =&gt; 'post_body'; br
-          textarea :name =&gt; 'post_body' do; end; br
-          input :type =&gt; 'hidden', :name =&gt; 'post_id', :value =&gt; @post.id
-          input :type =&gt; 'submit'
-        end
+      _post(@post)
     end
 
     # partials
-    def _login
-      form :action =&gt; R(Login), :method =&gt; 'post' do
-        label 'Username', :for =&gt; 'username'; br
-        input :name =&gt; 'username', :type =&gt; 'text'; br
-
-        label 'Password', :for =&gt; 'password'; br
-        input :name =&gt; 'password', :type =&gt; 'text'; br
-
-        input :type =&gt; 'submit', :name =&gt; 'login', :value =&gt; 'Login'
-      end
+    def _admin_menu
+      text [['Log out', R(Logout)], ['New', R(PostNew)]].map { |name, to|
+        capture { a name, :href =&gt; to}
+      }.join(' &amp;ndash; ')
     end
 
     def _post(post)
-      h1 post.title
-      p post.body
-      p do
-        [a(&quot;Edit&quot;, :href =&gt; R(Edit, post)), a(&quot;View&quot;, :href =&gt; R(View, post))].join &quot; | &quot;
+      h2 post.title
+      p.info do
+        text &quot;Written by &lt;strong&gt;#{post.user.username}&lt;/strong&gt; &quot;
+        text post.updated_at.strftime('%B %M, %Y @ %H:%M ')
+        _post_menu(post)
       end
+      text post.html_body
+    end
+    
+    def _post_menu(post)
+      text '('
+      a 'view', :href =&gt; R(PostN, post)
+      if logged_in?
+        text ', '
+        a 'edit', :href =&gt; R(Edit, post)
+      end
+      text ')'
     end
 
     def _form(post, opts)
-      p { &quot;You are logged in as #{@user.username} | #{a 'Logout', :href =&gt; R(Logout)}&quot; }
       form({:method =&gt; 'post'}.merge(opts)) do
-        label 'Title', :for =&gt; 'post_title'; br
-        input :name =&gt; 'post_title', :type =&gt; 'text', 
-              :value =&gt; post.title; br
+        label 'Title', :for =&gt; 'post_title'
+        input :name =&gt; 'post_title', :id =&gt; 'post_title', :type =&gt; 'text', 
+              :value =&gt; post.title
 
-        label 'Body', :for =&gt; 'post_body'; br
-        textarea post.body, :name =&gt; 'post_body'; br
+        label 'Body', :for =&gt; 'post_body'
+        textarea post.body, :name =&gt; 'post_body', :id =&gt; 'post_body'
 
         input :type =&gt; 'hidden', :name =&gt; 'post_id', :value =&gt; post.id
-        input :type =&gt; 'submit'
+        input :type =&gt; 'submit', :class =&gt; 'submit', :value =&gt; 'Submit'
       end
     end
+  end
 end
- 
+
 def Blog.create
   Blog::Models.create_schema :assume =&gt; (Blog::Models::Post.table_exists? ? 1.0 : 0.0)
 end
 
+__END__
+* {
+  margin: 0;
+  padding: 0;
+}
+
+body {
+  font: normal 14px Arial, 'Bitstream Vera Sans', Helvetica, sans-serif;
+  line-height: 1.5;
+}
+
+h1, h2, h3, h4 {
+  font-family: Georgia, serif;
+  font-weight: normal;
+}
+
+h1 {  
+  background-color: #EEE;
+  border-bottom: 5px solid #6F812D;
+  outline: 5px solid #9CB441;       
+  font-weight: normal;
+  font-size: 3em;  
+  padding: 0.5em 0;
+  text-align: center;
+}
+
+h1 a { color: #143D55; text-decoration: none }
+h1 a:hover { color: #143D55; text-decoration: underline }
+
+h2 {
+  font-size: 2em;
+  color: #287AA9;  
+}
+
+#wrapper { 
+  margin: 3em auto;
+  width: 700px;
+}
+
+p {
+  margin-bottom: 1em;
+}
+
+p.info, p#footer {
+  color: #999;
+  margin-left: 1em;
+}
+
+p.info a, p#footer a {
+  color: #999;
+}
+
+p.info a:hover, p#footer a:hover {
+  text-decoration: none;
+}
+
+a {
+  color: #6F812D;
+}
+
+a:hover {
+  color: #9CB441;
+}
+
+hr {
+  border-width: 5px 0;
+  border-style: solid;     
+  border-color: #9CB441;
+  border-bottom-color: #6F812D;
+  height: 0;   
+}
+
+p#footer {    
+  font-size: 0.9em;
+  margin: 0;      
+  padding: 1em;
+  text-align: center;
+}
+
+label {  
+  display: inline-block;
+  width: 100%;
+}
+
+input, textarea {     
+  margin-bottom: 1em;
+  width: 200px;  
+}
+
+input.submit {
+  float: left;
+  width: auto;
+}
+
+textarea {
+  font: normal 14px Arial, 'Bitstream Vera Sans', Helvetica, sans-serif;
+  height: 300px;
+  width: 400px;
+}
+</diff>
      <filename>examples/blog.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>205983ad09d255233590a1c9c6c9e7997d384a61</id>
    </parent>
  </parents>
  <author>
    <name>Magnus Holm</name>
    <email>judofyr@gmail.com</email>
  </author>
  <url>http://github.com/judofyr/camping/commit/61448a57bc6fea5790fcc0457aa6b90cdfb92fff</url>
  <id>61448a57bc6fea5790fcc0457aa6b90cdfb92fff</id>
  <committed-date>2008-10-31T13:10:12-07:00</committed-date>
  <authored-date>2008-10-31T12:13:33-07:00</authored-date>
  <message>Update example/blog.rb to new style</message>
  <tree>638fa235e22431ba0d6a8674058d2343e4bd794e</tree>
  <committer>
    <name>Magnus Holm</name>
    <email>judofyr@gmail.com</email>
  </committer>
</commit>
