Class: Person
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Person
- Defined in:
- app/models/person.rb
Class Method Summary collapse
- .find_by_address(string) ⇒ Object
- .find_by_email(addr) ⇒ Object
- .find_by_nickname(nick) ⇒ Object
- .find_by_phone_number(number) ⇒ Object
- .find_or_create_by(hash) ⇒ Object
- .is_name_prefix?(text) ⇒ Boolean
- .is_name_suffix?(text) ⇒ Boolean
- .lookup(name) ⇒ Object
- .name_components(name, transformer = nil) ⇒ Object
Instance Method Summary collapse
- #add_address(address_line, label = "Home") ⇒ Object
- #add_email(address, label = "Home") ⇒ Object
- #add_phone(number, label = "Home") ⇒ Object
- #address ⇒ Object
- #address=(new_address) ⇒ Object
- #age ⇒ Object
- #age=(new_age) ⇒ Object
- #as_api_json(options = {}) ⇒ Object
- #email ⇒ Object
- #email=(address) ⇒ Object
- #initials ⇒ Object
- #name ⇒ Object
- #name=(incoming_name) ⇒ Object
- #phone ⇒ Object
- #phone=(number) ⇒ Object
- #user ⇒ Object
- #user=(u) ⇒ Object
Class Method Details
.find_by_address(string) ⇒ Object
237 238 239 240 |
# File 'app/models/person.rb', line 237 def self.find_by_address(string) a = Address.find_by_address(string) a.person if a and a.addressable_type == self.name end |
.find_by_email(addr) ⇒ Object
227 228 229 230 |
# File 'app/models/person.rb', line 227 def self.find_by_email(addr) e = Email.where(address: Email.canonicalize(addr), emailable_type: self.name) e.person if e end |
.find_by_nickname(nick) ⇒ Object
223 224 225 |
# File 'app/models/person.rb', line 223 def self.find_by_nickname(nick) person = self.includes(:nicknames).joins(:nicknames).find_by("nicknames.nickname" => nick) end |
.find_by_phone_number(number) ⇒ Object
232 233 234 235 |
# File 'app/models/person.rb', line 232 def self.find_by_phone_number(number) p = Phone.where(number: Phone.canonicalize(number), phoneable_type: self.name) p.person if p end |
.find_or_create_by(hash) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'app/models/person.rb', line 214 def self.find_or_create_by(hash) = hash.clone if .has_key?(:name) = .merge(self.name_components([:name])) .delete(:name) end super() end |
.is_name_prefix?(text) ⇒ Boolean
83 84 85 86 |
# File 'app/models/person.rb', line 83 def self.is_name_prefix?(text) possibles = %w(mr mrs ms miss dr professor prof) result = possibles.include?(text.gsub(/[.]*/, "").downcase) if text.present? end |
.is_name_suffix?(text) ⇒ Boolean
88 89 90 91 |
# File 'app/models/person.rb', line 88 def self.is_name_suffix?(text) possibles = %w(esq phd jr iii ii) result = possibles.include?(text.gsub(/[.]*/, "").downcase) if text.present? end |
.lookup(name) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/person.rb', line 13 def self.lookup(name) return nil if not name person = self.find_by(self.name_components(name)) person ||= self.includes(:nicknames).joins(:nicknames).find_by("nicknames.nickname" => name) # Maybe we're looking up by phone number? if not person phone = Phone.where(number: Phone.canonicalize(name)).first rescue nil person = phone.phoneable if phone end if not person email = Email.where(emailable_type: self.name.to_s, address: Email.canonicalize(name.downcase)).first person = email.emailable if email end if not person # Try hard to find a person by their initials, even if there wasn't a nickname for them. people = self.all.collect {|p| [p.id, p.initials]} people.each do |parry| if parry[1] == name.upcase person = self.find(parry[0]) break end end end person end |
.name_components(name, transformer = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/person.rb', line 93 def self.name_components(name, transformer=nil) res = {} component = "" components = name.gsub(/,/, " ").gsub(/ /, " ").split(" ") rescue [name] num_parts = components.length component = components.shift # What kind of thing is this? if is_name_prefix?(component) res[:prefix] = component res[:prefix] = res[:prefix].send(transformer) if res[:prefix] && transformer res[:fname] = components.shift else res[:fname] = component end res[:fname] = res[:fname].send(transformer) if res[:fname] && transformer # Next up, middle initial or last name. # If only one word remains, that's the last name if components.length == 1 res[:lname] = components.shift res[:lname] = res[:lname].send(transformer) if res[:lname] && transformer elsif components.length > 0 # At least 2 words remain. We might have middle names, prefixes, suffixes, etc. components.reverse! component = components.shift if is_name_suffix?(component) res[:suffix] = component res[:suffix] = res[:suffix].send(transformer) if res[:suffix] && transformer res[:lname] = components.shift else res[:lname] = component end res[:lname] = res[:lname].send(transformer) if res[:lname] && transformer res[:minitial] = components.shift res[:minitial] = res[:minitial].send(transformer) if res[:minitial] && transformer end res[:minitial] = res[:minitial].gsub(/[.]/, "") if res[:minitial].present? res end |
Instance Method Details
#add_address(address_line, label = "Home") ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'app/models/person.rb', line 259 def add_address(address_line, label="Home") if self != self.class.find_by_address(address_line) a = Address.parse(address_line) a.addressable_type = self.class.name a.label = Label.get(label) a.save addresses << a end a = Address.find_by_address(address_line) end |
#add_email(address, label = "Home") ⇒ Object
250 251 252 253 254 255 256 257 |
# File 'app/models/person.rb', line 250 def add_email(address, label="Home") address = Email.canonicalize(address) if self != self.class.find_by_email(address) e = Email.create(address: address, emailable_type: self.class.name, label: Label.get(label)) emails << e end e = Email.where(address:address, emailable_type: self.class.name) end |
#add_phone(number, label = "Home") ⇒ Object
242 243 244 245 246 247 248 |
# File 'app/models/person.rb', line 242 def add_phone(number, label="Home") if self != self.class.find_by_phone_number(number) p = Phone.create(number: number, phoneable_type: self.class.name, label: Label.get(label)) phones << p end p = Phone.where(number: Phone.canonicalize(number), phoneable_type: self.class.name) end |
#address ⇒ Object
192 193 194 195 |
# File 'app/models/person.rb', line 192 def address a = self.addresses.where(label: Label.get("Work")).first a ||= self.addresses.first end |
#address=(new_address) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/models/person.rb', line 197 def address=(new_address) candidate = nil if new_address.is_a?(Address) candidate = new_address elsif %w(Integer Fixnum Bignum).include?(new_address.class.to_s) candidate = Address.find(new_address) else parsed = Address.parse(new_address) candidate = self.addresses.where(line1: parsed.line1).first candidate ||= Address.new parsed.attributes.each {|key, val| candidate.send((key + "=").to_sym, val) unless val == nil } candidate.save end self.addresses << candidate unless self.addresses.include?(candidate) self.save end |
#age ⇒ Object
183 184 185 |
# File 'app/models/person.rb', line 183 def age ((Date.today - self.birthdate).to_i / 365) if self.birthdate.present? end |
#age=(new_age) ⇒ Object
187 188 189 190 |
# File 'app/models/person.rb', line 187 def age=(new_age) self.birthdate = (Date.today - (rand(Date.today.month - 1))) - new_age.years self.birthdate end |
#as_api_json(options = {}) ⇒ Object
270 271 272 273 274 275 |
# File 'app/models/person.rb', line 270 def as_api_json(={}) candidate = self.as_json() candidate[:emails] = self.emails.collect {|e| { label: e.label, address: e.address }} candidate[:addresses] = self.addresses.collect {|a| { label: a.label, address: a.address }} candidate end |
#email ⇒ Object
52 53 54 |
# File 'app/models/person.rb', line 52 def email self.emails.first end |
#email=(address) ⇒ Object
56 57 58 59 60 |
# File 'app/models/person.rb', line 56 def email=(address) e = self.emails.where(address: Email.canonicalize(address)).first_or_initialize() if not address.blank? self.emails << e if e and not emails.include?(e) e end |
#initials ⇒ Object
140 141 142 143 144 145 146 |
# File 'app/models/person.rb', line 140 def initials res = "" res += fname[0] if fname res += minitial[0] if minitial res += lname[0] if lname res.upcase end |
#name ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/models/person.rb', line 148 def name components = [] if prefix.present? if not prefix.include?(".") and not prefix =~ /^miss$/i components << "#{prefix}." else components << prefix end end components << self.fname if self.fname.present? if minitial.present? if minitial.length < 2 components << "#{minitial}." else components << minitial end end components << self.lname if self.lname.present? components << ", #{suffix}" if self.suffix.present? components.join(" ").strip.gsub(/ ,/, ",") end |
#name=(incoming_name) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'app/models/person.rb', line 174 def name=(incoming_name) components = self.class.name_components(incoming_name) self.prefix = components[:prefix] self.fname = components[:fname] self.minitial = components[:minitial] self.lname = components[:lname] self.suffix = components[:suffix] end |
#phone ⇒ Object
62 63 64 65 66 |
# File 'app/models/person.rb', line 62 def phone p = self.phones.find_by(label: Label.get("Work")) p ||= self.phones.first p end |
#phone=(number) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/person.rb', line 68 def phone=(number) if number.is_a?(Phone) self.phones << number unless self.phones.include?(number) return end if not number.blank? p = self.phones.first rescue nil p ||= Phone.create(phoneable_type: self.class.name, label: Label.get("Work")) self.phones << p unless self.phones.include?(p) p.number = number p.save end end |
#user ⇒ Object
44 45 46 |
# File 'app/models/person.rb', line 44 def user self.users.first end |
#user=(u) ⇒ Object
48 49 50 |
# File 'app/models/person.rb', line 48 def user=(u) self.users = [u] end |