Skip to content

Commit 222c99d

Browse files
committed
Added tons of comments and whitespace.
I love making commits that are all comments. This one had a few other small refactorings in it too, the require_admin! and require_login! methods were too nice to not refactor.
1 parent 50f4b00 commit 222c99d

14 files changed

Lines changed: 256 additions & 25 deletions

File tree

Rakefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
#this file sets up all of our rake tasks
2+
13
require 'rubygems'
24
require 'spec/rake/spectask'
35
require 'cucumber/rake/task'
46

7+
#this lets us run 'rake spec' to run rspec tests
58
Spec::Rake::SpecTask.new do |t|
9+
10+
#tell rspec where our spec files are
611
t.spec_files = FileList['spec/*_spec.rb',]
12+
13+
#get some nice pretty options
714
t.spec_opts = ["--colour", "--backtrace"]
15+
#
16+
#include our spec helper
817
t.ruby_opts = ["-r spec/spec_helper.rb"]
918
end
1019

20+
#this lets us run 'rake features' to run cucumber tests
1121
Cucumber::Rake::Task.new(:features) do |t|
22+
#let's do some pretty output!
1223
t.cucumber_opts = "--format pretty"
1324
end

config.ru

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ require 'rubygems'
22
require 'sinatra'
33

44
set :env, :development
5-
set :public, File.expand_path(File.dirname(__FILE__) + '/public')
65

76
require 'hackety'
87

configure.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
#this file contains various configuration options
2+
3+
#let's set up the views directory for sinatra
14
set :views, File.join(File.dirname(__FILE__), 'views')
5+
#we'll also set up a public directory
6+
set :public, File.expand_path(File.dirname(__FILE__) + '/public')
27

8+
#this makes Haml escape any html by default. See here: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options
39
set :haml, :escape_html => true
410

11+
#this method will set up our database connection for any environment
512
def setup_db environ
13+
#we want to connect to localhost
614
MongoMapper.connection = Mongo::Connection.new('localhost')
15+
#and use the database for the environment we're on
16+
#this keeps them separate so our tests don't wipe out our development information!
717
MongoMapper.database = "hackety-#{environ}"
818
end
919

20+
#these configure blocks only run in one environment
21+
#right now the only configuration we do special is set up the databse
22+
#I'm sure these can be condensed into one block, I just havn't done it yet.
1023
configure :test do
1124
setup_db(:test)
1225
end
@@ -15,6 +28,12 @@ def setup_db environ
1528
setup_db(:development)
1629
end
1730

31+
configure :production do
32+
setup_db(:production)
33+
end
34+
35+
#for all environments,
1836
configure do
37+
#require all of our models!
1938
require_directory "models"
2039
end

controllers/hacker.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
#This is the 'hackers' controller. "Hackers" are what we call "Users" in HH.
2+
3+
4+
# An individual Hacker's page
15
get '/hackers/:name' do
6+
#find the hacker with the given name
27
@hacker = Hacker.first(:username => params[:name])
8+
9+
#render the template
310
haml :"hackers/show"
411
end

controllers/posts.rb

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,107 @@
1+
# This is the controller for blog posts.
2+
3+
4+
# We can access the blog at /blog
15
get "/blog" do
6+
#first we find all posts
27
@posts = Post.all
8+
9+
#and then we render our template
310
haml :'posts/index'
411
end
512

13+
#Admin users can make new posts at /posts/new
614
get "/posts/new" do
7-
unless logged_in? && current_user.admin?
8-
flash[:error] = "Sorry, buddy"
9-
redirect "/posts"
10-
end
15+
#make sure we only let in admins!
16+
admin_only!
1117

18+
#render the template
1219
haml :'posts/new'
1320
end
1421

22+
#When a new post is made, it sends a POST request to /posts
1523
post "/posts" do
16-
unless logged_in? && current_user.admin?
17-
flash[:error] = "Sorry, buddy"
18-
redirect "/posts"
19-
end
24+
#make sure we only let in admins!
25+
admin_only!
2026

