Rails 3.1.0 has been released!
jQuery as new default Javascript library, streaming response support, attr_accessible with roles, prepared statements, easier migrations.
Web Upd8: Ubuntu / Linux blog
Great blog with daily news and HOWTOs for Ubuntu, Linux and Gnome.
Ruby: Convert a time string to your local time zone
If you have a time given in a different time zone than your local one, parsing will convert it for you:
>> Time.parse('September 2nd, 3pm PST')
=> 2011-09-03 01:00:00 +0200
Note that in pure Ruby you need to require "tzinfo"
(Ruby 1.9) or require "time"
(Ruby 1.8) for Time.parse
to be available.
Windows 7: Open terminal from Explorer
Let's say you have an Explorer window showing a directory and want a cmd
terminal to be opened there.\
While you needed a PowerToy on Windows XP, this is a build-in feature for Windows 7.
Simply press and hold the Shift
key and right-click a folder icon or into empty space of an Explorer window.\
Then, choose "Open Command Window Here" from the context menu.
Fix: "undefined method `bytesize' for #<Array>"
I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:
"undefined method `bytesize' for #<Array>"
Starting the application in Passenger gave me a stacktrace in log/development.log
that pointed to the actual problem.
Possible causes discovered by looking at the logs
-----------------------------------------------------...
Use different code for Rails 2 and Rails 3
When writing a piece of reusable code, you sometimes need to have separate code for Rails 2 and Rails 3. You can distinguish between Rails versions like this:
if Rails.version < '3' # mind the quotes
# Rails 2 code goes here
else
# Rails 3+ code goes here
end
Always show all form errors during development
You've been there: A form cannot be submitted, but you don't see a validation error because the field at fault has no corresponding input field on the form. Because this is usually a bug, you insert debug information listing all errors into the form view. And once the bug is fixed, you forget to take out that debug information.
There is a better way. By copying one of the attached initializers into config/initializers
, your forms will always render a small box listing all form errors in the bottom right corner of the screen. This box is n...
Let Webrat make a POST request
Just add the parameter :post
to the visit
method:
visit publish_entry_path, :post
Webrat doesn't follow redirect because it considers the url external
Rails doesn't know which host it is running on. For generating links, it strips the hostname off the request URL, which can lead to errors when you have absolute URLs in your Cucumber tests.
If you really need to use absolute URLs somewhere, say in an email you send, either throw away the host when parsing it (e.g. body.scan(/http:\/\/[^\/]+\/([^\s"<]+)/)
) or tell Webrat you're back on your site.
Timecop creates records in the past after calling Timecop.freeze
This is a bug in Timecop 0.3.4 or lower. You should upgrade to 0.3.5.
Time#utc, Time#gmt and Time#localtime are destructive methods
Calling Time#utc
, Time#gmt
or Time#localtime
will not create a converted copy. Instead these methods modify the receiving Time
object:
>> time = Time.now
=> Thu Aug 25 09:52:28 +0200 2011
>> time.utc
=> Thu Aug 25 07:52:28 UTC 2011
>> time
=> Thu Aug 25 07:52:28 UTC 2011
This can have unexpected side effects when other code is holding pointers to the Time
object you are modifying. To be safe, call these methods on a clone of the Time
object. You can clone a Ruby object by using [#dup
](http://...
Convert the colorspace of a PDF from RGB to CMYK under Ubuntu Linux
Note that converting from RGB to CMYK will usually degrade your colors because no exact mapping is possible. Anyway, this Stackoverflow post worked for me:
gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
-sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK \
-sOutputFile=output.pdf input.pdf
Rotate a PDF under Ubuntu Linux
Use the PDF toolkit:
sudo apt-get install pdftk
To rotate page 1 by 90 degrees clockwise:
pdftk in.pdf cat 1E output out.pdf # old pdftk
pdftk in.pdf cat 1east output out.pdf # new pdftk
To rotate all pages clockwise:
pdftk in.pdf cat 1-endE output out.pdf # old pdftk
pdftk in.pdf cat 1-endeast output out.pdf # new pdftk
The E
(old pdftk) or east
(new pdftk) is meaningful if you want other rotations. From the man
page:
The page rotation setting...
Fix: Tail says "no space left on device"
Linux provides a fix number of filesystem watches. If you have some greedy daemon (like dropbox) running, chances are it uses all of them, and you cannot use tail -f
any more.
To increase the number of watches, put something like
fs.inotify.max_user_watches = 32768
into /etc/sysctl.conf
. (default is 8192)
To update the setting right away, run
sudo sysctl -p /etc/sysctl.conf
afterwards.
Fix Rubygems warning: Gem.source_index is deprecated, use Specification
After updating Rubygems you see a wall of deprecation warnings like this:
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from /usr/local/lib/site_ruby/1.8/rubygems/source_index.rb:197.
NOTE: Gem::SourceIndex#add_specs is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#spec_dirs= called from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.14/lib/bundler/rubygems_integration.rb:175
...
Who broke the build?
Retaliation is a Jenkins CI build monitor that automatically coordinates a foam missile counter-attack against the developer who breaks the build. It does this by playing a pre-programmed control sequence to a USB Foam Missile Launcher to target the offending code monkey.
Downgrade Firefox 6 to Firefox 5 on Ubuntu
Note that if you plan to downgrade Firefox because your Selenium tests broke after a Firefox upgrade, there is a better way that doesn't involve downgrading. Mozilla has stated that they will no longer provide security patches for any but the most recent versions of Firefox. So running an old Firefox should not be a long-term solution for anything.
If you still want to downgrade your Firefox for other reasons, here is how I downgra...
Prevent your Firefox from auto-updating
Note that if you plan to freeze your Firefox versions because your Selenium tests break whenever Firefox updates, there is a better way that lets you keep an up-to-date Firefox. Mozilla has stated that they will no longer provide security patches for any but the most recent versions of Firefox. So running an old Firefox should not be a long-term solution for anything.
If you still wish to disable the auto-update in Firefox, a poste...
An ActiveRecord is invalid, but has no errors
Did you return false
in a before_validation
callback?
Distance of time in what you like: days, months, years
Sometimes the Rails helper #distance_of_time_in_words
is using too much magic.
When you need a time difference in a specific unit, use this method:
^
def distance_of_time_in(unit, from, to)
diff = to - from
if 1.respond_to? unit
distance = diff / 1.send(unit)
distance.abs.round
else
raise ArgumentError, "#{unit.inspect} is not supported as unit"
end
end
distance_of_time_in(:days, Time.now, 1.year.ago)
=> 365
Remove the .abs
if you want the mathematical *differ...
Print large PDFs as a poster
Pdfposter is a Python script that allows to convert large PDFs into a PDF with multiple pages that can be printed and turned into one big poster.
In Ubuntu, you can install it with
sudo apt-get install pdfposter
Scaling to the desired size is a bit cumbersome. If you want to split large.pdf
, I suggest you run
pdfposter -vns 0.5 large.pdf larger.poster.pdf
which will tell you into how many pages the file would be split. Then simply tweak the -s
value till you get the desired number of pages, and remove the -n
to generate the r...
Best GitHub feature
When browsing a repository, pressing "t" allows you to quickly search for file names. Very awesome!
Go here to try it out.
Random list of ActiveSupport goodies
I recently browsed through the ActiveSupport code and found some nice stuff I did not know about:
ActiveSupport::Callbacks
-
ActiveRecord-like callbacks, if you need callbacks in non ActiveRecord objects
ActiveSupport::MessageEncryptor
-
encrypt and decrypt ruby objects
[ActiveSupport::MessageVerifier
](https://github.com/rails/rails/blob/mast...