Thursday 2 July 2009

config.gem warnings?! I don't care, just make it stop ..

Unpacked some gems that will be using for my app, well, i don't want to use system gems for various reasons. But when start the app, got a few warnings come up which is quite annoying
config.gem: Unpacked gem blahblah in vendor/gems has no specification file. Run 'rake gems:refresh_specs' to fix this
To turn it off, just include this line in your environment file before the initialiser config block
Rails::VendorGemSourceIndex.silence_spec_warnings = true

Thursday 4 June 2009

Send email with PDF attachment in Rails


I am trying to send an email with a PDF attachment via Action Mailer, but somehow when opening email the attachment was corrupted. Been banging my head against the wall for few hours, finally I realised it was because of my Windows development box (I hate you, windows).


So, by default File.read on Linux will open the file in Binary mode in but in Text mode on Windows. To fix this I need to open the file in binary mode before I read it in:

attachment "application/pdf" do |a|
a.filename = File.basename(order_form_content)
File.open(order_form_content, 'rb') do |file|
a.body = file.read
end
end

Security hole with http digest authentication


I came across this blog few days ago, it was a great spot, thought I posted here to help me easy to remember it.


The hole seems to be found in the new version of Rails 2.3, HTTP digest Authentication. You can log in even without an username and password. But HTTP basic authetication doesn't seems to have this problem.

Anyway, read it more here at Nate's Blog

Tuesday 12 May 2009

Ruby Math - BigDecimal


Let's have a look at this example:

1.2 + 1.0 == 2.2 (returns true)
but if I do:

1.2 - 1.0 == 0.2 (returns false)
Hmmm ... strange. This comes down to the way computers generally store numbers in binary but not in base10 system. The system that human generally work and think in.

Anyway, to fix this problem, I use Ruby built-in class BigDecimal

require 'bigdecimal'
require 'bigdecimal/util'
1.2.to_d - 1.0.to_d == 0.2.to_d (returns true)

Reference:

http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html
http://ramblingsof.justinwinkler.com/when-crap-just-doesnt-add-up

I currelty work for UK Business Directory - Touch Local, check out our awesome site written in Ruby on Rails