Forum: Ruby on Rails useful bit of code (hopefully)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-05-03 18:59
(Received via mailing list)
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
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2006-05-03 19:05
(Received via mailing list)
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 *DSL*ish :

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.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-05-03 19:06
(Received via mailing list)
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
114892d0cdbc61d4e686dac70495a22b?d=identicon&s=25 Rob Leslie (Guest)
on 2006-05-04 01:21
(Received via mailing list)
On May 2, 2006, at 7:27 AM, Jeroen Houben 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 Leslie
rob@mars.org
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-05-04 13:09
(Received via mailing list)
Jeroen Houben 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
This topic is locked and can not be replied to.