I first thought that I would have to use ERB to render non-HTML-like templates. Not true. Haml let you do a :plain filter which is pretty much the same as using ERB templates.
Here’s a simple example haml template for email:
1
2
3
4
5
6
| :plain
Welcome #{@user.username} to Fund-A-Stache!
click the following link to activate your account:
#{activation_link(@user)} |
I’m also using Google SMTP for email. That way I can send email from my home (which blocks SMTP traffic). This may change, but just so I can remember why I’m doing this, here’s the Pony call:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| Pony.mail(
:to=>@user.email,
:from=>'user@example.org',
:subject=>'Welcome',
:body=>(haml :registration_email, :layout=>false),
:content_type=>'text/html',
:via=>:smtp,
:smtp=>{
:host=>'smtp.gmail.com',
:port=>'587',
:tls=>true,
:user=>'user@example.org',
:password=>'secret',
:auth=>:plain, # :plain, :login, :cram_md5, no auth by default
:domain=>"example.org" # the HELO domain provided by the client to the server
}
) |
Posted: March 13th, 2010 | Author: jay | Filed under: Code | Tags: email, erb, haml, pony, ruby, smtp, template | No Comments »
This is simple, but I’m dumping it here anyway… I’d like to use the Twitter model of account access: http://example.com/USERNAME
So, if the user exists, if should display that user’s page. If the user doesn’t exist (or the user is deactivated) then this route will pass, which in this case, passes to the 404 error page. Here’s how I’m dealing with that in my routes:
1
2
3
4
5
| get '/:username' do
@user=User.first(:username=>params[:username],:activated=>true)
pass unless @user
haml :user_page
end |
Note:This should be low on the chain of routes so that users with names or actual routes can’t be rendered as user pages. Example, a user with a username of “login” should not interfere with the application function of “login”. I might look further into protecting this beyond it’s position in the URL parsing chain.
Posted: March 13th, 2010 | Author: jay | Filed under: Code | Tags: 404, pass, route, ruby, sinatra, url | No Comments »
This may not be the best way to do this, but this is what I used at the top of my test file so that a helper method has a value for Rack’s env['HTTP_HOST']:
1
2
3
4
5
6
7
| class FundastacheUserTest < Test::Unit::TestCase
...
def env
last_request.env['HTTP_HOST']="example.org"
last_request.env
end
... |
The test tests to see that using the activation link (that a user gets in an email) activates the user. It looks something like this:
1
2
3
4
5
6
7
8
9
10
11
12
| def test_using_activation_link_should_activate_account
create_user
user=User.first
path=activation_link(user)
path=~/http\:\/\/example.org(.*)/
get $1
follow_redirect!
assert last_response.ok?
assert last_response.body.include?('Account activated!')
user=User.first
assert user.activated
end |
and the helper method looks something like this:
1
2
3
4
5
6
7
8
9
10
|
module FundAStache
module Helpers
...
def activation_link(user)
"http://#{env['HTTP_HOST']}/user/activate/#{user.activation_token}"
end
...
end
end |
Posted: March 13th, 2010 | Author: jay | Filed under: Code | Tags: env, helper, http_host, rack, ruby, test | No Comments »