Add database field expression (such as Person::Fullname)

class Person < AR::Base

  def fullname
    first_name + " " + lastname
  end
end

p = Person.create(:first_name => "Joe", :last_name => "Smith")
p.first_name # "Joe"
p.last_name # "Smith"
p.fullname # "Joe Smith"

I actually prefer

def fullname
“#{self.first_name} #{self.lastname}”
end

because I don’t get exceptions if someone leaves first_name or last_name nil. (That and I like scoping my vars, though that’s not always necessary.)

Brian Hogan wrote the following on 18.07.2007 16:46 :

I actually prefer

def fullname
  "#{self.first_name} #{self.lastname}"
end

When I said 'extend' I had one use-case in mind, here's a part of one librairy I coded:

    def fullname
        if firstname.blank? && lastname.blank?
            return _('Unknown (fill your firstname and lastname in)')
        elsif firstname.blank? || lastname.blank?
            # no need for space, one is empty
            return "#{firstname}#{lastname}"
        else
            return "#{firstname} #{lastname}"
        end
    end

Notes:
- _() is from ruby-gettext,
- the .blank? are Rails specific IIRC,
- the return are superfluous, I'm in the process of ditching some of them where they don't help readability.

This would be quite difficult to do in a SQL view...

Lionel.

Brian Hogan wrote the following on 18.07.2007 16:46 :

I actually prefer

def fullname
  "#{self.first_name} #{self.lastname}"
end

- _() is from ruby-gettext,

I'm surprised that in a localized app you're using attributes such as "firstname" and "lastname" :slight_smile:

This would be quite difficult to do in a SQL view...

SELECT CASE WHEN firstname = '' THEN lastname
             WHEN lastname = '' THEN firstname
             ELSE firstname || ' ' || lastname
        END AS fullname
FROM ...

But I agree it's easy to handle in the app, and especially for localizations.

Michael Glaesemann
grzm seespotcode net

Michael Glaesemann wrote the following on 19.07.2007 01:59 :

- _() is from ruby-gettext,
    
I'm surprised that in a localized app you're using attributes such as "firstname" and "lastname" :slight_smile:

I was unaware of cultures where people are named by other means, care to point me to them?

Lionel.

Well, for example, Yukihiro Matsumoto would have his name order reversed in Japanese. In that case, what is *first* depends on the localization.

Michael Glaesemann
grzm seespotcode net

Michael Glaesemann wrote the following on 19.07.2007 03:21 :

Well, for example, Yukihiro Matsumoto would have his name order reversed in Japanese. In that case, what is *first* depends on the localization.
  
Ok. It seems fortunate for me that I'm still far from adressing the Japanese market.
The problem is probably a vocabulary one (english isn't my mother tongue) is forname/surname more suited?
It seems a migration is not far away...

Lionel.

Depends on how complicated you want to make it :slight_smile: I tend to go with given name, family name myself, but that still doesn't cover all the variations of names around the world. Though I like surname, the "fore" in forename also carries the meaning of in front, which has the same issues as first name. But like I said, even these don't cover the full range of human naming schemes.

Michael Glaesemann
grzm seespotcode net

I love threads like these :slight_smile: