December 2006
8 posts
6 tags
Rails with Mongrel on Plesk
I set up Mongrel on a server using Plesk 7. Each Rails app has a single Mongrel webserver1 running on the local interface (port 8001 and up). The Mongrel webservers are not accessible to the outside world. They are proxied through Apache using mod_proxy. Just add this snippet to the vhost.conf for the appropriate domain:
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /...
2 tags
Rails: value before type cast
To access a value in a model before it is type cast, append _before_type_cast.
self.phone_number_before_type_cast
A possible use of this is to remove characters from a string before it is cast to an integer (in a before_save method, for example):
def before_save
self['phone_number'] = phone_number_before_type_cast.gsub(/[^0-9]/,'')
end
2 tags
Ruby: strip all non-numeric characters from a...
Use gsub.
>> str = "222, 000*"
=> "222, 000*"
>> str.gsub!(/[^0-9]/,'')
=> "222000"
2 tags
Ruby: case statements
Known as switch statements in PHP.
case @user.age
when 0 .. 2
"baby"
when 3 .. 6
"little child"
when 7 .. 12
"child"
when 12 .. 18
# Note: 12 already matched by "child"
"youth"
else
"adult"
end
The when and else keywords are not indented - they line up with the opening case.
Docs: http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#case
4 tags
Rails: test fixtures are hashes with special...
If you’ve included fixtures in your unit test:
class UserTest
... the data will be available using:
users(:johnny)
A fixtures object behaves like a hash but is actually a method. It is a “hash with special powers”. To get Johnny’s surname you would do:
users(:johnny).surname
The YAML fixture might look something like this:
johnny:
id: 2
first_name: Johnny
...
2 tags
Handling money in Rails
Rails migrations do not seem to support decimal or float columns with a set precision and scale at the moment, e.g. float(8,2).
The accepted solution seems to be to store prices in pence (or cents) and then convert to a decimal price in pounds (or dollars) in the model. See How To Use Integer Fields As Money on the Rails wiki.
2 tags
Converting CMYK images to RGB colour space with...
CMYK images will not display in most web browsers (Safari is an exception). We added this fix to convert to RGB colour space wherever ImageMagick convert is called:
-colorspace RGB
For example:
convert -colorspace RGB -geometry 400x300 -quality 90 cmyk.jpg rgb.jpg
3 tags
Testing an IMAP connection
Use imtest.
For example:
imtest -m login -p imap localhost
If all is well, output should look something like:
S: * OK servername.com Cyrus IMAP4 v2.2.12-Invoca-RPM-2.2.12-6.fc4 server ready
C: C01 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ MAILBOX-REFERRALS NAMESPACE UIDPLUS ID NO_ATOMIC_RENAME UNSELECT CHILDREN MULTIAPPEND BINARY SORT THREAD=ORDEREDSUBJECT THREAD=REFERENCES...