Class: User

Inherits:
ActiveRecord::Base show all
Includes:
ProfileableMixins::Address, UserAvatar, UserCorporations, UserDateOfBirth, UserMixins::Identification, UserMixins::Memberships, UserProfile
Defined in:
app/models/user.rb

Direct Known Subclasses

ListExportUser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserDateOfBirth

#age, #birthday_this_year, #build_date_of_birth_profile_field, #date_of_birth, #date_of_birth=, #date_of_birth_profile_field, #find_or_build_date_of_birth_profile_field, #find_or_create_date_of_birth_profile_field, #localized_date_of_birth, #localized_date_of_birth=

Methods included from UserProfile

#academic_degree, #fill_in_template_profile_information, #landline_profile_fields, #mobile, #mobile=, #mobile_phone_profile_fields, #name_surrounding_profile_field, #personal_title, #phone, #phone=, #phone_profile_fields, #profile_field_value, #text_above_name, #text_after_name, #text_before_name, #text_below_name

Methods included from UserCorporations

#corporation, #corporation_id, #corporation_name, #corporation_name=

Methods inherited from ActiveRecord::Base

#readonly?

Instance Attribute Details

#add_to_corporationObject

Returns the value of attribute add_to_corporation.



10
11
12
# File 'app/models/user.rb', line 10

def add_to_corporation
  @add_to_corporation
end

#add_to_groupObject

Returns the value of attribute add_to_group.



10
11
12
# File 'app/models/user.rb', line 10

def add_to_group
  @add_to_group
end

#create_accountObject

Returns the value of attribute create_account.



10
11
12
# File 'app/models/user.rb', line 10

def 
  @create_account
end

Class Method Details

.aliveObject



834
835
836
837
838
839
840
# File 'app/models/user.rb', line 834

def self.alive
  if self.deceased_ids.count > 0
    self.where('NOT users.id IN (?)', self.deceased_ids)
  else
    self.where(true)
  end
end

.applicable_for_new_accountObject



850
851
852
# File 'app/models/user.rb', line 850

def self.
  self.alive..with_email
end

.deceasedObject



826
827
828
# File 'app/models/user.rb', line 826

def self.deceased
  self.joins(:profile_fields).where(:profile_fields => {label: 'date_of_death'})
end

.deceased_idsObject



830
831
832
# File 'app/models/user.rb', line 830

def self.deceased_ids
  self.deceased.pluck(:id)
end

.find_all_by_email(email) ⇒ Object

This method finds all users having the given email attribute. notice: case insensitive



802
803
804
805
806
807
808
# File 'app/models/user.rb', line 802

def self.find_all_by_email( email ) # TODO: Test this; # TODO: optimize using where
  email_fields = ProfileField.where( type: "ProfileFieldTypes::Email", value: email )
  matching_users = email_fields
    .select{ |ef| ef.profileable_type == "User" }
    .collect { |ef| ef.profileable }
  return matching_users.to_a
end

.find_all_by_name(name) ⇒ Object

This method finds all users having the given name attribute. notice: case insensitive



795
796
797
# File 'app/models/user.rb', line 795

def self.find_all_by_name( name ) # TODO: Test this
  self.where("CONCAT(first_name, ' ', last_name) = ?", name)
end

.find_all_hiddenObject



726
727
728
# File 'app/models/user.rb', line 726

def self.find_all_hidden
  self.where(id: Group.hidden_users.member_ids)
end

.find_all_non_hiddenObject



730
731
732
733
# File 'app/models/user.rb', line 730

def self.find_all_non_hidden
  non_hidden_user_ids = User.pluck(:id) - Group.hidden_users.member_ids
  self.where(id: non_hidden_user_ids)  # in order to make it work with cancan.
end

.find_by_email(email) ⇒ Object



810
811
812
# File 'app/models/user.rb', line 810

def self.find_by_email( email )
  self.find_all_by_email(email).first
end

.find_by_name(name) ⇒ Object



788
789
790
# File 'app/models/user.rb', line 788

