cleaner way to write this...

1 view
Skip to first unread message

David C.

unread,
Aug 22, 2006, 8:52:43 PM8/22/06
to rubyonra...@googlegroups.com

Within the last few days I saw an excellent example of this but can't
find the link. So... I thought I'd ask the experts.

In my addressbook model, I wanted to be able to just say
thing.full_address to return a preformatted version of the customers
entire address. Something like this:

# from addressbook model
def full_address
full = first_name + ' ' + last_name + '<br>' + address1 + '<br>'
unless address2.blank?; full += address2 + "<br>"; end
full += city + ' ' + state + ',' + zip
full
end


I'm sure there's a cleaner more compact way to do this, but this is the
extent of my ruby knowledge at this point.


Any help cleaning / compacting this would be greatly appriciated.

Thanks!

--
Posted via http://www.ruby-forum.com/.

Tom Mornini

unread,
Aug 22, 2006, 9:43:16 PM8/22/06
to rubyonra...@googlegroups.com

Don't put view code (HTML) in your models!

Better to put that in a helper.

--
-- Tom Mornini

_Kevin

unread,
Aug 22, 2006, 9:55:50 PM8/22/06
to Ruby on Rails: Talk

I'm with Tom on this one.
Make a partial that properly formats the address when passed the
object.

FWIW, the format I think you were looking for is like this...

def print_stuff
"#{variable1}-#{variable2}"
end

_Kevin
www.sciwerks.com

Gareth Adams

unread,
Aug 23, 2006, 6:22:47 AM8/23/06
to rubyonra...@googlegroups.com

One thing I'd suggest is writing a function in the model like this:

def full_address
[first_name + ' ' + last_name,
address1,
address2,
city + ' ' + state + ',' + zip].compact # compact removes nil
values from an array
end

and then a helper

def format_address(object)
object.respond_to?("full_address") and object.full_address.join("<br>")
end

Gareth

Straaten, Onno van der

unread,
Aug 23, 2006, 6:35:21 AM8/23/06
to rubyonra...@googlegroups.com
For this I would prefer a view helper method because I don't like html
code in my model.


def full_address(aAddress)
returning html = [] do
html << aAddress.first_name + ' ' + aAddress.last_name +
'<br>' + aAddress.address1 + <br>'
html << aAddress.address2 + "<br>" if !aAddress.address2
html << aAddress.city + ' ' + aAddress.state + ',' +
aAddress zip
end.join("\n")
end


Onno

-----Original Message-----
From: rubyonra...@googlegroups.com
[mailto:rubyonra...@googlegroups.com] On Behalf Of Gareth Adams
Sent: woensdag 23 augustus 2006 12:23
To: rubyonra...@googlegroups.com
Subject: [Rails] Re: cleaner way to write this...


On 8/23/06, David C. <rails-mai...@andreas-s.net> wrote:
>
>
>
> Within the last few days I saw an excellent example of this but can't
> find the link. So... I thought I'd ask the experts.
>
> In my addressbook model, I wanted to be able to just say
> thing.full_address to return a preformatted version of the customers
> entire address. Something like this:
>
> # from addressbook model
> def full_address
> full = first_name + ' ' + last_name + '<br>' + address1 + '<br>'
> unless address2.blank?; full += address2 + "<br>"; end
> full += city + ' ' + state + ',' + zip
> full
> end
>
>
> I'm sure there's a cleaner more compact way to do this, but this is
the
> extent of my ruby knowledge at this point.
>
>
> Any help cleaning / compacting this would be greatly appriciated.
>
> Thanks!

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.

David Coleman

unread,
Aug 23, 2006, 9:08:27 AM8/23/06
to rubyonra...@googlegroups.com
Gareth Adams wrote:
> def full_address
> [first_name + ' ' + last_name,
> address1,
> address2,
> city + ' ' + state + ',' + zip].compact # compact removes nil
> values from an array
> end
>
> and then a helper
>
> def format_address(object)
> object.respond_to?("full_address") and
> object.full_address.join("<br>")
> end
>
> Gareth


Nice. Thanks Gareth!

David Coleman

unread,
Aug 23, 2006, 9:35:32 AM8/23/06
to rubyonra...@googlegroups.com
Gareth Adams wrote:
> def full_address
> [first_name + ' ' + last_name,
> address1,
> address2,
> city + ' ' + state + ',' + zip].compact # compact removes nil
> values from an array
> end

Almost worked. address2.nil? returns false, but address2.blank? returns
true. Since address2 isn't nil, it still shows.

Thanks for pointing me in the right direction!!

David Coleman

unread,
Aug 23, 2006, 10:05:53 AM8/23/06
to rubyonra...@googlegroups.com
FYI, Added the delete_if to get rid of the blank ones:

def full_address
addr = [first_name + ' ' + last_name,


address1,
address2,
city + ' ' + state + ', ' + zip].compact

addr.delete_if {|x| x.blank? }
end

thx.

Reply all
Reply to author
Forward
0 new messages