27+
#make the post with the given params
2128
@post = Post.create(params)
29+
30+
#set a friendly message
2231
flash[:notice] = "Post Created"
32+
33+
#go check out that post
2334
redirect "/posts/#{@post.id}"
2435
end
2536

37+
#for completeness, we can also see every post at /posts
2638
get "/posts" do
39+
#grab all of the posts
2740
@posts = Post.all
41+
42+
#render the template
2843
haml :'posts/index'
2944
end
3045

46+
#an individual post can be seen at /posts/:id
3147
get "/posts/:id" do
48+
#find the post with that id
3249
@post = Post.find(params[:id])
50+
51+
#render our template
3352
haml :'posts/show'
3453
end
3554

55+
#admins can edit posts at /posts/:id/edit
3656
get "/posts/:id/edit" do
37-
unless logged_in? && current_user.admin?
38-
flash[:error] = "Sorry, buddy"
39-
redirect "/posts"
40-
end
57+
#make sure we only let in admins!
58+
admin_only!
4159

60+
#find the post with the right id
4261
@post = Post.find(params[:id])
62+
63+
#render our template
4364
haml :'posts/edit'
4465
end
4566

67+
#to update a post, send a PUT request to /posts/:id
4668
put "/posts/:id" do
47-
unless logged_in? && current_user.admin?
48-
flash[:error] = "Sorry, buddy"
49-
redirect "/posts"
50-
end
69+
#make sure we only let in admins!
70+
admin_only!
71+
72+
#find the post with the right id
5173
@post = Post.find(params[:id])
74+
75+
#update its info
5276
@post.update_attributes(params)
77+
78+
#set a friendly message
5379
flash[:notice] = "Post Modified"
80+
81+
#and visit that post
5482
redirect "/posts/#{@post.id}"
5583
end
5684

85+
#I've included comments in here too, because comments can only be made on posts
86+
#make a new comment by sending a POST request to /comments
5787
post "/comments" do
58-
unless current_user
59-
flash[:error] = "You must be logged in to comment!"
60-
redirect "/posts"
61-
end
88+
#we need to be logged in to comment
89+
#if we fail, return to /posts
90+
require_login! :return => "/posts"
6291

92+
#set the email of the comment to our email
6393
params[:comment]['user_email'] = current_user.email
94+
95+
#find the post we want to comment on
6496
@post = Post.find(params[:post_id])
97+
98+
# create our new comment and add it to the posts' comments
6599
@post.comments << Comment.new(params[:comment])
66100
@post.save
101+
102+
#set a helpful message
67103
flash[:notice] = "Thanks for your comment!"
104+
105+
#go back to the page for that post
68106
redirect "/posts/#{@post.id}"
69107
end

controllers/sessions.rb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,87 @@
1+
#The session controller deals with users loggin in, logging out, and
2+
#signing up. An important part of the site!
3+
4+
#new users sign up at /signup
15
get '/signup' do
6+
7+
#just render our template!
28
haml :"sessions/signup"
39
end
410

11+
#the form for /signup sends a POST to /signup!
512
post '/signup' do
13+
#create a new hacker with our parameters
614
@hacker = Hacker.create(params[:user])
15+
16+
#we need to make sure all the information is okay.
717
if @hacker && @hacker.valid?
8-
session[:user] = @user.id
18+
#add our hacker_id to the session
19+
session[:hacker_id] = @hacker.id
20+
21+
#set a friendly message
922
flash[:notice] = "Account created."
23+
24+
#let's go to the main page!
1025
redirect '/'
1126
else
27+
#this is what happens if the information is bad.
28+
29+
#set an error message
1230
flash[:notice] = 'There were some problems creating your account. Please be sure you\'ve entered all your information correctly.'
31+
32+
#let's go back to the signup page so that they can try again.
1333
redirect '/signup'
1434
end
1535
end
1636

