Archive for December 2009

Dec232009

Action Mailer Configuration for Google app or Gmail

Comments Off

Recently I needed to setup smtp server to send mail. nothing was new, I simply used rails ActionMailer as one
should be. It should have been working, but wait; ohhhhhh snap, it was not!

It came out to be our very own google’s fault. Yes! as the email server was hosted on google app. And our google uses SSL SMTP or to be precise STARTTLS for routing emails. So here I am writing down step by step, what worked for me.

1. Create #{RAILS_ROOT}/config/mailer_settings.yml with following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
development:
	enable_starttls_auto:  true
	address: "smtp.gmail.com"
	port: 587
	domain:  'http://'
	authentication: plain
	user_name: "user@domain_name"
	password:  your-password
 
production:
	enable_starttls_auto:  true
	address: "smtp.gmail.com"
	port: 587
	domain:  'http://'
	authentication: plain
	user_name: "user@domain_name"
	password: your-password

Note: you can use enable_starttls_auto directly if your rails version > 2.2 and ruby
version is >=1.8.7, because built-in support for starttls. If not, then also you can use it; but you first need to install this gem.

1
gem install ambethia-smtp-tls

make dependencies

1
2
#in "#{RAILS_ROOT}/config/environment.rb" write the following
config.gem "ambethia-smtp-tls", :lib => "smtp-tls", :source => "http://gems.github.com/"

2. Load mail configuration settings, if not in test environment

1
2
3
4
5
# Load mail configuration if not in test environment
if RAILS_ENV != 'test'
	email_settings = YAML::load(File.open("#{RAILS_ROOT}/config/mailer_settings.yml"))
	ActionMailer::Base.smtp_settings = email_settings[RAILS_ENV] unless email_settings[RAILS_ENV].nil?
end

:) Happy Boarding

Dec32009

Webrat Steps extension (Be more DRY)

Comments Off

Recently when I was working with Cucumber and Webrat, I found default webrat definition very interesting. Even in some case I didn’t even had to write a single step definition. But while proceeding I found that something is missing from webrat definition. So I decided to extend it. And come up with some methods defined as below.

To use them just download this file and put it under features/step_definitions/

You can copy below code clipboard

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
94
95
96
97
98
99
100
101
102
103
104
105
106
#Created By Bagwan Pankaj (RailsJaipur) “http://railsjaipur.in/”
#This file is an extension of default webrat steps. some of the webrat steps have been extended
#in this file so that one need not to write unrequired steps.
#Extends default
#USE: When I follow //
When /^I follow \/([^\/]*)\/$/ do |regexp_link|
  click_link(regexp_link)
end
 
#USE: When I follow // within ""
When /^I follow \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp_link, parent|
  click_link_within(parent,regexp_link)
end
 
#USE: Then It should not include tag ""
Then /^It should not include tag "([^\"]*)"$/ do |tag|
  assert_have_no_selector(tag, :count => 0)
end
 
#USE: Then It should not include tag "" within ""
Then /^It should not include tag "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  within parent do |scope|
    scope.should_not have_selector(tag_match, :count => 0)
  end
end
 
#USE: Then It should not include selector ""
Then /^It should not include selector "([^\"]*)"$/ do |tag|
  Then %{It should not include tag \"#{tag}\"}
end
 
#USE: Then It should not include selector "" within ""
Then /^It should not include selector "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  Then %{It should not include tag \"#{tag_match}\" within \"#{parent.to_s}\"}
end
 
#USE: Then It should include tag ""
Then /^It should include tag "([^\"]*)"$/ do |tag|
  assert_have_selector(tag)
end
 
#USE: Then It should include tag "" within ""
Then /^It should include tag "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  within parent do |scope|
    scope.should have_selector(tag_match)
  end
end
 
#USE: Then It should include selector ""
Then /^It should include selector "([^\"]*)"$/ do |tag|
  Then %{It should include tag \"#{tag}\"}
end
 
#USE: Then It should include selector "" within ""
Then /^It should include selector "([^\"]*)" within "([^\"]*)"$/ do |tag_match, parent|
  Then %{It should include tag \"#{tag_match}\" within \"#{parent.to_s}\"}
end
 
#USE: When I check // within “”
When /^I check \/([^\/]*)\/ within "([^\"]*)"$/ do |field, parent|
  within parent do |scope|
    regexp = Regexp.new(field)
    scope.check(regexp)
  end
end
 
#USE: When /^I check “([^\"]*)” within “”
When /^I check "([^\"]*)" within "([^\"]*)"$/ do |field, parent|
  within parent do |scope|
    scope.check(field)
  end
end
 
#USE: When /^(?:|I )check //
When /^(?:|I )check \/([^\/]*)\/$/ do |field|
  regexp = Regexp.new(field)
  check(regexp)
end
 
#USE: I should be able to see  "
"
Then /^I should be able to see (\d+) "([^\"]*)"$/ do |count,selector|
  assert_have_selector(selector, :count => count)
end
 
#USE: I should be able to see  "
" within "
"
Then /^I should be able to see (\d+) "([^\"]*)" within "([^\"]*)"$/ do |count,selector, parent|
  within parent do  |scope|
    scope.should have_selector(selector, :count => count)
  end
end
 
When /^(?:|I )choose "([^\"]*)" within "([^\"]*)"$/ do |field,parent|
  within parent do |scope|
    scope.choose(field)
  end
end
 
When /^(?:|I )choose \/([^\/]*)\/ within "([^\"]*)"$/ do |field,parent|
  within parent do |scope|
    regexp = Regexp.new(field)
    scope.choose(regexp)
  end
end

Now you can use above listed methods easily and I assure you that if you use them properly you do not have to write a single step definition.

Their uses can be found against USE in the downloaded file.

“Use the Rails the way Rails is”

Happy Boarding :)

Get Adobe Flash playerPlugin by wpburn.com wordpress themes