SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie - better way to build address string

    I want to build an address string so that my reports and screens that show addresses won't have empty lines or punctuation hanging out in the middle of nowhere (such as commas inbetween city and state - if there's no city or state, there's a blank line with a comma). I wrote this, but it seems too verbose for ruby. Is there a better way to write this?

    Thank you!



    def full_address
    full_address = ''
    full_address = Company.address_1 if Company.address_1

    if Company.address_2.empty?
    else
    if full_address == ''
    full_address = Company.address_2
    else
    full_address << 'linebreak' + Company.address_2
    end
    end

    if Company.address_3.empty?
    else
    if full_address == ''
    full_address = Company.address_3
    else
    full_address << 'linebreak' + Company.address_3
    end
    end

    if Company.city.empty?
    else
    if full_address == ''
    full_address = Company.city
    else
    full_address << 'linebreak' + Company.city
    end
    end

    if Company.state.empty?
    else
    if full_address == ''
    full_address = Company.state
    else
    if Company.city.empty?
    full_address = full_address + 'linebreak' + Company.state
    else
    full_address << ', ' + Company.state
    end
    end
    end

    if Company.zip.empty?
    else
    if full_address == ''
    full_address = Company.zip
    else
    full_address << ' ' + Company.zip
    end
    end

    if Company.zip4.empty?
    else
    if full_address == ''
    full_address = Company.zip4
    else
    if Company.zip.empty?
    full_address << ' ' + Company.zip4
    else
    full_address << '-' + Company.zip4
    end
    end
    end

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using an array is the easiest way to deal with this.
    It would be worth creating this in your company model.

    Code:
    def address_lines
      [address_1, address_2, address_3, city, state, zip, zip4]
    end
    Once it is in an array you can use compact or compact! to remove any nil elements, and join to create a string in the format you want. So in your view you could use:
    Code:
    Address: <&#37;= company.address_lines.compact.join("\n<br />") -%>
    That will remove the nil elements and then output the remaining elements with a XHTML line break between each one.

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I put the linebreaks and punctuation in? Specifically, a comma after the city (if there's both a city and state) and a dash between the zip and the +4 (if both exist).

    Thanks for your time.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this is a Ruby application you could do the following:
    Code:
    company.address_lines.compact.join(",\n")
    That will give
    Code:
    address_1, 
    address_2, 
    address_3, 
    city, 
    state, 
    zip, 
    zip4
    If it is a Rails app then this will put a comma in as well as the new line break I put in my original
    Code:
    company.address_lines.compact.join(",\n<br />")
    This will give
    Code:
    address_1, 
    <br />address_2, 
    <br />address_3, 
    <br />city, 
    <br />state, 
    <br />zip, 
    <br />zip4
    For the zip code, I'd add another class/model method to company:
    Code:
    def full_zip
      [zip4, zip].join("-")
    end
    I'm not sure which way zip and zip4 should go as that looks nothing like the post codes we use this side of the pond. Then update the address_lines method to
    Code:
    def address_lines
      [address_1, address_2, address_3, city, state, full_zip]
    end

  5. #5
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    This looks like a good solution.

    Thank you very much.


  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •