Sha256: da95a92547aab9284f4bf9d810d92b045fe34b472d12f97b57ceae9754e827a1

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

$:.unshift( "../lib" )
require 'rubygems'
require 'capcode'
require 'capcode/base/couchdb'

class Story < Capcode::Base
  include Capcode::Resource
  
  property :title, String
  property :body, String
  property :date, String
end

module Capcode
  class HTTPError
    def r404(f)
      "Pas glop !!! #{f} est inconnu !!!"
    end
  end
  
  class Index < Route '/'
    def get
      r = "<html><body>"
      
      story = Story.find( :all )
      
      story.each do |s|
        r += "<h2>#{s.title}</h2><small>#{s.date} - <a href='#{URL( Remove, s.id, s.rev )}'>Delete this entry</a></small><p>#{s.body}</p>"
      end
      
      r+"<hr /><a href='#{URL(Add)}'>Add a new entry</a></body></html>"
    end
  end
  
  class Remove < Route '/remove/([^\/]*)/(.*)'
    def get( id, rev )
      Story.delete(id, rev)
      redirect( Index )
    end
  end
  
  class Add < Route '/add'
    def get
      '
        <html><body>
          <h1>Add a new entry</h1>
          <form method="POST">
            Titre : <input type="text" name="title"><br />
            <textarea name="body"></textarea><br />
            <input type="submit">
          </form>
        </body></html>
      '
    end
    def post
      Story.create( :title => params['title'], :body => params['body'], :date => Time.now.to_s )
      redirect( Index )
    end
  end
end

Capcode.run( :port => 3001, :host => "localhost", :db_config => "blog-couchdb.yml" )

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
Capcode-0.2.0 examples/blog-couchdb.rb
Capcode-0.3.0 examples/blog-couchdb.rb
Capcode-0.4.0 examples/blog-couchdb.rb
Capcode-0.5.0 examples/blog-couchdb.rb
Capcode-0.6.0 examples/blog-couchdb.rb