| Class | Profile |
| In: |
app/models/profile.rb
|
| Parent: | ActiveRecord::Base |
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.
| IDENTIFIER_FORMAT | = | /^#{Noosfero.identifier_format}$/ | Valid identifiers must match this format. | |
| RESERVED_IDENTIFIERS | = | %w[ admin system myprofile profile cms community test search not_found cat tag environment webmaster info root assets ] | These names cannot be used as identifiers for Profiles |
finds a profile by its identifier. This method is a shortcut to find_by_identifier.
Examples:
person = Profile['username'] org = Profile.['orgname']
# File app/models/profile.rb, line 244
244: def [](identifier)
245: self.find_by_identifier(identifier)
246: end
# File app/models/profile.rb, line 54
54: def self.extra_data_for_index(sym = nil, &block)
55: self.extra_index_methods.push(sym) if sym
56: self.extra_index_methods.push(block) if block_given?
57: end
# File app/models/profile.rb, line 397
397: def self.recent(limit = nil)
398: self.find(:all, :order => 'id desc', :limit => limit)
399: end
# File app/models/profile.rb, line 424
424: def accept_category?(cat)
425: forbidden = [ ProductCategory, Region ]
426: !forbidden.include?(cat.class)
427: end
adds a person as administrator os this profile
# File app/models/profile.rb, line 385
385: def add_admin(person)
386: self.affiliate(person, Profile::Roles.admin)
387: end
# File app/models/profile.rb, line 121
121: def add_category(c)
122: if self.id
123: ProfileCategorization.add_category_to_profile(c, self)
124: else
125: pending_categorizations << c
126: end
127: end
Adds a person as member of this Profile.
# File app/models/profile.rb, line 368
368: def add_member(person)
369: if self.has_members?
370: if self.closed?
371: AddMember.create!(:person => person, :organization => self)
372: else
373: self.affiliate(person, Profile::Roles.member)
374: end
375: else
376: raise _("%s can't has members") % self.class.name
377: end
378: end
# File app/models/profile.rb, line 389
389: def add_moderator(person)
390: if self.has_members?
391: self.affiliate(person, Profile::Roles.moderator)
392: else
393: raise _("%s can't has moderators") % self.class.name
394: end
395: end
# File app/models/profile.rb, line 284
284: def admin_url
285: generate_url(:controller => 'profile_editor', :action => 'index')
286: end
# File app/models/profile.rb, line 468
468: def boxes_limit
469: LayoutTemplate.find(layout_template).number_of_boxes
470: end
# File app/models/profile.rb, line 129
129: def category_ids=(ids)
130: ProfileCategorization.remove_all_for(self)
131: ids.uniq.each do |item|
132: add_category(Category.find(item))
133: end
134: 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.
# File app/models/profile.rb, line 222
222: def contact_email
223: self.user ? self.user.email : nil
224: end
# File app/models/profile.rb, line 357
357: def copy_article_tree(article, parent=nil)
358: article_copy = article.copy(:profile => self, :parent => parent)
359: if article.profile.home_page == article
360: self.home_page = article_copy
361: end
362: article.children.each do |a|
363: copy_article_tree a, article_copy
364: end
365: end
# File app/models/profile.rb, line 351
351: def copy_articles_from other
352: other.top_level_articles.each do |a|
353: copy_article_tree a
354: end
355: end
# File app/models/profile.rb, line 201
201: def copy_blocks_from(profile)
202: self.boxes.destroy_all
203: profile.boxes.each do |box|
204: self.boxes << Box.new(:position => box.position)
205: box.blocks.each do |block|
206: self.boxes[-1].blocks << block.class.new(:title => block.title, :settings => block.settings, :position => block.position)
207: end
208: end
209: end
creates the initial set of boxes when the profile is created. Can be overriden for each subclass to create a custom set of boxes for its instances.
# File app/models/profile.rb, line 181
181: def create_default_set_of_boxes
182: if template
183: copy_blocks_from template
184: else
185: 3.times do
186: self.boxes << Box.new
187: end
188:
189: if self.respond_to?(:default_set_of_blocks)
190: default_set_of_blocks.each_with_index do |blocks,i|
191: blocks.each do |block|
192: self.boxes[i].blocks << block.new
193: end
194: end
195: end
196: end
197:
198: true
199: end
# File app/models/profile.rb, line 137
137: def create_pending_categorizations
138: pending_categorizations.each do |item|
139: ProfileCategorization.add_category_to_profile(item, self)
140: end
141: pending_categorizations.clear
142: end
# File app/models/profile.rb, line 446
446: def custom_footer
447: self[:custom_footer] || environment.custom_footer
448: end
# File app/models/profile.rb, line 442
442: def custom_header
443: self[:custom_header] || environment.custom_header
444: end
# File app/models/profile.rb, line 429
429: def default_homepage(attrs)
430: TinyMceArticle.new(attrs)
431: end
returns true if the given user can see profile information about this profile, and false otherwise.
# File app/models/profile.rb, line 403
403: def display_info_to?(user)
404: if self.public_profile
405: true
406: else
407: if user.nil?
408: false
409: else
410: # other possibilities would come here
411: (user == self) || (user.memberships.include?(self))
412: end
413: end
414: end
# File app/models/profile.rb, line 50
50: def extra_data_for_index
51: self.class.extra_index_methods.map { |meth| meth.to_proc.call(self) }.flatten
52: end
# File app/models/profile.rb, line 315
315: def find_tagged_with(tag)
316: # FIXME: this can be SLOW
317: articles.select {|item| item.tags.map(&:name).include?(tag) }
318: end
# File app/models/profile.rb, line 462
462: def find_theme(the_id)
463: themes.find { |item| item.id == the_id }
464: end
# File app/models/profile.rb, line 292
292: def generate_url(options)
293: url_options.merge(options)
294: end
Tells whether a specified profile has members or nor.
On this class, returns false by default.
# File app/models/profile.rb, line 323
323: def has_members?
324: false
325: end
Sets the identifier for this profile. Raises an exception when called on a existing profile (since profiles cannot be renamed)
# File app/models/profile.rb, line 153
153: def identifier=(value)
154: unless self.new_record?
155: raise ArgumentError.new(_('An existing profile cannot be renamed.'))
156: end
157: self[:identifier] = value
158: end
# File app/models/profile.rb, line 328
328: def insert_default_article_set
329: if template
330: copy_articles_from template
331: else
332: # a default homepage
333: hp = default_homepage(:name => _("My home page"), :body => _("<p>This is a default homepage created for me. It can be changed though the control panel.</p>"), :advertise => false)
334: hp.profile = self
335: hp.save!
336: self.home_page = hp
337:
338: # a default rss feed
339: feed = RssFeed.new(:name => 'feed')
340: self.articles << feed
341:
342: # a default private folder if public
343: if self.public?
344: folder = Folder.new(:name => _("Intranet"), :public_article => false)
345: self.articles << folder
346: end
347: end
348: self.save!
349: end
returns false.
# File app/models/profile.rb, line 272
272: def is_validation_entity?
273: false
274: end
# File app/models/profile.rb, line 108
108: def location
109: myregion = self.region
110: if myregion
111: myregion.name
112: else
113: ''
114: end
115: end
# File app/models/profile.rb, line 263
263: def organization?
264: self.kind_of?(Organization)
265: end
# File app/models/profile.rb, line 117
117: def pending_categorizations
118: @pending_categorizations ||= []
119: end
returns false
# File app/models/profile.rb, line 255
255: def person?
256: self.kind_of?(Person)
257: end
# File app/models/profile.rb, line 288
288: def public_profile_url
289: generate_url(:controller => 'profile', :action => 'index')
290: end
# File app/models/profile.rb, line 380
380: def remove_member(person)
381: self.disaffiliate(person, Profile::Roles.all_roles)
382: end
# File app/models/profile.rb, line 168
168: def set_default_environment
169: if self.environment.nil?
170: self.environment = Environment.default
171: end
172: true
173: end
# File app/models/profile.rb, line 434
434: def short_name
435: if self[:nickname].blank?
436: truncate self.identifier, 15, '...'
437: else
438: self[:nickname]
439: end
440: end
FIXME this can be SLOW
# File app/models/profile.rb, line 301
301: def tags
302: totals = {}
303: articles.each do |article|
304: article.tags.each do |tag|
305: if totals[tag.name]
306: totals[tag.name] += 1
307: else
308: totals[tag.name] = 1
309: end
310: end
311: end
312: totals
313: end
# File app/models/profile.rb, line 450
450: def theme
451: self[:theme] || environment.theme
452: end
# File app/models/profile.rb, line 144
144: def top_level_articles(reload = false)
145: if reload
146: @top_level_articles = nil
147: end
148: @top_level_articles ||= Article.top_level_for(self)
149: end
# File app/models/profile.rb, line 417
417: def update_category_from_region
418: ProfileCategorization.remove_region(self)
419: if region
420: self.add_category(region)
421: end
422: end
# File app/models/profile.rb, line 276
276: def url
277: if self.domains.empty?
278: generate_url(:controller => 'content_viewer', :action => 'view_page', :page => [])
279: else
280: Noosfero.url_options.merge({ :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []})
281: end
282: end