Reflection to Tealeaf Course3 Week5 (1/2)
###Feature test for sending email
Following by previous week, this week we have a feature test for sending email.
Install gem,
1 | $ gem 'capybara-email' |
And added this line to spec_helper.rb
1 | require 'capybara/email/rspec' |
Then
1 | require 'spec_helper' |
Especially notice for open_email
, current_email
functions are offered by capybara-email
gem.
###Invite Friend and Registeration
We also have a work on inviting friends through email, the key point here is how to use a secure token
to tracking data.
- create an invitations data, including inviter, recipient email, name, and
token
. when user get the email, following the link provided to server, page will direct to the register page with existing data stored with this token.
And set the routes for path
1 | get 'register/:token', to: 'users#new_with_invitation_token' |
- we can have a hidden data in the register form for new#users to use.
1 | = hidden_field_tag :token, @invitation_token |
we don’t use form_for element, becuase this hidden attribute does not belong to backend modeled form.
###Concerns
To Dry our code, we can use concern
with module
. Here is a post by DHH why we should do this.
We can merge the same function feature into concern file. For example: Tokenable
1 | #/lib/tokenable.rb |
Then add to model file
1 | require 'tokenable' |
I prefer to specify require file name, or we can use auto loading library files.
###Email Service Providers
Since we have test email sending on ENV:development and EVN:test, now we have test it in the real world. That’s why we need a real email service provider. Google has it but with many restriction, Tealeaf recommend Mailgun
or Postmark
. Here we have a taste for `Mailgun’.
To setting config/environments/production.rb
, add these lines:
1 | config.action_mailer.default_url_options = { :host => "tomo-myflix.herokuapp.com" } |
tomo-myflix.herokuapp.com
is my app name on Heroku. And ENV about Mailgun are maintained by Heroku.
I aslo can use this command to check Heroku configurations
.
1 | $ heroku config |
Then push it to heroku and test for a real email address, it works!!
###Background Jobs
Sometimes, some actions do not need to be synchrous, like sending email, we don’t need to wait email sending action completed. That’s why we need to use a background job to do sending email to speed up the flow.
Sidekiq
and Resque
are some of this concept product. And Tealeaf recommend to use Sidekiq.
Install gem Sidekiq
1 | gem 'sidekiq' |
Only a tiny change for using it by delay method
1 | AppMailer.delay.notify_on_new_todo(current_user, @todo) # new |
Add this line in spec/spec_helper.rb
1 | require 'sidekiq/testing/inline' |
Here we setup sidekiq, but we still need start Redis
server:
1 | $ brew install redis |
And this start sidekiq with another terminal:
1 | $ bundle exec sidekiq |
So far, this makes local
works fine with sidekiq
for background jobs. If we want to use background jobs in ENV:production, Heroku take charges if dyno is more than 1, or try this way to simulate
I have not implemented this yet, but I’ll try it later.
There is also an article talking about Procfile and Foreman.
If sidekiq version >= 3.0, you might also run this line1
heroku config:set REDIS_PROVIDER=REDISTOGO_URL
###Monitor Production Error
Here we have a try for Sentry.
Tracking errors is also a key for automation deployment.