Skip to content

Commit 537a1c6

Browse files
committed
Users can create new programs.
Users can now make new programs. Pretty cool.
1 parent 8800198 commit 537a1c6

8 files changed

Lines changed: 63 additions & 1 deletion

File tree

controllers/programs.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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

features/programs.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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"

features/support/env.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
DatabaseCleaner.strategy = :truncation
1313

1414
Sinatra::Application.app_file = app_file
15-
Sinatra::Application.set :environment, :test
1615

1716
World 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

features/support/paths.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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:

models/program.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

utility.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ def require_directory dirname
1111
require f
1212
end
1313
end
14+
15+
class String
16+
def to_slug
17+
self.gsub(/\s/, "").downcase
18+
end
19+
end

views/programs/new.haml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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" }

views/programs/show.haml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
%h1= @program.title
2+
%h2 By #{@program.creator_username}
3+
4+
%p= @program.body

0 commit comments

Comments
 (0)