Skip to content

Commit 755c942

Browse files
committed
Added the ability to add new posts.
1 parent dff8b40 commit 755c942

12 files changed

Lines changed: 85 additions & 0 deletions

File tree

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ gem "cucumber-sinatra"
99
gem "capybara"
1010
gem "factory_girl"
1111
gem "bson_ext"
12+
gem "sinatra-authentication"
13+
gem "database_cleaner"
14+
gem "rack-flash"

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
require 'rubygems'
22
require 'spec/rake/spectask'
3+
require 'cucumber/rake/task'
4+
35
Spec::Rake::SpecTask.new do |t|
46
t.spec_files = FileList['spec/*_spec.rb',]
57
end
8+
9+
Cucumber::Rake::Task.new(:features) do |t|
10+
t.cucumber_opts = "--format pretty"
11+
end

factories/user.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Factory.sequence :email do |n|
2+
"user#{n}@example.com"
3+
end
4+
5+
Factory.define :user, :class => "MmUser" do |u|
6+
u.email { Factory.next(:email) }
7+
u.password "foobar"
8+
u.password_confirmation {|user| user.password }
9+
end
10+
11+
Factory.define :admin, :parent => :user do |u|
12+
u.permission_level -1
13+
end

features/blog.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ Feature: The Hackety Blog
33
Given there is a blog post with the title "First Post"
44
When I go to the blog page
55
Then I should see "First Post"
6+
Scenario: I can create a post
7+
Given I'm logged in as admin
8+
And I go to the new post page
9+
When I fill in "Title" with "new title"
10+
And I fill in "Body" with "new body"
11+
And I press "Create Post"
12+
Then I should see "Post Created"
13+
And I should see "new title"
14+
And I should see "new body"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Given /^I'm logged in as admin$/ do
2+
password = "foobar"
3+
@user = Factory(:admin,:password => password, :password_confirmation => password)
4+
visit "/login"
5+
fill_in "email", :with => @user.email
6+
fill_in "password", :with => password
7+
click_button "login"
8+
end
9+

features/support/env.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Generated by cucumber-sinatra. (Sat Jul 10 15:46:03 -0400 2010)
2+
ENV['RACK_ENV']='test'
23

34
app_file = File.join(File.dirname(__FILE__), '..', '..', 'hackety.rb')
45
require app_file
56

67
require 'capybara'
78
require 'capybara/cucumber'
89
require 'spec'
10+
require 'database_cleaner'
11+
require 'database_cleaner/cucumber'
12+
DatabaseCleaner.strategy = :truncation
913

1014
Sinatra::Application.app_file = app_file
15+
Sinatra::Application.set :environment, :test
1116

1217
World do
1318
Capybara.app = Sinatra::Application

features/support/paths.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def path_to(page_name)
1616
when /the blog page/
1717
'/blog'
1818

19+
when /the new post\s? page/
20+
'/posts/new'
21+
1922
# Add more mappings here.
2023
# Here is an example that pulls values out of the Regexp:
2124
#

hackety.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
require 'rubygems'
22
require 'sinatra'
33
require 'mongo_mapper'
4+
require 'sinatra-authentication'
5+
require 'haml'
6+
require 'rack-flash'
7+
8+
use Rack::Session::Cookie, :secret => 'h4ck3ty h4ck f0r gr347 g00d'
9+
use Rack::Flash
410

511
configure do
612
MongoMapper.connection = Mongo::Connection.new('localhost')
@@ -18,3 +24,18 @@
1824
@posts = Post.all
1925
haml :blog_index
2026
end
27+
28+
get "/posts/new" do
29+
haml :posts_new
30+
end
31+
32+
post "/posts" do
33+
@post = Post.create(params)
34+
flash[:notice] = "Post Created"
35+
redirect "/posts/#{@post.id}"
36+
end
37+
38+
get "/posts/:id" do
39+
@post = Post.find(params[:id])
40+
haml :posts_show
41+
end

models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class User
2+
end

views/layout.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
%head
44
%title Hackety Hack
55
%body
6+
%div{:id => "flash"}= flash[:notice]
67
= yield

0 commit comments

Comments
 (0)