profile.rb
1.73 KB
1
2
3
4
5
6
7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# A Profile is the representation and web-presence of an individual or an
# organization. Every Profile is attached to its VirtualCommunity of origin,
# which by default is the one returned by VirtualCommunity:default.
class Profile < ActiveRecord::Base
after_create do |profile|
homepage = Comatose::Page.new
homepage.title = profile.name
homepage.parent = Comatose::Page.root
homepage.slug = profile.identifier
homepage.save!
end
# Valid identifiers must match this format.
IDENTIFIER_FORMAT = /^[a-z][a-z0-9_]*[a-z0-9]$/
# These names cannot be used as identifiers for Profiles
RESERVED_IDENTIFIERS = %w[
admin
customize
cms
system
community
]
has_many :domains, :as => :owner
belongs_to :virtual_community
has_many :affiliations
has_many :people, :through => :affiliations
# Sets the identifier for this profile. Raises an exception when called on a
# existing profile (since profiles cannot be renamed)
def identifier=(value)
unless self.new_record?
raise ArgumentError.new(_('An existing profile cannot be renamed.'))
end
self[:identifier] = value
end
validates_presence_of :identifier, :name
validates_format_of :identifier, :with => IDENTIFIER_FORMAT
validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS
# A profile_owner cannot have more than one profile, but many profiles can exist
# without being associated to a particular user.
validates_uniqueness_of :user_id, :allow_nil =>true
# creates a new Profile. By default, it is attached to the default
# VirtualCommunity (see VirtualCommunity#default), unless you tell it
# otherwise
def initialize(*args)
super(*args)
self.virtual_community ||= VirtualCommunity.default
end
end