def self.find_by_name( name )
  self.find_all_by_name(name).limit(1).first
end

.find_by_title(title) ⇒ Object

This method returns the first user matching the given title.



782
783
784
785
786
# File 'app/models/user.rb', line 782

def self.find_by_title( title )
  self.where("? LIKE CONCAT('%', first_name, ' ', last_name, '%')", title).select do |user|
    user.title == title
  end.first
end

.hiddenObject



822
823
824
# File 'app/models/user.rb', line 822

def self.hidden
  self.with_group_flag('hidden_users')
end

.joins_groupsObject



854
855
856
# File 'app/models/user.rb', line 854

def self.joins_groups
  self.joins(:groups).where('dag_links.valid_to IS NULL')
end

.with_emailObject



846
847
848
# File 'app/models/user.rb', line 846

def self.with_email
  self.joins(:profile_fields).where('profile_fields.type = ? AND profile_fields.value != ?', 'ProfileFieldTypes::Email', '')
end

.with_group_flag(flag) ⇒ Object



818
819
820
# File 'app/models/user.rb', line 818

def self.with_group_flag(flag)
  self.with_group_flags.where("flags.key = ?", flag)
end

.with_group_flagsObject



814
815
816
# File 'app/models/user.rb', line 814

def self.with_group_flags
  self.joins(:groups => :flags)
end

.without_accountObject



842
843
844
# File 'app/models/user.rb', line 842

def self.
  self.includes(:account).where(:user_accounts => { :user_id => nil })
end

Instance Method Details

#accept_terms(terms_stamp) ⇒ Object



858
859
860
861
862
# File 'app/models/user.rb', line 858

def accept_terms(terms_stamp)
  self.accepted_terms = terms_stamp
  self.accepted_terms_at = Time.zone.now
  save!
end

#accepted_terms?(terms_stamp) ⇒ Boolean

Returns:

  • (Boolean)


863
864
865
# File 'app/models/user.rb', line 863

def accepted_terms?(terms_stamp)
  self.accepted_terms == terms_stamp
end

#activate_accountObject

This method activates the user account, i.e. grants the user the right to log in.



240
241
242
243
244
245
246
# File 'app/models/user.rb', line 240

def 
  unless self.
    self. = self.
    self.save
  end
  return self.
end

#address_labelObject



187
188
189
190
191
192
# File 'app/models/user.rb', line 187

def address_label
  cached do
    AddressLabel.new(self.name, self.postal_address_field_or_first_address_field, 
      self.name_surrounding_profile_field, self.personal_title, self.corporation_name)
  end
end

#admin_ofObject

This method finds all objects the user is an administrator of.



625
626
627
# File 'app/models/user.rb', line 625

def admin_of
  self.administrated_objects
end

#admin_of?(structureable) ⇒ Boolean

This method verifies if the user is administrator of the given structureable object.

Returns:

  • (Boolean)


631
632
633
# File 'app/models/user.rb', line 631

def admin_of?( structureable )
  self.admin_of.include? structureable
end

#admin_of_anything?Boolean

Admins


Returns:

  • (Boolean)


619
620
621
# File 'app/models/user.rb', line 619

def admin_of_anything?
  groups.find_all_by_flag(:admins_parent).count > 0
end

#administrated_objects(role = :admin) ⇒ Object

This method returns all structureable objects the user is administrator of.



653
654
655
656
657
658
659
660
661
662
663
# File 'app/models/user.rb', line 653

def administrated_objects( role = :admin )
  objects = directly_administrated_objects( role )
  if objects
    objects += objects.collect do |directly_administrated_object|
      directly_administrated_object.descendants
    end.flatten
    objects
  else
    []
  end
end

#aliasObject

