niedziela, 6 grudnia 2009

Walidacja numeru IBAN (konta bankowego) w Ruby on Rails

Więcej na temat samego numeru i genezy algorytmu można znaleźć na wikipedi http://pl.wikipedia.org/wiki/International_Bank_Account_Number

validates_each(:iban) do |record, attr, value|
iban = value.to_s.gsub(" ", "")
unless iban =~ /\A(PL)?[0-9]{26}\Z/
record.errors.add(attr, :invalid)
else
iban = iban[2..27] if iban.size == 28 # skip PL, size: 26
iban << "2521" # size 30
iban = iban[2..29] + iban[0..1] # size 30
if iban.to_i % 97 != 1
record.errors.add(attr, :invalid)
end
end
end

wtorek, 1 grudnia 2009

How to Intercept all calls to I18n.t() method to see its parameters.

Sometimes it is not easy to understand the documentation of Rails or some library when its explaining the way it calls I18n. Here is my way to easily figure it out. You can put it in your environement.rb . Running it only in development mode is recommended.


I18n.class_eval do
class << self

def translate_with_output(*args)
puts "Translation called with arguments: "
args.each{|a| puts a.inspect}
translate_without_output(*args)
end

alias_method_chain :translate, :output
alias_method :t, :translate
end
end