File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ get "/programs/new" do
2+ require_login!
3+ haml :"programs/new"
4+ end
5+
6+ post "/programs" do
7+ require_login!
8+ params [ :program ] [ 'creator_username' ] = current_user . username
9+ program = Program . create ( params [ :program ] )
10+ flash [ :notice ] = "Program created!"
11+ redirect "/programs/#{ program . creator_username } /#{ program . slug } "
12+ end
13+
14+ get "/programs/:username/:slug" do
15+ @program = Program . first ( :creator_username => params [ :username ] , :slug => params [ :slug ] )
16+ haml :"programs/show"
17+ end
Original file line number Diff line number Diff line change 1+ Feature : Programs
2+ Scenario : I can create new programs
3+ Given I'm logged in as "steve"
4+ When I go to the new program page
5+ And I fill in "Title" with "Hello world"
6+ And I fill in "Body" with "puts 'Hello, world!'"
7+ And I press "Create"
8+ Then I should be on the "steve/helloworld" program page.
9+ And I should see "Program created"
10+ And I should see "Hello world"
11+ And I should see "By steve"
12+ And I should see "puts"
Original file line number Diff line number Diff line change 1212DatabaseCleaner . strategy = :truncation
1313
1414Sinatra ::Application . app_file = app_file
15- Sinatra ::Application . set :environment , :test
1615
1716World do
1817 Capybara . app = Sinatra ::Application
1918 Capybara . app . set ( :environment , :test )
19+ Capybara . app . set ( :raise_errors , true )
2020 include Capybara
2121 include Spec ::Expectations
2222 include Spec ::Matchers
Original file line number Diff line number Diff line change @@ -40,6 +40,10 @@ def path_to(page_name)
4040 "/hackers/#{ $1} "
4141 when /my messages page/
4242 "/messages"
43+ when /the new program page/
44+ "/programs/new"
45+ when /the "(.*)\/ (.*)" program page/
46+ "/programs/#{ $1} /#{ $2} "
4347
4448 # Add more mappings here.
4549 # Here is an example that pulls values out of the Regexp:
Original file line number Diff line number Diff line change 1+ class Program
2+ include MongoMapper ::Document
3+ key :creator_username , String
4+ key :title , String
5+ key :slug , String
6+
7+ before_save :make_slug
8+
9+ private
10+ def make_slug
11+ self . slug = self . title . to_slug
12+ end
13+ end
Original file line number Diff line number Diff line change @@ -11,3 +11,9 @@ def require_directory dirname
1111 require f
1212 end
1313end
14+
15+ class String
16+ def to_slug
17+ self . gsub ( /\s / , "" ) . downcase
18+ end
19+ end
Original file line number Diff line number Diff line change 1+ %form {:action => " /programs" , :method => " POST" }
2+ %label{:for => " program_title" } Title
3+ %input{:type => " text" , :id => " program_title" , :name => " program[title]" }
4+ %label{:for => " program_body" } Body
5+ %input{:type => " text" , :id => " program_body" , :name => " program[body]" }
6+ %input{:type => " submit" , :value => " Create" }
Original file line number Diff line number Diff line change 1+ %h1 = @program.title
2+ %h2 By #{@program.creator_username}
3+
4+ %p = @program.body
You can’t perform that action at this time.
0 commit comments