The UserAlias class inherits from String, but has some more methods, e.g. a method to generate a new alias from other user attributes. To make sure that ‘alias` returns an object of UserAlias type, the accessor methods are overridden here.



205
206
207
# File 'app/models/user.rb', line 205

def alias
  UserAlias.new(super) if super.present?
end

#alive?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'app/models/user.rb', line 152

def alive?
  not dead?
end

#bookmarked_objectsObject

This method lists all bookmarked objets of this user.



571
572
573
# File 'app/models/user.rb', line 571

def bookmarked_objects
  self.bookmarks.collect { |bookmark| bookmark.bookmarkable }
end

#capitalize_nameObject

This method will make the first_name and the last_name capitalized. For example:

@user = User.create( first_name: "john", last_name: "doe", ... )
@user.capitalize_name  # => "John Doe"
@user.save
@user.name  # => "John Doe"


73
74
75
76
77
# File 'app/models/user.rb', line 73

def capitalize_name
  self.first_name = capitalized_name_string( self.first_name )
  self.last_name = capitalized_name_string( self.last_name )
  self.name
end

#corporate_vita_memberships_in(corporation) ⇒ Object

Corporate Vita



418
419
420
421
422
423
# File 'app/models/user.rb', line 418

def corporate_vita_memberships_in(corporation)
  Rails.cache.fetch([self, 'corporate_vita_memberships_in', corporation], expires_in: 1.week) do
    group_ids = corporation.status_groups.map(&:id) & self.parent_groups.map(&:id)
    UserGroupMembership.now_and_in_the_past.find_all_by_user(self).where(ancestor_id: group_ids, ancestor_type: 'Group')
  end
end

#corporationsObject

This returns all corporations of the user. The Corporation model inherits from the Group model. corporations are child_groups of the corporations_parent_group in the DAG.

everyone
   |----- corporations_parent
   |                |---------- corporation_a      <----
   |                |                |--- ...           |--- These are corporations
   |                |---------- corporation_b      <----
   |                                 |--- ...
   |----- other_group_1
   |----- other_group_2

Warning! This method does not distinguish between regular members and guest members. If a user is only guest in a corporation, ‘user.corporations` WILL list this corporation.



347
348
349
350
351
352
353
# File 'app/models/user.rb', line 347

def corporations
  cached do
    my_corporation_ids = (self.group_ids & Group.corporations.pluck(:id) ) if Group.corporations_parent
    my_corporation_ids ||= []
    Corporation.find my_corporation_ids
  end
end

#current_corporationsObject

This returns the corporations the user is currently member of.



357
358
359
360
361
362
363
# File 'app/models/user.rb', line 357

def current_corporations
  cached do
    self.corporations.select do |corporation|
      Role.of(self).in(corporation).current_member?
    end || []
  end
end

#current_status_group_in(corporation) ⇒ Object



451
452
453
# File 'app/models/user.rb', line 451

def current_status_group_in(corporation)
  StatusGroup.find_by_user_and_corporation(self, corporation) if corporation
end

#current_status_membership_in(corporation) ⇒ Object



445
446
447
448
449
# File 'app/models/user.rb', line 445

def current_status_membership_in( corporation )
  if status_group = current_status_group_in(corporation)
    StatusGroupMembership.find_by_user_and_group(self, status_group)
  end
end

#date_of_deathObject

Date of Death The date of death is localized already! Why?



139
140
141
# File 'app/models/user.rb', line 139

def date_of_death
  cached { profile_fields.where(label: 'date_of_death').first.try(:value) }
end

#deactivate_accountObject

This method deactivates the user account, i.e. destroys the associated object and prevents the user from logging in.



251
252
253
254
255
# File 'app/models/user.rb', line 251

def 
  raise "no user account exists, therefore it can't be destroyed." if not self.
  self..destroy
  self. = nil
end

#dead?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/models/user.rb', line 149

def dead?
  date_of_death ? true : false
end

#developerObject



695
696
697
# File 'app/models/user.rb', line 695

def developer
  self.member_of? Group.developers
end

#developer=(mark_as_developer) ⇒ Object



698
699
700
701
702
703
704
# File 'app/models/user.rb', line 698

def developer=( mark_as_developer )
  if mark_as_developer
    Group.developers.assign_user self
  else
    Group.developers.unassign_user self
  end
end

#developer?Boolean

