From ffd7927573debca2a1c8792b2d93b4bbab4d011d Mon Sep 17 00:00:00 2001 From: Marnen Laibow-Koser Date: Tue, 24 Mar 2009 11:14:25 -0400 Subject: [PATCH] Make nil-safe and case-insensitive. [#18] --- app/models/user.rb | 3 +-- spec/models/user_spec.rb | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2f128845..e4e921d8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -59,8 +59,7 @@ def coords # ['Smith', 'John', 'jsmith1@aol.com'] < ['Smith', 'John', 'jsmith2@aol.com'] def <=>(other) attrs = [:lastname, :firstname, :email] - attrs.collect{|a| self.read_attribute a} <=> attrs.collect{|a| other.read_attribute a} - #[self.lastname, self.firstname, self.email] <=> [other.lastname, other.firstname, other.email] + attrs.collect{|a| self[a].downcase rescue nil}.compact <=> attrs.collect{|a| other[a].downcase rescue nil}.compact end # Returns the user's name as a string. Order can be :first_last (default) or :last_first. E-mail address will be returned if no name is specified. diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c0de2613..75222121 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -117,10 +117,13 @@ end it "should sort on last name, first name, and e-mail address in that order" do - smith = u(['Smith', 'John', 'jsmith1@aol.com']) + attrs = ['Smith', 'John', 'jsmith1@aol.com'] + smith = u(attrs) (smith <=> u(['Smith', 'John', 'jsmith2@aol.com'])).should == -1 (smith <=> u(['Jones', 'Robert', 'rj123@gmail.com'])).should == 1 (smith <=> u(['Smith', 'Mary', 'aaa@aaa.com'])).should == -1 + (smith <=> u([nil, nil, 'Smitty@aol.com'])).should == -1 # nil-safe + (smith <=> u(attrs.collect(&:downcase))).should == 0 # not case-sensitive end protected