37+
#people can log in by going to /login
1738
get '/login' do
39+
#just gotta render that template!
1840
haml :"sessions/login"
1941
end
2042

43+
#the form at /login sends a POST request to /login
2144
post '/login' do
45+
#let's see if we got a correct username/password:
2246
if hacker = Hacker.authenticate(params[:username], params[:password])
47+
48+
#we did! Set our session up
2349
session[:hacker_id] = hacker.id
50+
51+
#let the user know they logged in via a flash message
2452
flash[:notice] = "Login successful."
2553

54+
#if they came from somewhere special, let's take them back there!
2655
if session[:return_to]
56+
#grab the url we need to go to
2757
redirect_url = session[:return_to]
58+
59+
#reset the session so we don't go there twice
2860
session[:return_to] = false
61+
62+
#go to the url!
2963
redirect redirect_url
3064
else
65+
#if we didn't go somewhere special, let's just go to /
3166
redirect '/'
3267
end
3368
else
69+
#oops! I guess we got our information wrong! Let's give them a message:
3470
flash[:notice] = "The username or password you entered is incorrect."
71+
72+
#and go back to the login page so they can try again
3573
redirect '/login'
3674
end
3775
end
3876

77+
#users can logout by going to /logout
3978
get '/logout' do
79+
#we need to remove our id from the session
4080
session[:hacker_id] = nil
81+
82+
#and let the user know they logged out!
4183
flash[:notice] = "Logout successful."
84+
85+
#and then return to the main page
4286
redirect '/'
4387
end

factories/hacker.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
#These are the factory_girl factories for Hackers.
2+
#Learn more about factory_girl here: http://github.com/thoughtbot/factory_girl
3+
4+
#this is an email sequence, so we don't use the same email over and over!
15
Factory.sequence :email do |n|
26
"user#{n}@example.com"
37
end
48

9+
#this factory defines a hacker
510
Factory.define :hacker do |u|
611
u.username "steve"
712
u.email { Factory.next(:email) }
813
u.password "foobar"
14+
#we need to set the password_confirmation to the same value as the password
915
u.password_confirmation {|user| user.password }
1016
u.admin false
1117
end
1218

19+
#this is the factory for the admin.
1320
Factory.define :admin, :parent => :hacker do |u|
21+
#feel the power!!!
1422
u.admin true
1523
end

factories/post.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#These are the factory_girl factories for Posts.
2+
#Learn more about factory_girl here: http://github.com/thoughtbot/factory_girl
3+
4+
#just a little factory for a post!
15
Factory.define :post do |p|
26
p.title "default title"
37
p.body "This is a body"

hackety.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1+
#woo hoo! This is the main file for the Hackety site.
2+
3+
#first off, we need to include rubygems and sinatra
14
require 'rubygems'
25
require 'sinatra'
6+
7+
#haml gives us all of our templates
38
require 'haml'
9+
10+
#mongomapper connects us to our database
411
require 'mongo_mapper'
12+
13+
#rack-flash gives us nice flash messages
514
require 'rack-flash'
15+
16+
#rdiscount lets us write things using markdown
617
require 'rdiscount'
718

19+
#we need to set up a secret to encrypt our sessions with
820
use Rack::Session::Cookie, :secret => 'h4ck3ty h4ck f0r gr347 g00d'
21+
22+
#we also have to let the world know we want to use flashes
923
use Rack::Flash
1024

25+
#let's require a bunch of files that do good things
1126
require 'utility'
12-
1327
require 'configure'
14-
1528
require 'helpers'
1629

30+
#here's the root route. This is what happens when you go to '/'
1731
get "/" do
1832
haml :index
1933
end
2034

35+
#finally, let's require all of our controllers
2136
require_directory "controllers"

0 commit comments

Comments
 (0)