forked from hacketyhack/hackety-hack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.rb
94 lines (80 loc) · 3.19 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# The **helpers.rb** file contains all of our Sinatra helpers. This is large
# enough that including it in a separate file makes it much easier to find;
# otherwise, I'd be opening **hackety.rb** and searching for 'helpers,' and
# that's just stupid.
helpers do
# A tiny bit of metaprogramming goes a long way. We want to generate three
# methods (`development?`, `production?`, and `test?`) that let us know which
# environment we happen to be in. This is useful in a few places.
[:development, :production, :test].each do |environment|
define_method "#{environment.to_s}?" do
return settings.environment == environment.to_sym
end
end
# This incredibly useful helper gives us the currently logged in user. We
# keep track of that by just setting a session variable with their id. If it
# doesn't exist, we just want to return nil.
def current_user
return nil unless session[:hacker_id]
# Memoize to make sure code like this works properly:
# current_user.password = 'foo'
# current_user.save
@current_user ||= Hacker.first(:id => session[:hacker_id]) if session[:hacker_id]
end
# This very simple method checks if we've got a logged in user. That's pretty
# easy: just check our current_user.
def logged_in?
current_user != nil
end
# Our `admin_only!` helper will only let admin users visit the page. If
# they're not an admin, we redirect them to either / or the page that we
# specified when we called it.
def admin_only!(opts = {:return => "/"})
unless logged_in? && current_user.admin?
flash[:error] = "Sorry, buddy"
redirect opts[:return]
end
end
# Similar to `admin_only!`, `require_login!` only lets logged in users access
# a particular page, and redirects them if they're not.
def require_login!(opts = {:return => "/"})
unless logged_in?
flash[:error] = "Sorry, buddy"
redirect opts[:return]
end
end
# We also want to have a way for the desktop application to make calls to the
# site. For this, we allow a username and password to be passed in, and we
# authenticate directly, rather than relying on a previously logged in
# session.
def require_login_or_api!(opts={})
return if session[:hacker_id]
hacker = Hacker.authenticate(opts[:username], opts[:password])
if hacker
session[:hacker_id] = hacker.id
else
halt 401
end
end
# [Gravatar](http://gravatar.com/) is used for our avatars. Generating the
# url for one is pretty simple, we just need the proper email address, and
# then we make an md5 of it. No biggie.
def gravatar_url_for email
require 'digest/md5'
"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.downcase)}"
end
end
# This handy helper method lets us require an entire directory of `rb` files.
# It's much simpler than having to require them all directly.
def require_directory dirname
Dir.glob("#{File.expand_path(File.dirname(__FILE__))}/#{dirname}/*.rb").each do |f|
require f
end
end
# This method is a handy monkeypatch on String. It allows us to turn any string
# into a slug that's suitable for putting into URLs.
class String
def to_slug
self.gsub(/[^a-zA-Z _0-9]/, "").gsub(/\s/, "_").downcase
end
end