This method returns whether the user is a developer. This is needed, for example, to determine if some features are presented to the current_user.

Returns:

  • (Boolean)


692
693
694
# File 'app/models/user.rb', line 692

def developer?
  self.developer
end

#directly_administrated_objects(role = :admin) ⇒ Object

This method returns all structureable objects the user is directly administrator of, i.e. the user is a member of the administrators group of this object.



638
639
640
641
642
643
644
645
646
647
648
649
# File 'app/models/user.rb', line 638

def directly_administrated_objects( role = :admin )
  admin_group_flag = :admins_parent if role == :admin
  admin_group_flag = :main_admins_parent if role == :main_admin
  admin_groups = self.ancestor_groups.find_all_by_flag( admin_group_flag )
  if admin_groups.count > 0
    objects = admin_groups.collect do |admin_group|
      admin_group.administrated_object
    end
  else
    []
  end
end

#end_all_non_corporation_memberships(options = {}) ⇒ Object



176
177
178
179
180
181
# File 'app/models/user.rb', line 176

def end_all_non_corporation_memberships(options = {})
  date = options[:at] || Time.zone.now
  for group in (self.direct_groups - Group.corporations_parent.descendant_groups)
    UserGroupMembership.find_by_user_and_group(self, group).invalidate at: date
  end
end

#find_or_build_last_seen_activityObject

Activities




286
287
288
# File 'app/models/user.rb', line 286

def find_or_build_last_seen_activity
  last_seen_activities.last || last_seen_activities.build
end

#first_corporationObject

This returns the first corporation where the user is still member of or nil



378
379
380
381
382
383
384
385
386
387
388
389
# File 'app/models/user.rb', line 378

def first_corporation
  # if self.corporations
  #   self.corporations.select do |corporation|
  #     not ( self.guest_of?( corporation )) and
  #     not ( self.former_member_of_corporation?( corporation ))
  #   end.sort_by do |corporation|
  #     corporation.membership_of( self ).valid_from or Time.zone.now
  #   end.first
  # end
  
  sorted_current_corporations.first
end

#former_member_of_corporation?(corporation) ⇒ Boolean

Former Member

Returns:

  • (Boolean)


738
739
740
# File 'app/models/user.rb', line 738

def former_member_of_corporation?( corporation )
  corporation.becomes(Corporation).former_members.include? self
end

#genderObject

This accessors allow to access the gender of the user rather than just asking if the user is female as allowed by the ActiveRecord accessor. (:female is a boolean column in the users table.)



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

def gender
  return :female if female?
  return :male
end

#gender=(new_gender) ⇒ Object



123
124
125
126
127
128
129
# File 'app/models/user.rb', line 123

def gender=( new_gender )
  if new_gender.to_s == "female"
    self.female = true
  else
    self.female = false
  end
end

#generate_aliasObject



209
210
211
# File 'app/models/user.rb', line 209

def generate_alias
  UserAlias.generate_for(self)
end

#generate_alias!Object



213
214
215
# File 'app/models/user.rb', line 213

def generate_alias!
  self.alias = self.generate_alias
end

#global_adminObject

Global Admin Switch



760
761
762
# File 'app/models/user.rb', line 760

def global_admin
  self.in? Group.everyone.admins
end

#global_admin=(new_setting) ⇒ Object



766
767
768
769
770
771
772
773
# File 'app/models/user.rb', line 766

def global_admin=(new_setting)
  if new_setting == true
    Group.everyone.admins << self
  else
    UserGroupMembership.find_by_user_and_group(self, Group.everyone.main_admins_parent).try(:destroy)
    UserGroupMembership.find_by_user_and_group(self, Group.everyone.admins_parent).try(:destroy)
  end
end

#global_admin?Boolean

Returns:

  • (Boolean)


763
764
765
# File 'app/models/user.rb', line 763

def global_admin?
  self.global_admin
end

#group_flagsObject

This efficiently returns all flags of the groups the user is currently in.

For example, ony can find out with one sql query whether a user is hidden:

