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



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

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

.sreg_mapObject



8
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
# File 'app/models/person.rb', line 8

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)


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

def administrator?
  administrator_classes.length > 0
end

#administrator_classesObject



106
107
108
109
110
# File 'app/models/person.rb', line 106

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



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

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



62
63
64
65
66
67
68
# File 'app/models/person.rb', line 62

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

#app_profileObject



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

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

#current_ageObject



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

def current_age
  age_as_of Date.today
end

#nameObject



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

def name
  if firstname.blank? && lastname.blank?
    "Anonymous User"
  else
    "#{firstname} #{lastname}"
  end
#    n = firstname
#    if nickname and nickname.length > 0
#      n += " \"#{nickname}\""
#    end
#    n += " #{lastname}"
#    return n
end

#permitted?(obj, perm_name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/person.rb', line 70

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



45
46
47
48
49
50
51
52
# File 'app/models/person.rb', line 45

def primary_email_address
  primary = email_addresses.detect(&:primary) || email_addreses.first
  if primary.nil?
    return nil
  else
    return primary.address
  end
end

#primary_email_address=(address) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/person.rb', line 54

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



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

def profile
  app_profile
end

#uncached_permitted?(obj, perm_name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/person.rb', line 82

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