# Useful bit of code (hopefully)

**URL:** <https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384>\
**Category:** Rails\
**Created:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384 "2006-05-03T18:59:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384/1 "2006-05-03T18:59:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384/2 "2006-05-03T19:05:26Z")

</div>

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.
```

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [May 3, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384/3 "2006-05-03T19:06:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [May 4, 2006, 1:21am UTC](https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384/4 "2006-05-04T01:21:25Z")

</div>

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.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [May 4, 2006, 1:09pm UTC](https://www.ruby-forum.com/t/useful-bit-of-code-hopefully/59384/5 "2006-05-04T13:09:22Z")

</div>

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
