OSDN Git Service

81a87da83088bdf5d5737c7596ebdd14e8ee5433
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / mongrel-1.1.5-x86-mswin32-60 / examples / camping / blog.rb
1 #!/usr/bin/env ruby
2
3 $:.unshift File.dirname(__FILE__) + "/../../lib"
4 require 'rubygems'
5 require_gem 'camping', '>=1.4'
6 require 'camping/session'
7   
8 Camping.goes :Blog
9
10 module Blog
11     include Camping::Session
12 end
13
14 module Blog::Models
15     def self.schema(&block)
16         @@schema = block if block_given?
17         @@schema
18     end
19   
20     class Post < Base; belongs_to :user; end
21     class Comment < Base; belongs_to :user; end
22     class User < Base; end
23 end
24
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
30       t.column :body,     :text
31     end
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
36     end
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
41       t.column :body,     :text
42     end
43     execute "INSERT INTO blog_users (username, password) VALUES ('admin', 'camping')"
44 end
45
46 module Blog::Controllers
47     class Index < R '/'
48         def get
49             @posts = Post.find :all
50             render :index
51         end
52     end
53      
54     class Add
55         def get
56             unless @state.user_id.blank?
57                 @user = User.find @state.user_id
58                 @post = Post.new
59             end
60             render :add
61         end
62         def post
63             post = Post.create :title => input.post_title, :body => input.post_body,
64                                :user_id => @state.user_id
65             redirect View, post
66         end
67     end
68
69     class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
70         def get(*args)
71             div do
72                 code args.inspect; br; br
73                 code ENV.inspect; br
74                 code "Link: #{R(Info, 1, 2)}"
75             end
76         end
77     end
78
79     class View < R '/view/(\d+)'
80         def get post_id 
81             @post = Post.find post_id
82             @comments = Models::Comment.find :all, :conditions => ['post_id = ?', post_id]
83             render :view
84         end
85     end
86      
87     class Edit < R '/edit/(\d+)', '/edit'
88         def get post_id 
89             unless @state.user_id.blank?
90                 @user = User.find @state.user_id
91             end
92             @post = Post.find post_id
93             render :edit
94         end
95      
96         def post
97             @post = Post.find input.post_id
98             @post.update_attributes :title => input.post_title, :body => input.post_body
99             redirect View, @post
100         end
101     end
102      
103     class Comment
104         def post
105             Models::Comment.create(:username => input.post_username,
106                        :body => input.post_body, :post_id => input.post_id)
107             redirect View, input.post_id
108         end
109     end
110      
111     class Login
112         def post
113             @user = User.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password]
114      
115             if @user
116                 @login = 'login success !'
117                 @state.user_id = @user.id
118             else
119                 @login = 'wrong user name or password'
120             end
121             render :login
122         end
123     end
124      
125     class Logout
126         def get
127             @state.user_id = nil
128             render :logout
129         end
130     end
131      
132     class Style < R '/styles.css'
133         def get
134             @headers["Content-Type"] = "text/css; charset=utf-8"
135             @body = %{
136                 body {
137                     font-family: Utopia, Georga, serif;
138                 }
139                 h1.header {
140                     background-color: #fef;
141                     margin: 0; padding: 10px;
142                 }
143                 div.content {
144                     padding: 10px;
145                 }
146             }
147         end
148     end
149 end
150
151 module Blog::Views
152
153     def layout
154       html do
155         head do
156           title 'blog'
157           link :rel => 'stylesheet', :type => 'text/css', 
158                :href => '/styles.css', :media => 'screen'
159         end
160         body do
161           h1.header { a 'blog', :href => R(Index) }
162           div.content do
163             self << yield
164           end
165         end
166       end
167     end
168
169     def index
170       if @posts.empty?
171         p 'No posts found.'
172         p { a 'Add', :href => R(Add) }
173       else
174         for post in @posts
175           _post(post)
176         end
177       end
178     end
179
180     def login
181       p { b @login }
182       p { a 'Continue', :href => R(Add) }
183     end
184
185     def logout
186       p "You have been logged out."
187       p { a 'Continue', :href => R(Index) }
188     end
189
190     def add
191       if @user
192         _form(post, :action => R(Add))
193       else
194         _login
195       end
196     end
197
198     def edit
199       if @user
200         _form(post, :action => R(Edit))
201       else
202         _login
203       end
204     end
205
206     def view
207         _post(post)
208
209         p "Comment for this post:"
210         for c in @comments
211           h1 c.username
212           p c.body
213         end
214
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'
222         end
223     end
224
225     # partials
226     def _login
227       form :action => R(Login), :method => 'post' do
228         label 'Username', :for => 'username'; br
229         input :name => 'username', :type => 'text'; br
230
231         label 'Password', :for => 'password'; br
232         input :name => 'password', :type => 'text'; br
233
234         input :type => 'submit', :name => 'login', :value => 'Login'
235       end
236     end
237
238     def _post(post)
239       h1 post.title
240       p post.body
241       p do
242         a "Edit", :href => R(Edit, post)
243         a "View", :href => R(View, post)
244       end
245     end
246
247     def _form(post, opts)
248       p do
249         text "You are logged in as #{@user.username} | "
250         a 'Logout', :href => R(Logout)
251       end
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
256
257         label 'Body', :for => 'post_body'; br
258         textarea post.body, :name => 'post_body'; br
259
260         input :type => 'hidden', :name => 'post_id', :value => post.id
261         input :type => 'submit'
262       end
263     end
264 end
265  
266 def Blog.create
267     Camping::Models::Session.create_schema
268     unless Blog::Models::Post.table_exists?
269         ActiveRecord::Schema.define(&Blog::Models.schema)
270     end
271 end
272
273 if __FILE__ == $0
274   require 'mongrel/camping'
275
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
279   Blog.create
280   
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("")
286       trap("INT") { stop }
287       run
288     end
289   end
290
291   puts "** Blog example is running at http://localhost:3002/blog"
292   puts "** Default username is `admin', password is `camping'"
293   config.join
294 end