|
1 | 1 | # encoding: utf-8 |
2 | 2 |
|
3 | | -#first off, we need to include rubygems and sinatra |
4 | | -require 'rubygems' |
| 3 | +# This is the source code for the [Hackety Hack website][hw]. Hackety Hack is |
| 4 | +# the easiest way to learn programming, and so our documentation should be |
| 5 | +# top-notch. |
| 6 | +# |
| 7 | +# To get started, you'll need to install some prerequisite software: |
| 8 | +# |
| 9 | +# **Ruby** is used to power the site. We're currently using ruby 1.9.2p0. I |
| 10 | +# highly reccomend that you use [rvm][rvm] to install and manage your Rubies. |
| 11 | +# It's a fantastic tool. If you do decide to use `rvm`, you can install the |
| 12 | +# appropriate Ruby and create a gemset by simply `cd`-ing into the root project |
| 13 | +# directory; I have a magical `.rvmrc` file that'll set you up. |
| 14 | +# |
| 15 | +# **MongoDB** is a really awesome document store. We use it to persist all of |
| 16 | +# the data on the website. To get MongoDB, please visit their |
| 17 | +# [downloads page](http://www.mongodb.org/downloads) to find a package for your |
| 18 | +# system. |
| 19 | +# |
| 20 | +# After installing Ruby and MongoDB, you need to aquire all of the Ruby gems |
| 21 | +# that we use. This is pretty easy, since we're using **bundler**. Just do |
| 22 | +# this: |
| 23 | +# |
| 24 | +# $ gem install bundler |
| 25 | +# $ bundle install |
| 26 | +# |
| 27 | +# That'll set it all up! Then, you need to make sure you're running MongoDB. |
| 28 | +# I have to open up another tab in my terminal and type |
| 29 | +# |
| 30 | +# $ mongod |
| 31 | +# |
| 32 | +# to get this to happen. When you're done hacking, you can hit ^-c to stop |
| 33 | +# `mongod` from running. |
| 34 | +# |
| 35 | +# To actually start up the site, just |
| 36 | +# |
| 37 | +# $ rackup |
| 38 | +# |
| 39 | +# and then visit [http://localhost:9292/](http://localhost:9292). You're good |
| 40 | +# to go! |
| 41 | +# |
| 42 | +# [hw]: http://hackety-hack.com |
| 43 | +# [rvm]: http://rvm.beginrescueend.com/ |
| 44 | + |
| 45 | +#### About hackety.rb |
| 46 | +# |
| 47 | +# This file is the main entry point to the application. It has three main |
| 48 | +# purposes: |
| 49 | +# |
| 50 | +# 1. Include all relevant gems and library code. |
| 51 | +# 2. Configure all settings based on our environment. |
| 52 | +# 3. Set up a few basic routes. |
| 53 | +# |
| 54 | +# Everything else is handled by code that's included from this file. |
| 55 | + |
| 56 | +#### Including gems |
| 57 | + |
| 58 | +# We need to require rubygems and bundler to get things going. Then we call |
| 59 | +# `Bundler.setup` to get all of the magic started. |
5 | 60 |
|
6 | | -#this bundler stuff needed for heroku |
| 61 | +require 'rubygems' |
7 | 62 | require 'bundler' |
8 | 63 | Bundler.setup |
9 | 64 |
|
| 65 | +# We use [sinatra](http://sinatrarb.com/) for our web framework. Sinatra is |
| 66 | +# very light and simple. Good stuff. |
10 | 67 | require 'sinatra' |
11 | 68 |
|
12 | | -#this lets us send emails |
| 69 | +# [Pony](https://github.com/adamwiggins/pony) is used to send emails, just like the Pony express. Also, running `gem install pony` is really satisfying. |
13 | 70 | require 'pony' |
14 | 71 |
|
15 | | -#haml gives us all of our templates |
| 72 | +# [haml](http://haml-lang.com/) creates all of our templates. haml is concise |
| 73 | +# and expressive. I really enjoy it. |
16 | 74 | require 'haml' |
17 | 75 |
|
18 | | -#mongomapper connects us to our database |
| 76 | +# [MongoMapper](http://mongomapper.com/) is a library we use to make it easy to |
| 77 | +# store our model classes into MongoDB. |
19 | 78 | require 'mongo_mapper' |
20 | 79 |
|
21 | | -#rack-flash gives us nice flash messages |
| 80 | +# If you've used Rails' flash messages, you know how convenient they are. |
| 81 | +# rack-flash lets us use them. |
22 | 82 | require 'rack-flash' |
| 83 | +use Rack::Flash |
23 | 84 |
|
24 | | -#rdiscount lets us write things using markdown |
| 85 | +# [rdiscount](https://github.com/rtomayko/rdiscount) is a fast implementation |
| 86 | +# of the Markdown markup language. The web site renders most user submitted |
| 87 | +# comment with Markdown. |
25 | 88 | require 'rdiscount' |
26 | 89 |
|
27 | | -#we need to set up a secret to encrypt our sessions with |
28 | | -use Rack::Session::Cookie, :secret => ENV['COOKIE_SECRET'] |
29 | | - |
30 | | -#we also have to let the world know we want to use flashes |
31 | | -use Rack::Flash |
32 | | - |
| 90 | +# Rails has a `content_for` helper that lets you place different parts of your |
| 91 | +# view into different places in your template. This helps a lot with |
| 92 | +# javascript, and conditional stylesheets or other includes. It's so nice that |
| 93 | +# foca has written |
| 94 | +# [a Sinatra version](https://github.com/foca/sinatra-content-for). |
33 | 95 | require 'sinatra/content_for' |
34 | 96 |
|
| 97 | +# We moved lots of helpers into a separate file. These are all things that are |
| 98 | +# useful throughout the rest of the application. This file |
35 | 99 | require_relative 'helpers' |
36 | 100 |
|
| 101 | +#### Configure settings |
| 102 | + |
| 103 | +# We need a secret for our sessions. This is set via an environment variable so |
| 104 | +# that we don't have to give it away in the source code. Heroku makes it really |
| 105 | +# easy to keep environment variables set up, so this ends up being pretty nice. |
| 106 | +use Rack::Session::Cookie, :secret => ENV['COOKIE_SECRET'] |
| 107 | + |
| 108 | +# We use [Exceptional](http://www.getexceptional.com/) to keep track of errors |
| 109 | +# that happen. This code is from their |
| 110 | +# [example documentation](http://docs.getexceptional.com/getting-started/sinatra/) |
| 111 | +# for Sinatra. It _might_ be better off inside of a config block, but I haven't |
| 112 | +# tested it in that role yet. |
37 | 113 | if ENV['RACK_ENV'] == 'production' |
38 | 114 | set :raise_errors, true |
39 | 115 |
|
40 | 116 | require 'exceptional' |
41 | 117 | use Rack::Exceptional, ENV['EXCEPTIONAL_API_KEY'] |
42 | 118 | end |
43 | 119 |
|
44 | | -#this makes Haml escape any html by default. See here: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options |
| 120 | +# This makes [Haml escape any html](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options) by default. |
45 | 121 | set :haml, :escape_html => true |
46 | 122 |
|
47 | | -#these configure blocks only run in one environment |
| 123 | +# The `PONY_VIA_OPTIONS` hash is used to configure `pony`. Basically, we only |
| 124 | +# want to actually send mail if we're in the production environment. So we set |
| 125 | +# the hash to just be `{}`, except when we want to send mail. |
48 | 126 | configure :test do |
49 | 127 | PONY_VIA_OPTIONS = {} |
50 | 128 | end |
|
53 | 131 | PONY_VIA_OPTIONS = {} |
54 | 132 | end |
55 | 133 |
|
| 134 | +# We're using [SendGrid](http://sendgrid.com/) to send our emails. It's really |
| 135 | +# easy; the Heroku addon sets us up with environment variables with all of the |
| 136 | +# configuration options that we need. |
56 | 137 | configure :production do |
57 | | - |
58 | 138 | PONY_VIA_OPTIONS = { |
59 | 139 | :address => "smtp.sendgrid.net", |
60 | 140 | :port => "25", |
|
66 | 146 |
|
67 | 147 | end |
68 | 148 |
|
69 | | -#for all environments, |
| 149 | +# We don't want to bother with running our own MongoDB server in production; |
| 150 | +# that's what The Cloud (tm) is for! So we want to double check our environment |
| 151 | +# variables, and if it appears that we'd like to connect to |
| 152 | +# [MongoHQ](https://mongohq.com/), let's do that. Otherwise, just connect to |
| 153 | +# our local server running on localhost. |
70 | 154 | configure do |
71 | 155 | if ENV['MONGOHQ_URL'] |
72 | 156 | MongoMapper.connection = Mongo::Connection.new(ENV['MONGOHQ_HOST'], ENV['MONGOHQ_PORT']) |
|
78 | 162 | MongoMapper.connection = Mongo::Connection.new('localhost') |
79 | 163 | MongoMapper.database = "hackety-development" |
80 | 164 | end |
81 | | - #require all of our models! |
82 | | - require_directory "models" |
83 | 165 | end |
84 | 166 |
|
| 167 | +# Since Sinatra doesn't automatically load anything, we have to do it |
| 168 | +# ourselves. Remember that helpers.rb file? Well, we made a handy |
| 169 | +# `require_directory` method that, well, `require`s a whole directory. So let's |
| 170 | +# include both of our models as well as our controllers. |
| 171 | +require_directory "models" |
85 | 172 | require_directory "controllers" |
86 | 173 |
|
| 174 | +#### Set up basic routes |
| 175 | + |
| 176 | +# The first thing you'll ever see when going to the website is here. It all |
| 177 | +# starts with `/`. If we're logged in, we want to just redirect to the main |
| 178 | +# activity stream. If not, let's show that pretty splash page that sings all of |
| 179 | +# our praises. |
| 180 | +# |
| 181 | +# One small note about rendering, though: Our main layout doesn't exactly work |
| 182 | +# for the main page, it's an exception. So we don't want to use our regular old |
| 183 | +# `layout.haml` file. So we tell Sinatra not to. |
87 | 184 | get '/' do |
88 | 185 | if logged_in? |
89 | 186 | redirect "/stream" |
90 | 187 | end |
91 | 188 | haml :index, :layout => :plain |
92 | 189 | end |
93 | 190 |
|
94 | | -get '/post' do |
95 | | - haml :post |
96 | | -end |
97 | | - |
| 191 | +# Hopefully, anyone visiting the site will think that Hackety Hack sounds |
| 192 | +# pretty cool. If they do, they'll visit the downloads page. This'll direct |
| 193 | +# them to download Hackety, and sign up for an account. |
| 194 | +# |
| 195 | +# Similar to the home page, we also don't want our layout here, either. |
98 | 196 | get '/download' do |
99 | 197 | haml :download, :layout => :plain |
100 | 198 | end |
101 | 199 |
|
| 200 | +# The main activity stream is the main page for the site when a user is logged |
| 201 | +# in. It lets them share what they're doing with others, and also view all of |
| 202 | +# the content that others have posted. So we grab it all, and sort it in the |
| 203 | +# opposite order that it's been updated. Wouldn't want to see old stuff! |
102 | 204 | get '/stream' do |
103 | 205 | @content_list = Content.all.sort{|a, b| b.updated_at <=> a.updated_at } |
104 | 206 | haml :stream |
|
0 commit comments