user.group_flags.include? 'hidden_users'


752
753
754
# File 'app/models/user.rb', line 752

def group_flags
  groups.joins(:flags).pluck('flags.key')
end

#guest_of?(group) ⇒ Boolean

This method says if the user (self) is a guest of the given group.

Returns:

  • (Boolean)


681
682
683
684
# File 'app/models/user.rb', line 681

def guest_of?( group )
  return false if not group.find_guests_parent_group
  group.guests.include? self
end

#has_account?Boolean

A user stored in the database does not necessarily possess the right to log in, i.e. have a user account. This method allows to find out whether the user has got an active user account.

Returns:

  • (Boolean)


230
231
232
233
# File 'app/models/user.rb', line 230

def has_account?
  return true if self.
  return false
end

#has_no_account?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'app/models/user.rb', line 234

def has_no_account?
  not self..present?
end

#hiddenObject



717
718
719
# File 'app/models/user.rb', line 717

def hidden
  cached { self.member_of? Group.hidden_users }
end

#hidden=(hidden) ⇒ Object



721
722
723
724
# File 'app/models/user.rb', line 721

def hidden=(hidden)
  Group.hidden_users.assign_user self if hidden == true || hidden == "true"
  Group.hidden_users.unassign_user self if hidden == false || hidden == "false"
end

#hidden?Boolean

Hidden

Some users are hidden for regular users. They can only be seen by their administrators. This is necessary for some organizations due to privacy reasons.

Returns:

  • (Boolean)


713
714
715
# File 'app/models/user.rb', line 713

def hidden?
  self.hidden
end

#inspectObject

