We were having problem on our website to send email to our staffs. The web content is servered by Modx and design and developed by a design house.
After looking into the code, it's found that the default email server was not able to send email to our gmail domain email address. To server the problem, we use another server on sendgrid.
$mail = new PHPMailer();
$mail->From = 'xxx@xxx-xxx.com';
$mail->FromName = 'some name';
$mail->IsSMTP();
$mail->Host = "www.sendgrid.net";
$mail->Port = 3535;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
...
Monday, November 26, 2012
Wednesday, November 21, 2012
Ruby - Get a class object from name string
eval('your_class_name')
Tuesday, November 20, 2012
Thursday, November 15, 2012
Rails - access to root/base module/class
Use ::
e.g. ::Calculator
e.g. ::Calculator
Wednesday, November 14, 2012
Rails - Rails console useful tips
[8] pry(main)> app.class
=> ActionDispatch::Integration::Session
[9] pry(main)> app.users_path
=> "/users"
[10] pry(main)> app.root_url
=> "http://www.example.com/"
[11] pry(main)> app.new_user_session_path
=> "/user/sign_in"
[14] pry(main)> app.get '/users'
=> 302
[15] pry(main)> app.response.body.length
=> 101
app.get
app.post
app.put
app.delete
[13] pry(main)> helper.class
=> ActionView::Base
[17] pry(main)> "".methods.grep(/case/).sort
=> ["camelcase",
"casecmp",
"downcase",
"downcase!",
"swapcase",
"swapcase!",
"titlecase",
"upcase",
"upcase!"]
[18] pry(main)> all_users = User.all; nil
User Load (1.6ms) SELECT "users".* FROM "users"
=> nil
[22] pry(main)> y User.first
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
--- !ruby/ActiveRecord:User
attributes:
name: ghealey
encrypted_password: $2a$10$rhYT.gbYbOanyudJa19cluQ1XlFFh4ntC8IhH5p.xxa9ddrtSx4.O
created_at: 2010-11-26 05:51:01.32897
failed_attempts: "0"
updated_at: 2012-11-14 06:20:53.496916
last_sign_in_ip:
email: ghealey@example.com
=> nil
References:
http://37signals.com/svn/posts/3176-three-quick-rails-console-tips
http://samuelmullen.com/2012/07/getting-more-out-of-the-rails-console/
http://paulsturgess.co.uk/articles/39-debugging-tips-in-ruby-on-rails
http://www.therailworld.com/posts/35-Interacting-with-Controllers-via-the-app-object
=> ActionDispatch::Integration::Session
[9] pry(main)> app.users_path
=> "/users"
[10] pry(main)> app.root_url
=> "http://www.example.com/"
[11] pry(main)> app.new_user_session_path
=> "/user/sign_in"
[14] pry(main)> app.get '/users'
=> 302
[15] pry(main)> app.response.body.length
=> 101
app.get
app.post
app.put
app.delete
[13] pry(main)> helper.class
=> ActionView::Base
[17] pry(main)> "".methods.grep(/case/).sort
=> ["camelcase",
"casecmp",
"downcase",
"downcase!",
"swapcase",
"swapcase!",
"titlecase",
"upcase",
"upcase!"]
[18] pry(main)> all_users = User.all; nil
User Load (1.6ms) SELECT "users".* FROM "users"
=> nil
[22] pry(main)> y User.first
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
--- !ruby/ActiveRecord:User
attributes:
name: ghealey
encrypted_password: $2a$10$rhYT.gbYbOanyudJa19cluQ1XlFFh4ntC8IhH5p.xxa9ddrtSx4.O
created_at: 2010-11-26 05:51:01.32897
failed_attempts: "0"
updated_at: 2012-11-14 06:20:53.496916
last_sign_in_ip:
email: ghealey@example.com
=> nil
References:
http://37signals.com/svn/posts/3176-three-quick-rails-console-tips
http://samuelmullen.com/2012/07/getting-more-out-of-the-rails-console/
http://paulsturgess.co.uk/articles/39-debugging-tips-in-ruby-on-rails
http://www.therailworld.com/posts/35-Interacting-with-Controllers-via-the-app-object
Gmail alias
http://support.google.com/mail/bin/answer.py?hl=en&answer=12096
Gmail doesn't offer traditional aliases, but you can receive messages sent to your.username+any.alias@gmail.com. For example, messages sent to jane.doe+notes@gmail.com are delivered to jane.doe@gmail.com.
Gmail doesn't offer traditional aliases, but you can receive messages sent to your.username+any.alias@gmail.com. For example, messages sent to jane.doe+notes@gmail.com are delivered to jane.doe@gmail.com.
Tuesday, November 13, 2012
Jenkins, Rails - Run rails test directly on Jenkins workspace
$ cd /var/lib/jenkins/jobs/[Jenkins_project_name]/workspace/
$ RAILS_ENV=test bundle exec rake test:units
$ RAILS_ENV=test bundle exec rake test:units
Rails - Trace and solve rake db migration error
We were doing a simple db migration in which a new column, say 'valuation_date', is added to an existing model 'Record'. In the migration, the valuation_date column is added. Then a code segment is executed to assign value to it. Like this:
def self.up
add_column :records, :valuation_date, :date
Record.find_each do |record|
record.valuation_date = ...
record.save
end
end
The deployment (rake db:migrate) works fine on development environment. However, when doing cap deploy:migrations, it returns error:
== AddValutionDateToRecords: migrating ===================================
-- add_column(:records, :valuation_date, :date)
-> 0.0014s
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `column' for nil:NilClass
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Turns out this only happens when running the migration under rails' staging environment. Then tried execute the db migration and trace the issue.
$ bundle exec rake RAILS_ENV=staging db:migrate --trace
undefined method `relation' for nil:NilClass
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/arel-2.0.10/lib/arel/crud.rb:12:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/persistence.rb:266:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/locking/optimistic.rb:77:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/attribute_methods/dirty.rb:68:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/timestamp.rb:60:in `update'
...
To look deeper, set a "debugger" breakpoint before record.save. Then another 2 breakpoints on crud.rb:12 .../active_record/base.rb:1718. It was found that record.class.arel_table['valuation_date'] is nil that caused the failure.
Investigation #1
Check if the Arel using different version in different environments. In debug mode, $ p Arel::VERSION show they are the same.
Investigation #2
Compare the difference between dev and staging:
$ rake about
$ rake RAILS_ENV=staging about
The results show that ActionDispatch::Static middleware is not include in staging. Tried to comment out the config.serve_static_asserts = false in config/environments/production.rb. Then the middleware ActionDispatch::Static is added. But it doesn't help.
Investigation #3
Change the settings on config/environments/production.rb to those in config/environments/development.rb one by one to locate what makes the difference. Luckily, the first one hits. It is the config.cache_classes=true on staging environment that leaded to the error. The model column was cached and not reloaded after add_column. So, the assign and save of a record resulted in error. To solve the problem, after add_column, do a reset_column_information. i.e.
def self.up
add_column :records, :valuation_date, :date
Record.reset_column_information
Record.find_each do |record|
record.valuation_date = ...
record.save
end
end
Problem solved!
def self.up
add_column :records, :valuation_date, :date
Record.find_each do |record|
record.valuation_date = ...
record.save
end
end
The deployment (rake db:migrate) works fine on development environment. However, when doing cap deploy:migrations, it returns error:
== AddValutionDateToRecords: migrating ===================================
-- add_column(:records, :valuation_date, :date)
-> 0.0014s
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `column' for nil:NilClass
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Turns out this only happens when running the migration under rails' staging environment. Then tried execute the db migration and trace the issue.
$ bundle exec rake RAILS_ENV=staging db:migrate --trace
undefined method `relation' for nil:NilClass
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/arel-2.0.10/lib/arel/crud.rb:12:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/persistence.rb:266:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/locking/optimistic.rb:77:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/attribute_methods/dirty.rb:68:in `update'
/Users/laileo/.rvm/gems/ree-1.8.7-2011.03@merlot/gems/activerecord-3.0.11/lib/active_record/timestamp.rb:60:in `update'
...
To look deeper, set a "debugger" breakpoint before record.save. Then another 2 breakpoints on crud.rb:12 .../active_record/base.rb:1718. It was found that record.class.arel_table['valuation_date'] is nil that caused the failure.
Investigation #1
Check if the Arel using different version in different environments. In debug mode, $ p Arel::VERSION show they are the same.
Investigation #2
Compare the difference between dev and staging:
$ rake about
$ rake RAILS_ENV=staging about
The results show that ActionDispatch::Static middleware is not include in staging. Tried to comment out the config.serve_static_asserts = false in config/environments/production.rb. Then the middleware ActionDispatch::Static is added. But it doesn't help.
Investigation #3
Change the settings on config/environments/production.rb to those in config/environments/development.rb one by one to locate what makes the difference. Luckily, the first one hits. It is the config.cache_classes=true on staging environment that leaded to the error. The model column was cached and not reloaded after add_column. So, the assign and save of a record resulted in error. To solve the problem, after add_column, do a reset_column_information. i.e.
def self.up
add_column :records, :valuation_date, :date
Record.reset_column_information
Record.find_each do |record|
record.valuation_date = ...
record.save
end
end
Problem solved!
rails - rake about
When "rake" fails on certain RAILS_ENV, check and compare the rake
information by rake about.
$ rake RAILS_ENV=staging about
$ rake about
information by rake about.
$ rake RAILS_ENV=staging about
$ rake about
Rails - A closer look on ActonRecord and ARel
http://erniemiller.org/2010/03/28/advanced-activerecord-3-queries-with-arel/
http://erniemiller.org/2010/05/11/activerecord-relation-vs-arel/
http://www.slideshare.net/flah00/activerecord-arel
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html
http://erniemiller.org/2010/05/11/activerecord-relation-vs-arel/
http://www.slideshare.net/flah00/activerecord-arel
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html
Monday, November 12, 2012
A few notes on selenium IDE usage
- For time consuming actions:
click - some location
waitForPageToLoad
pause
- For page waits for ajax response:
click - ajax button
pause
... next action ...
- Use selenium locator for target:
id=...
css=...
click - some location
waitForPageToLoad
pause
- For page waits for ajax response:
click - ajax button
pause
... next action ...
- Use selenium locator for target:
id=...
css=...
Friday, November 9, 2012
Management practice - Set deadline for every task
No matter which development methodology is using, whether it is Scrum or Kanban, it's a good idea to set a date on which a task should be finished. For Scrum, within a sprint, 2 weeks or 3 weeks, certain PBIs/stories have to be completed. This can push or encourage developers to aim tasks for the deadline. Otherwise, developers may try to refactor and refactor for a better solution or test again and again. There's no perfect solution. Just set a date and let stories be closed on that date. Then start new tasks on next cycle.
Another advantage is to provide a product road map for product owner to foresee and expect certain features to be done on certain milestones.
Another advantage is to provide a product road map for product owner to foresee and expect certain features to be done on certain milestones.
Thursday, November 8, 2012
Selenium IDE - select element by CSS
select element by css:
<tr>
<td>verifyElementPresent</td>
<td>css=span#direct-debit-scheme-menu-title</td>
<td></td>
</tr>
<tr>
<td>verifyElementPresent</td>
<td>css=span#direct-debit-scheme-menu-title</td>
<td></td>
</tr>
Create a bootable USB stick on Mac
references:
http://www.ubuntu.com/download/help/create-a-usb-stick-on-mac-osx
http://renevanbelzen.wordpress.com/2009/10/14/creating-a-bootable-usb-stick-with-mac-os-x-in-10-easy-steps/
$ diskutil list
$ diskutil unmountDisk /dev/diskN (where diskN is the USB drive)
$ mv ...../clonezilla-live-1.2.12-67-amd64.iso ..../clonezilla-live-1.2.12-67-amd64.dmg
$ su
$ dd if=/Users/myself/isoDisks/clonezilla-live-1.2.12-67-amd64.dmg of=/dev/disk5 bs=1m
$ diskutil list
$ diskutil eject /dev/disk5
http://www.ubuntu.com/download/help/create-a-usb-stick-on-mac-osx
http://renevanbelzen.wordpress.com/2009/10/14/creating-a-bootable-usb-stick-with-mac-os-x-in-10-easy-steps/
$ diskutil list
$ diskutil unmountDisk /dev/diskN (where diskN is the USB drive)
$ mv ...../clonezilla-live-1.2.12-67-amd64.iso ..../clonezilla-live-1.2.12-67-amd64.dmg
$ su
$ dd if=/Users/myself/isoDisks/clonezilla-live-1.2.12-67-amd64.dmg of=/dev/disk5 bs=1m
$ diskutil list
$ diskutil eject /dev/disk5
Rails - before_filter method parameter
before_filter { |c| c.some_method(some_parameter) }
Wednesday, November 7, 2012
Clone hard disk
http://www.techmixer.com/free-clone-hard-disk-software-to-clone-your-hard-disk-data/
Tried Clonezilla, works great!
Tried Clonezilla, works great!
Rescue failed desktop/server/laptop
SystemResuceCD
Keyword: System, Failure, Windows, Linux, Boot, CD
Keyword: System, Failure, Windows, Linux, Boot, CD
Friday, November 2, 2012
Rails note - Builds single purpose controller method/route
Just a simple note:
In Rails application, it would be better for every controller
method/route serves single purpose, instead of multiple purposes and
switch by parameter options.
This approach provides a cleaner view for system maintenance and auditing.
In Rails application, it would be better for every controller
method/route serves single purpose, instead of multiple purposes and
switch by parameter options.
This approach provides a cleaner view for system maintenance and auditing.
SSH tunnel vs VPN in short
Both: Encrypted data for secure connection.
VPN: Operating System level security
SSH: Application level security
VPN: Share intranet resource such as file shares, network printer, etc
SSH: Setup per individual resources.
VPN: Easy for client usage. More to do on server setup.
SSH: Not easy for novice user. Easy for SSH server setup.
VPN: More secure for business use. You can force all network traffic on
system through VPN.
SSH: Good for single purpose connection. e.g. remote access intranet
server.
VPN: Operating System level security
SSH: Application level security
VPN: Share intranet resource such as file shares, network printer, etc
SSH: Setup per individual resources.
VPN: Easy for client usage. More to do on server setup.
SSH: Not easy for novice user. Easy for SSH server setup.
VPN: More secure for business use. You can force all network traffic on
system through VPN.
SSH: Good for single purpose connection. e.g. remote access intranet
server.
Subscribe to:
Posts (Atom)