Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Finalzing a trivial example

  • Loading branch information...
commit 4b82c900b3a70ad4f979cb086b930ec3bae65fa8 1 parent 1fbe49d
Jesse Cooke authored March 02, 2010
27  examples/widgets/spec/widgets_spec.rb
@@ -19,20 +19,33 @@ def app
19 19
             last_response.body.should contain 'Home'
20 20
         end
21 21
 
  22
+        it 'should have a message form' do
  23
+            get '/'
  24
+            last_response.should be_ok
  25
+            last_response.body.should have_selector("form[name='message']")
  26
+        end
  27
+
  28
+        it 'should accept a POST' do
  29
+            post    '/',
  30
+                    "message"   => "yo dawg"
  31
+            last_response.should be_ok
  32
+            last_response.should contain("yo dawg")
  33
+       end
  34
+
22 35
     end
23 36
 
24  
-    context "'/login' route" do
  37
+    context "'/message' route" do
25 38
 
26  
-        it 'should have a form' do
27  
-            get '/login'
  39
+        it 'should have the formatted message' do
  40
+            get '/message'
28 41
             last_response.should be_ok
29  
-            last_response.body.should include("action='/login'")
  42
+            last_response.body.should contain("***")
30 43
         end
31 44
 
32  
-        it 'should have a legit login form' do
33  
-            get '/login'
  45
+        it 'should kill the message' do
  46
+            get '/kill'
34 47
             last_response.should be_ok
35  
-            last_response.body.should have_selector("form[name='login']")
  48
+            last_response.should_not have_selector("#message")
36 49
         end
37 50
 
38 51
     end
7  examples/widgets/views/index.haml
... ...
@@ -0,0 +1,7 @@
  1
+%form{ :method => :post, :action => '/', :name => 'message' }
  2
+
  3
+    %label{ :for => 'message' }Message
  4
+    %input{ :type => 'password', :name => 'message' }
  5
+    %br
  6
+    %input{ :type => 'submit', :value => 'Post the message' }
  7
+
6  examples/widgets/views/layout.haml
@@ -8,5 +8,9 @@
8 8
             %h1 Widgets
9 9
             %nav
10 10
                 %a{ :href => '/' }Home
11  
-                %a{ :href => '/login' }Login
  11
+                %a{ :href => '/message' }Message
  12
+                - if session[:message]
  13
+                    %a{ :href => '/kill' }Kill the message
  14
+        - if @message
  15
+            #message= @message
12 16
         = yield
1  examples/widgets/views/login.haml
... ...
@@ -1 +0,0 @@
1  
-%form{ :method => :post, :action => '/login', :name => 'login' }
2  examples/widgets/views/message.haml
... ...
@@ -0,0 +1,2 @@
  1
+%h2 The formatted message
  2
+= format_message
35  examples/widgets/widgets.rb
... ...
@@ -1,9 +1,10 @@
1  
-%w(rubygems sinatra dm-core haml sass).each{ |lib| require lib }
  1
+%w(rubygems sinatra haml sass).each{ |lib| require lib }
2 2
 
3 3
 
4 4
 configure do
5 5
     set :views, "#{File.dirname(__FILE__)}/views"
6 6
     set :haml,  { :format    => :html5 }
  7
+    enable :sessions
7 8
 end
8 9
 
9 10
 configure :development do
@@ -12,12 +13,40 @@
12 13
 
13 14
 end
14 15
 
  16
+helpers do
  17
+
  18
+    def format_message
  19
+        message = session[:message] if session[:message]
  20
+        "*** #{message} ***"
  21
+    end
  22
+
  23
+end
  24
+
  25
+before do
  26
+
  27
+    if session[:message]
  28
+        puts "this is the message: #{session[:message]}"
  29
+    end
  30
+
  31
+    @message = session[:message]
  32
+
  33
+end
15 34
 
16 35
 
17 36
 get '/' do
18 37
     haml :index
19 38
 end
20 39
 
21  
-get '/login' do
22  
-    haml :login
  40
+post '/' do
  41
+    @message = session[:message] = params[:message]
  42
+    haml :index
  43
+end
  44
+
  45
+get '/message' do
  46
+    haml :message
  47
+end
  48
+
  49
+get '/kill' do
  50
+    session[:message] = nil
  51
+    haml :index
23 52
 end

0 notes on commit 4b82c90

Please sign in to comment.
Something went wrong with that request. Please try again.