The string returned by this method represents the user in the rails console. For example, if you type ‘User.all` in the console, the answer would be:

User: alias_of_the_first_user, User: alias_of_the_second_user, ...


875
876
877
# File 'app/models/user.rb', line 875

def inspect
  "User: #{self.id} #{self.alias}"
end

#join(event_or_group) ⇒ Object

This makes the user join an event or a grop.



516
517
518
519
520
521
522
# File 'app/models/user.rb', line 516

def join(event_or_group)
  if event_or_group.kind_of? Group
    event_or_group.assign_user self
  elsif event_or_group.kind_of? Event
    event_or_group.attendees_group.assign_user self
  end
end

#last_group_in_first_corporationObject



410
411
412
# File 'app/models/user.rb', line 410

def last_group_in_first_corporation
  my_groups_in_first_corporation.last
end

#leave(event_or_group) ⇒ Object



523
524
525
526
527
528
529
530
531
532
# File 'app/models/user.rb', line 523

def leave(event_or_group)
  if event_or_group.kind_of? Group
    # TODO: Change to `unassign` when he can have multiple dag links between two nodes.
    # event_or_group.members.destroy(self)  
    raise 'We need multiple dag links between two nodes!'
  elsif event_or_group.kind_of? Event
    # TODO: Change to `unassign` when he can have multiple dag links between two nodes.
    event_or_group.attendees_group.members.destroy(self)  
  end
end

#main_admin_of?(structureable) ⇒ Boolean

This method says whether the user (self) is a main admin of the given structureable object.

Returns:

  • (Boolean)


671
672
673
# File 'app/models/user.rb', line 671

def main_admin_of?( structureable )
  self.administrated_objects( :main_admin ).include? structureable
end

#male?Boolean

Returns:

  • (Boolean)


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

def male?
  not female?
end

#mark_as_deceased(options = {}) ⇒ Object

Example:

user.mark_as_deceased at: "2014-03-05".to_datetime


160
161
162
163
164
165
166
167
168
# File 'app/models/user.rb', line 160

def mark_as_deceased(options = {})
  date = options[:at] || Time.zone.now
  self.current_corporations.each do |corporation|
    self.current_status_membership_in(corporation).move_to corporation.deceased, at: date
  end
  end_all_non_corporation_memberships at: date
  set_date_of_death_if_unset(date)
  .try(:destroy)
end

#markable_as_deceased?Boolean

Defines whether the user can be marked as deceased (by a workflow).

Returns:

  • (Boolean)


172
173
174
# File 'app/models/user.rb', line 172

def markable_as_deceased?
  alive?
end

#member_of?(object, options = {}) ⇒ Boolean

This method is a dirty hack to preserve the obsolete role model mechanism, which is currently not in use, since the abilities are defined directly in the Ability class.

Options:

with_invalid, also_in_the_past : true/false

TODO: refactor it together with the role model mechanism.

Returns:

  • (Boolean)


604
605
606
607
608
609
610
611
612
613
614
# File 'app/models/user.rb', line 604

def member_of?( object, options = {} )
  if object.kind_of? Group
    if options[:with_invalid] or options[:also_in_the_past]
      self.ancestor_group_ids.include? object.id
    else  # only current memberships:
      self.group_ids.include? object.id  # This uses the validity range mechanism
    end
  else
    self.ancestors.include? object
  end
end

#my_groups_in_first_corporationObject

This returns the groups within the first corporation where the user is still member of in the order of entering the group. The groups must not be special and the user most not be a special member.



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'app/models/user.rb', line 394

def my_groups_in_first_corporation
  cached do
    if first_corporation
      my_memberships = UserGroupMembership.find_all_by_user( self )
      my_memberships = my_memberships.now.reorder(:valid_from)
      my_groups = my_memberships.collect { |membership| membership.try( :group ) } if my_memberships
      my_groups ||= []
      my_groups.select do |group|
        first_corporation.in?( group.ancestor_groups )
      end.reject { |group| group.is_special_group? or self.guest_of?( group ) }
    else
      []
    end
  end
end

#nameObject

The name of the user, i.e. first_name and last_name.



61
62
63
# File 'app/models/user.rb', line 61

def name
  first_name + " " + last_name if first_name && last_name
end

#name_affixObject



94
95
96
# File 'app/models/user.rb', line 94

def name_affix
  title.gsub(name, '').strip
end

#news_pagesObject

List news (Pages) that concern the user.

everyone ---- page_1 ---- page_2      <--- show
    |
    |----- group_1 ---- page_3        <--- DO NOT show
    |
    |----- group_2 ---- user
    |        |-- page_4               <--- show
    |
    |--- user


549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'app/models/user.rb', line 549

def news_pages
  # List all pages that do not have ancestor groups
  # which the user is no member of.
  #
  
  # THIS WORKS BUT LOOKS UGLY. TODO: Refactor this:
  group_ids_the_user_is_no_member_of = 
    Group.pluck(:id) - self.group_ids
  pages_that_belong_to_groups_the_user_is_no_member_of = Page
    .includes(:ancestor_groups)
    .where(groups: {id: group_ids_the_user_is_no_member_of})
  Page
    .where('NOT id IN (?)', (pages_that_belong_to_groups_the_user_is_no_member_of + [0])) # +[0]-hack: otherwise the list is empty when all pages should be shown, i.e. for fresh systems.
    .order('pages.updated_at DESC')
end

#postal_address_with_name_surroundingObject



183
184
185
# File 'app/models/user.rb', line 183

def postal_address_with_name_surrounding
  address_label.to_s
end

#relationshipsObject

This returns all relationship opjects.



469
470
471
# File 'app/models/user.rb', line 469

def relationships
  relationships_as_first_user + relationships_as_second_user
end

#role_for(structureable) ⇒ Object

This method returns the role the user (self) has for a given structureable object.

The roles may be :member, :admin or :main_admin.



584
585
586
587
588
589
# File 'app/models/user.rb', line 584

def role_for( structureable )
  return nil if not structureable.respond_to? :parent_groups
  return :main_admin if self.main_admin_of? structureable
  return :admin if self.admin_of? structureable
  return :member if self.member_of? structureable
end

#set_date_of_death_if_unset(new_date_of_death) ⇒ Object



143
144
145
146
147
148
# File 'app/models/user.rb', line 143

def set_date_of_death_if_unset(new_date_of_death)
  new_date_of_death = I18n.localize(new_date_of_death.to_date)
  unless self.date_of_death
    profile_fields.create(type: "ProfileFieldTypes::General", label: 'date_of_death', value: new_date_of_death)
  end
end

#sorted_current_corporationsObject

This returns the same as ‘current_corporations`, but sorted by the date of joining the corporations, earliest joining first.



