profile.rb
3.67 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
# A Profile is the representation and web-presence of an individual or an
# organization. Every Profile is attached to its Environment of origin,
# which by default is the one returned by Environment:default.
class Profile < ActiveRecord::Base
PERMISSIONS[:profile] = {
'edit_profile' => N_('Edit profile'),
'destroy_profile' => N_('Destroy profile'),
'manage_memberships' => N_('Manage memberships'),
'post_content' => N_('Post content'),
'edit_profile_design' => N_('Edit profile design'),
}
after_create do |profile|
homepage = Article.new
homepage.title = profile.name
homepage.parent = Comatose::Page.root
homepage.slug = profile.identifier
homepage.save!
end
after_destroy do |profile|
article = Article.find_by_path(profile.identifier)
article.destroy if article
end
acts_as_accessible
acts_as_design
acts_as_ferret :fields => [ :name ]
# 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
system
myprofile
profile
cms
community
test
search
not_found
]
acts_as_taggable
belongs_to :user
has_many :domains, :as => :owner
belongs_to :environment
has_many :role_assignments, :as => :resource
has_many :people, :through => :role_assignments
# 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
validates_uniqueness_of :identifier
# 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
# Environment (see Environment#default), unless you tell it
# otherwise
def initialize(*args)
super(*args)
self.environment ||= Environment.default
end
# Searches tags by tag or name
def self.search(term)
find_tagged_with(term) + find_all_by_name(term)
end
def homepage(reload = false)
@homepage = nil if reload
@homepage ||= Article.find_by_path(self.identifier)
end
# Returns information about the profile's owner that was made public by
# him/her.
#
# The returned value must be an object that responds to a method "summary",
# which must return an array in the following format:
#
# [
# [ 'First Field', first_field_value ],
# [ 'Second Field', second_field_value ],
# ]
#
# This information shall be used by user interface to present the
# information.
#
# In this class, this method returns nil, what is interpreted as "no
# information at all". Subclasses must override this method to provide their
# specific information.
def info
nil
end
# returns the contact email for this profile. By default returns the the
# e-mail of the owner user.
#
# Subclasses may -- and should -- override this method.
def contact_email
self.user ? self.user.email : nil
end
# gets recent documents in this profile.
#
# +limit+ is the maximum number of documents to be returned. It defaults to
# 10.
def recent_documents(limit = 10)
homepage.children.find(:all, :limit => limit, :order => 'created_on desc')
end
def superior_instance
environment
end
end