Hi,
I often find myself using bits of code like this inside ActiveRecord,
perhaps it’s useful for others, or others can improve on it:
###########
fix user input before validating it
before_validation :sanitize_input
santize input before actual validation is called
this uses the little methods defined below
def sanitize_input
trim %w(adres postcode woonplaats email naam telefoon mobiel)
downcase :email
empty_to_nil %w(telefoon mobiel)
end
pass in a list of fields whose values will be converted to nil
you should only use these on objects that respond to empty?
if the value is empty (this causes empty strings ‘’ to become nils)
def empty_to_nil(fields)
fields.to_a.each {|f| self[f] = nil if self[f].empty? }
end
pass in a list of fields whose values will be trimmed
def trim(fields)
fields.to_a.each {|f| self[f].strip! }
end
pass in a list of fields of this object that will be lowercased
useful for accepting only unique emails
def downcase(fields)
fields.to_a.each {|f| self[f].downcase! }
end
##########
HTH,
Jeroen
Hello Jeroen,
I often find myself using bits of code like this inside ActiveRecord,
perhaps it’s useful for others, or others can improve on it:
thanks to share this with us.
empty_to_nil %w(telefoon mobiel)
end
pass in a list of fields whose values will be converted to nil
you should only use these on objects that respond to empty?
if the value is empty (this causes empty strings ‘’ to become nils)
def empty_to_nil(fields)
fields.to_a.each {|f| self[f] = nil if self[f].empty? }
end
Another way is to make it more DSLish :
def empty_to_nil(*fields)
fields.each {|f| self[f] = nil if self[f].empty? }
end
and the same for trim and downcase (you get the idea).
so that sanitize_input looks like that :
def sanitize_input
trim :adres, :postcode, :woonplaats, :email, :naam, :telefoon,
:mobiel
downcase :email
empty_to_nil :telefoon, :mobiel
end
Regards,
-- Jean-François.
Jean-François wrote:
Hello Jeroen,
I often find myself using bits of code like this inside ActiveRecord,
perhaps it’s useful for others, or others can improve on it:
thanks to share this with us.
No problem
def sanitize_input
trim :adres, :postcode, :woonplaats, :email, :naam, :telefoon, :mobiel
downcase :email
empty_to_nil :telefoon, :mobiel
end
Cool, it does look a little more straightforward like that, thanks!
Jeroen
On May 2, 2006, at 7:27 AM, Jeroen H. wrote:
[…]
downcase :email
[…]
pass in a list of fields of this object that will be lowercased
useful for accepting only unique emails
def downcase(fields)
fields.to_a.each {|f| self[f].downcase! }
end
Note that this is not safe to do in general: the local-part of an
email address must be treated as case sensitive [RFC 2821]. Although
it might not hurt you in practice, you risk interoperability with
systems that don’t ignore the case of mailbox names.
–
Rob L.
[email protected]
Jeroen H. wrote:
santize input before actual validation is called
def empty_to_nil(fields)
def downcase(fields)
fields.to_a.each {|f| self[f].downcase! }
end
##########
Slightly improved version:
before_validation :sanitize_input
private
santize input before actual validation is called
def sanitize_input
strip :address, :postcode, email, :phone
downcase :email
empty_to_nil :phone
end
pass in the fields whose values will be converted to nil
if the value is empty (this causes empty form elements to become nils)
you should only use these on objects that respond to empty? (it’s
really only meant for strings)
def empty_to_nil(*fields)
fields.each {|f| self[f] = nil if self[f].empty? unless self[f].nil?
}
end
pass in a list of fields whose values will be trimmed
def strip(*fields)
fields.each {|f| self[f].strip! unless self[f].nil? }
end
pass in a list of fields of this object that will be lowercased
useful for accepting only unique emails
def downcase(*fields)
fields.each {|f| self[f].downcase! unless self[f].nil? }
end
HTH,
Jeroen