368
369
370
371
372
373
374
# File 'app/models/user.rb', line 368

def sorted_current_corporations
  cached do
    current_corporations.sort_by do |corporation|
      corporation.membership_of(self).valid_from || Time.zone.now - 100.years
    end
  end
end

#status_group_in_primary_corporationObject



455
456
457
458
459
460
461
# File 'app/models/user.rb', line 455

def status_group_in_primary_corporation
  # - First try the `first_corporation`,  which does not consider corporations the user is
  #   a former member of.
  # - Next, use all corporations, which applies to completely excluded members.
  #
  cached { current_status_group_in(first_corporation || corporations.first) }
end

#status_group_membershipsObject



439
440
441
442
443
# File 'app/models/user.rb', line 439

def status_group_memberships
  self.status_groups.collect do |group|
    StatusGroupMembership.find_by_user_and_group( self, group )
  end
end

#status_groups(options = {}) ⇒ Object

This returns all status groups of the user, i.e. groups that represent the member status of the user in a corporation.

options:

:with_invalid  =>  true, false


435
436
437
# File 'app/models/user.rb', line 435

def status_groups(options = {})
  StatusGroup.find_all_by_user(self, options)
end

#titleObject

This method returns a kind of label for the user, e.g. for menu items representing the user. Use this rather than the name attribute itself, since the title method is likely to be overridden in the main application. Notice: This method does not return the academic title of the user.



90
91
92
# File 'app/models/user.rb', line 90

def title
  name
end

#to_paramObject

This sets the format of the User urls to be

example.com/users/24-john-doe

rather than just

example.com/users/24

This method uses a cache on purpose, since it is directly used by rails to construct the url.



110
111
112
# File 'app/models/user.rb', line 110

def to_param
  "#{id} #{title}".parameterize
end

#upcoming_eventsObject

This method lists all upcoming events of the groups the user is member of.



510
511
512
# File 'app/models/user.rb', line 510

def upcoming_events
  Event.upcoming.find_all_by_groups( self.groups ).direct
end

#update_last_seen_activity(description = nil, object = nil) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'app/models/user.rb', line 290

def update_last_seen_activity(description = nil, object = nil)
  unless readonly?
    if description and not self.incognito?
      activity = find_or_build_last_seen_activity
      activity.touch unless activity.new_record? # even if the attributes didn't change. The user probably hit 'reload' then.
      activity.description = description
      activity.link_to_object = object
      activity.save
    else
      last_seen_activities.destroy_all
    end
  end
end

#workflowsObject

This method returns all workflows applicable for this user, i.e. this returns all workflows of all groups the user is a member of.



480
481
482
483
484
485
486
# File 'app/models/user.rb', line 480

def workflows
  my_workflows = []
  self.groups.each do |group|
    my_workflows += group.child_workflows
  end
  return my_workflows
end

#workflows_by_corporationObject



493
494
495
496
497
498
499
500
501
502
503
# File 'app/models/user.rb', line 493

def workflows_by_corporation
  hash = {}
  other_workflows = self.workflows
  self.corporations.each do |corporation|
    corporation_workflows = self.workflows_for(corporation)
    hash.merge!( corporation.title.to_s => corporation_workflows )
    other_workflows -= corporation_workflows
  end
  hash.merge!( I18n.t(:others).to_s => other_workflows ) if other_workflows.count > 0
  return hash
end

#workflows_for(group) ⇒ Object



488
489
490
491
# File 'app/models/user.rb', line 488

def workflows_for(group)
  (([group.becomes(Group)] + group.descendant_groups) & self.groups)
    .collect { |g| g.child_workflows }.select { |w| not w.nil? }.flatten
end