|
1 | 1 | require 'rubygems' |
2 | 2 | require 'sinatra' |
3 | | -require 'mongo_mapper' |
4 | 3 | require 'haml' |
| 4 | +require 'mongo_mapper' |
5 | 5 | require 'rack-flash' |
6 | 6 | require 'rdiscount' |
7 | 7 |
|
8 | 8 | use Rack::Session::Cookie, :secret => 'h4ck3ty h4ck f0r gr347 g00d' |
9 | 9 | use Rack::Flash |
10 | 10 |
|
11 | | -set :views, File.join(File.dirname(__FILE__), 'views') |
12 | | - |
13 | | -def setup_db environ |
14 | | - MongoMapper.connection = Mongo::Connection.new('localhost') |
15 | | - MongoMapper.database = "hackety-#{environ}" |
16 | | -end |
17 | | - |
18 | | -configure :test do |
19 | | - setup_db(:test) |
20 | | -end |
21 | | - |
22 | | -configure :development do |
23 | | - setup_db(:development) |
24 | | -end |
25 | | - |
26 | | -configure do |
27 | | - Dir.glob("#{File.expand_path(File.dirname(__FILE__))}/models/*.rb").each do |f| |
28 | | - require f |
29 | | - end |
30 | | -end |
31 | | - |
32 | | -helpers do |
| 11 | +require 'utility' |
33 | 12 |
|
34 | | - def current_user |
35 | | - return User.first(:id => session[:user_id]) if session[:user_id] |
36 | | - nil |
37 | | - end |
| 13 | +require 'configure' |
38 | 14 |
|
39 | | - def logged_in? |
40 | | - return session[:user_id] != nil |
41 | | - end |
42 | | - |
43 | | -end |
| 15 | +require 'helpers' |
44 | 16 |
|
45 | 17 | get "/" do |
46 | 18 | haml :index |
47 | 19 | end |
48 | 20 |
|
49 | | -get "/blog" do |
50 | | - @posts = Post.all |
51 | | - haml :'posts/index' |
52 | | -end |
53 | | - |
54 | | -get "/posts/new" do |
55 | | - unless logged_in? && current_user.admin? |
56 | | - flash[:error] = "Sorry, buddy" |
57 | | - redirect "/posts" |
58 | | - end |
59 | | - |
60 | | - haml :'posts/new' |
61 | | -end |
62 | | - |
63 | | -post "/posts" do |
64 | | - unless logged_in? && current_user.admin? |
65 | | - flash[:error] = "Sorry, buddy" |
66 | | - redirect "/posts" |
67 | | - end |
68 | | - |
69 | | - @post = Post.create(params) |
70 | | - flash[:notice] = "Post Created" |
71 | | - redirect "/posts/#{@post.id}" |
72 | | -end |
73 | | - |
74 | | -get "/posts" do |
75 | | - @posts = Post.all |
76 | | - haml :'posts/index' |
77 | | -end |
78 | | - |
79 | | -get "/posts/:id" do |
80 | | - @post = Post.find(params[:id]) |
81 | | - haml :'posts/show' |
82 | | -end |
83 | | - |
84 | | -get "/posts/:id/edit" do |
85 | | - unless logged_in? && current_user.admin? |
86 | | - flash[:error] = "Sorry, buddy" |
87 | | - redirect "/posts" |
88 | | - end |
89 | | - |
90 | | - @post = Post.find(params[:id]) |
91 | | - haml :'posts/edit' |
92 | | -end |
93 | | - |
94 | | -put "/posts/:id" do |
95 | | - unless logged_in? && current_user.admin? |
96 | | - flash[:error] = "Sorry, buddy" |
97 | | - redirect "/posts" |
98 | | - end |
99 | | - @post = Post.find(params[:id]) |
100 | | - @post.update_attributes(params) |
101 | | - flash[:notice] = "Post Modified" |
102 | | - redirect "/posts/#{@post.id}" |
103 | | -end |
104 | | - |
105 | | -post "/comments" do |
106 | | - unless current_user |
107 | | - flash[:error] = "You must be logged in to comment!" |
108 | | - redirect "/posts" |
109 | | - end |
110 | | - |
111 | | - params[:comment]['user_email'] = current_user.email |
112 | | - @post = Post.find(params[:post_id]) |
113 | | - @post.comments << Comment.new(params[:comment]) |
114 | | - @post.save |
115 | | - flash[:notice] = "Thanks for your comment!" |
116 | | - redirect "/posts/#{@post.id}" |
117 | | -end |
118 | | - |
119 | | -get '/signup' do |
120 | | - haml :"users/signup" |
121 | | -end |
122 | | - |
123 | | -post '/signup' do |
124 | | - @user = User.create(params[:user]) |
125 | | - if @user && @user.valid? |
126 | | - session[:user] = @user.id |
127 | | - flash[:notice] = "Account created." |
128 | | - redirect '/' |
129 | | - else |
130 | | - flash[:notice] = 'There were some problems creating your account. Please be sure you\'ve entered all your information correctly.' |
131 | | - redirect '/signup' |
132 | | - end |
133 | | -end |
134 | | - |
135 | | -get '/login' do |
136 | | - haml :"users/login" |
137 | | -end |
138 | | - |
139 | | -post '/login' do |
140 | | - if user = User.authenticate(params[:username], params[:password]) |
141 | | - session[:user_id] = user.id |
142 | | - flash[:notice] = "Login successful." |
143 | | - |
144 | | - if session[:return_to] |
145 | | - redirect_url = session[:return_to] |
146 | | - session[:return_to] = false |
147 | | - redirect redirect_url |
148 | | - else |
149 | | - redirect '/' |
150 | | - end |
151 | | - else |
152 | | - flash[:notice] = "The username or password you entered is incorrect." |
153 | | - redirect '/login' |
154 | | - end |
155 | | -end |
156 | | - |
157 | | -get '/logout' do |
158 | | - session[:user_id] = nil |
159 | | - flash[:notice] = "Logout successful." |
160 | | - redirect '/' |
161 | | -end |
162 | | - |
| 21 | +require_directory "controllers" |
0 commit comments