Class: Person

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/person.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_email_address(address) ⇒ Object



39
40
41
42
43
44
# File 'app/models/person.rb', line 39

def self.find_by_email_address(address)
  ea = EmailAddress.find_by_address(address)
  if not ea.nil?
    return ea.person
  end
end

.sreg_mapObject



9
10
11
12
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
# File 'app/models/person.rb', line 9

def self.sreg_map  
  {:fullname => Proc.new do |fullname|
    if fullname =~ /^([^ ]+) +(.*)$/
      {:firstname => $1, :lastname => $2}
    else
      {:firstname => fullname}
    end
  end, 
  :dob => Proc.new do |dob|
    if dob =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/
      {:birthdate => Time.local($1, $2, $3)}
    else
      {}
    end
  end,
  :gender => Proc.new do |gender|
    if gender == 'M'
      {:gender => 'male'}
    elsif gender == 'F'
      {:gender => 'female'}
    else
      {}
    end
  end,
  :email => Proc.new do |email|
    {:primary_email_address => email}
  end
  }
end

Instance Method Details

#administrator?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/person.rb', line 116

def administrator?
  administrator_classes.length > 0
end

#administrator_classesObject



110
111
112
113
114
# File 'app/models/person.rb', line 110

def administrator_classes
  AeUsers.permissioned_classes.select do |c|
    permitted?(c, "change_permissions_#{c.name.tableize}")
  end
end

#age_as_of(base = Date.today) ⇒ Object



124
125
126
127
128
# File 'app/models/person.rb', line 124

def age_as_of(base = Date.today)
  if not birthdate.nil?
    base.year - birthdate.year - ((base.month * 100 + base.day >= birthdate.month * 100 + birthdate.day) ? 0 : 1)
  end
end

#all_permissionsObject



66
67
68
69
70
71
72
# File 'app/models/person.rb', line 66

def all_permissions
  allperms = permissions
  roles.each do |role|
    allperms += role.permissions
  end
  return allperms
end

#app_profileObject



130
131
132
133
# File 'app/models/person.rb', line 130

def app_profile
  @app_profile ||= AeUsers.profile_class.find_by_person_id(id)
  @app_profile
end

#current_ageObject



120
121
122
# File 'app/models/person.rb', line 120

def current_age
  age_as_of Date.today
end

#nameObject



139
140
141
142
143
144
145
146
147
# File 'app/models/person.rb', line 139

def name
  return "#{firstname} #{lastname}"
#    n = firstname
#    if nickname and nickname.length > 0
#      n += " \"#{nickname}\""
#    end
#    n += " #{lastname}"
#    return n
end

#permitted?(obj, perm_name) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/person.rb', line 74

def permitted?(obj, perm_name)
  if AeUsers.cache_permissions?
    if obj and obj.kind_of? ActiveRecord::Base
      return AeUsers.permission_cache.permitted?(self, obj, perm_name)
    else
      return AeUsers.permission_cache.permitted?(self, nil, perm_name)
    end
  else
    return uncached_permitted?(obj, perm_name)
  end
end

#primary_email_addressObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/person.rb', line 46

def primary_email_address
  primary = email_addresses.find_by_primary true
  if not primary
    primary = email_addresses.find :first
  end
  if primary.nil?
    return nil
  else
    return primary.address
  end
end

#primary_email_address=(address) ⇒ Object



58
59
60
61
62
63
64
# File 'app/models/person.rb', line 58

def primary_email_address=(address)
  if primary_email_address != address
    ea = email_addresses.find_or_create_by_address(address)
    ea.primary = true
    ea.save
  end
end

#profileObject



135
136
137
# File 'app/models/person.rb', line 135

def profile
  app_profile
end

#uncached_permitted?(obj, perm_name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/person.rb', line 86

def uncached_permitted?(obj, perm_name)
  result = false
  all_permissions.each do |permission|
    po = permission.permissioned
    
    if po.kind_of? ActiveRecord::Base
      objmatch = (po.class.name == obj.class.name and po.id == obj.id)
    else
      objmatch = (po == obj)
    end
    
    permmatch = (permission.permission == perm_name)
    
    result = ((po.nil? or objmatch) and
      (permission.permission.nil? or permmatch))
    
    if result
      break
    end
  end
  logger.debug "Permission check result: #{result}"
  return result
end