3 $:.unshift File.dirname(__FILE__) + "/../../lib"
5 require_gem 'camping', '>=1.4'
6 require 'camping/session'
11 include Camping::Session
15 def self.schema(&block)
16 @@schema = block if block_given?
20 class Post < Base; belongs_to :user; end
21 class Comment < Base; belongs_to :user; end
22 class User < Base; end
25 Blog::Models.schema do
26 create_table :blog_posts, :force => true do |t|
27 t.column :id, :integer, :null => false
28 t.column :user_id, :integer, :null => false
29 t.column :title, :string, :limit => 255
32 create_table :blog_users, :force => true do |t|
33 t.column :id, :integer, :null => false
34 t.column :username, :string
35 t.column :password, :string
37 create_table :blog_comments, :force => true do |t|
38 t.column :id, :integer, :null => false
39 t.column :post_id, :integer, :null => false
40 t.column :username, :string
43 execute "INSERT INTO blog_users (username, password) VALUES ('admin', 'camping')"
46 module Blog::Controllers
49 @posts = Post.find :all
56 unless @state.user_id.blank?
57 @user = User.find @state.user_id
63 post = Post.create :title => input.post_title, :body => input.post_body,
64 :user_id => @state.user_id
69 class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
72 code args.inspect; br; br
74 code "Link: #{R(Info, 1, 2)}"
79 class View < R '/view/(\d+)'
81 @post = Post.find post_id
82 @comments = Models::Comment.find :all, :conditions => ['post_id = ?', post_id]
87 class Edit < R '/edit/(\d+)', '/edit'
89 unless @state.user_id.blank?
90 @user = User.find @state.user_id
92 @post = Post.find post_id
97 @post = Post.find input.post_id
98 @post.update_attributes :title => input.post_title, :body => input.post_body
105 Models::Comment.create(:username => input.post_username,
106 :body => input.post_body, :post_id => input.post_id)
107 redirect View, input.post_id
113 @user = User.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password]
116 @login = 'login success !'
117 @state.user_id = @user.id
119 @login = 'wrong user name or password'
132 class Style < R '/styles.css'
134 @headers["Content-Type"] = "text/css; charset=utf-8"
137 font-family: Utopia, Georga, serif;
140 background-color: #fef;
141 margin: 0; padding: 10px;
157 link :rel => 'stylesheet', :type => 'text/css',
158 :href => '/styles.css', :media => 'screen'
161 h1.header { a 'blog', :href => R(Index) }
172 p { a 'Add', :href => R(Add) }
182 p { a 'Continue', :href => R(Add) }
186 p "You have been logged out."
187 p { a 'Continue', :href => R(Index) }
192 _form(post, :action => R(Add))
200 _form(post, :action => R(Edit))
209 p "Comment for this post:"
215 form :action => R(Comment), :method => 'post' do
216 label 'Name', :for => 'post_username'; br
217 input :name => 'post_username', :type => 'text'; br
218 label 'Comment', :for => 'post_body'; br
219 textarea :name => 'post_body' do; end; br
220 input :type => 'hidden', :name => 'post_id', :value => post.id
221 input :type => 'submit'
227 form :action => R(Login), :method => 'post' do
228 label 'Username', :for => 'username'; br
229 input :name => 'username', :type => 'text'; br
231 label 'Password', :for => 'password'; br
232 input :name => 'password', :type => 'text'; br
234 input :type => 'submit', :name => 'login', :value => 'Login'
242 a "Edit", :href => R(Edit, post)
243 a "View", :href => R(View, post)
247 def _form(post, opts)
249 text "You are logged in as #{@user.username} | "
250 a 'Logout', :href => R(Logout)
252 form({:method => 'post'}.merge(opts)) do
253 label 'Title', :for => 'post_title'; br
254 input :name => 'post_title', :type => 'text',
255 :value => post.title; br
257 label 'Body', :for => 'post_body'; br
258 textarea post.body, :name => 'post_body'; br
260 input :type => 'hidden', :name => 'post_id', :value => post.id
261 input :type => 'submit'
267 Camping::Models::Session.create_schema
268 unless Blog::Models::Post.table_exists?
269 ActiveRecord::Schema.define(&Blog::Models.schema)
274 require 'mongrel/camping'
276 Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog.db'
277 Blog::Models::Base.logger = Logger.new('camping.log')
278 Blog::Models::Base.threaded_connections=false
281 # Use the Configurator as an example rather than Mongrel::Camping.start
282 config = Mongrel::Configurator.new :host => "0.0.0.0" do
283 listener :port => 3002 do
284 uri "/blog", :handler => Mongrel::Camping::CampingHandler.new(Blog)
285 uri "/favicon", :handler => Mongrel::Error404Handler.new("")
291 puts "** Blog example is running at http://localhost:3002/blog"
292 puts "** Default username is `admin', password is `camping'"