Respect time zones: Use Time.current and Time.zone.parse
Reading the current time
Don't use Time.now
as it's broken when using time zones. \
Instead, use Time.current
, DateTime.current
or Date.current
(the latter actually not being different, but just use it for good measure).
Parsing strings
Don't use Time.parse
as it only gives you a Time
object.
Use Time.zone.parse
instead for a TimeWithZone
.
Related cards:
When you set date attributes, you should not pass times
As you know, time zones make stuff a bit more difficult but are necessary.
A time-zoned record is converted to UTC using to_s(:db)
to be st...
Database: Scopes, migrations, and indices
Wether you modify an existing named scope or add a new one, or when you write a new query, make sure you have the proper indices.
This particularly applies if you're going to run non-trivial queries of course (admin backends, analytics, etc).
##...
Caching: don't use content_for, it won't work
Do not use content_for
inside a cached view fragment. It won't work because Memcache will just output whatever is in the cache, and not execute such commands.
Cleaner Rspec
When simply checking equality or truthiness then
Instead of:
it "should have role set to admin" do
@user.role.should eql('admin')
end
it "should be valid" do
@user.valid?.should be_true
end
Do:
it { @user.role.shou...
How to run long scripts on production/staging server
In a nutshell, use screen
Why?
Sometimes we are concerned that if our connection gets closed, the process we are running will close too.
The processes or login sessions you establish through screen don't go away. You can resume your...
Rebase your feature branches
Regularly, but at least before merging your feature branches, rebase them to get rid of "fix typo" and "wip" commits.
Getting rid of unnecessary commits
Let's say you do a git rebase -i HEAD~~~~
and have this...
Bundle install in parallel
Gave a shot to the new Bundler 1.4.0RC1 during the weekend and found out it now supports gem installation in parallel. To invoke you need to pass --jobs
parameter with number of threads you want it to run – for me the best performance was achiev...
Profiling Ruby with ruby-prof
require 'ruby-prof'
# you don't need this if you have ruby-prof in your Gemfile
You can set one of the things you want to measure by using (default is PROCESS_TIME
most useful ones are PROCESS_TIME, MEMORY, CPU_TIME):
RubyProf.me...
Killing wkhtmltopdf during cucumber
wkhtmltopdf
hangs on mac during cucumber unless we click on it. The main reason is with the version we use which is 0.11.0_rc1 and in out app/bin we have another version and it is a known issue with these versions. The fix is to go to 0.9.9, to ...
Creating a gem in lib folder
Go to lib folder and use bundler to generate main files for a gem:
$ bundle gem test_gem
create test_gem/Gemfile
create test_gem/Rakefile
create test_gem/LICENSE
create test_gem/README.md
...