reflection to tealeaf course3 week4

Three kinds of BDD

In week 4 beginning, we got a brief intro for 3 kinds of BDD:

  • meet in the middle

    I think this should be the popular one for modern development. The front-end engineer build the pages from mockups, then back-end developer connect UI with controllers and models.

  • inside out

    It develops from models to controllers till views and integrations. It’s hard to decide what models you really need in the beginning.

  • outside in

    It devlops from integrations to controllers and models. It start from a big vision, and close to what user’s need. But for developer, it’s hard to code from this big vision, especially for integration test in the development beginning.

Self Referential Associations

We can use self referential association to let user track other users. For example:

1
2
3
4
5
# t.integer :follower_id, :leader_id
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: 'User'
belongs_to :leader, class_name: 'User'
end

Then, model User can have these virtual attributes:

1
2
has_many :following_relationships, class_name: 'Relationship', foreign_key: :follower_id
has_many :leading_relationships, class_name: 'Relationship', foreign_key: :leader_id

Sending Emails

Rails also offer ActionMailer for sending emails.

  • create files in app/mailers/app_mailer.rb
1
2
3
4
5
6
7
class AppMailer < ActionMailer::Base

def send_welcome_email(user)
@user = user
mail to: user.email, from: '[email protected]', subject: 'Welcome to Myflix'
end
end
  • View template in app/views/app_mailers/send_welcome_email:
1
2
3
%html(lang='en-US')
%body
%p Welcome to MyFlix, #{@user.full_name}
  • Config for config/environments/development.rb
1
config.action_mailer.delivery_method = :letter_opener

And

1
2
3
group :development do
gem 'letter-opener'
end

This is only for development setting, we also need to set for test and production independent

Handling Sensitive Account Info

It’s dangerous to upload config setting file to github or other public storage, there is someone update AWS key to GitHub and got a unbelievable credit bill.

Use ENV to protect secret data, we can use gem Figaro.
Figara is the simplest way to keep our key value safe.

There are two posts talking about this topic:

  1. Rails Environment Variables
  2. Configuration and config Vars

Update: Tealeaf also have a post about Managing Environment Configuration Variables in Rails

Test Email sending

Remember to clear email deliveries before test:

1
before { ActionMailer::Base.deliveries.clear }

Then we can validate email in the test by this

1
2
3
post :create
expect(ActionMailer::Base.deliveries.last.to).to eq([user_attributes["email"]])
# expect(ActionMailer::Base.deliveries.last.body).to include(user_attributes["full_name"])

Update database, use rake tasks

An advice from TA Tomtomecek, I update existing data in model User, and he suggest me to use custom rake tasks to do that. I’ve not taste that yet, just leave a memo here to remember.