Class: User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- User
- Includes:
- ProfileableMixins::Address, UserAvatar, UserCorporations, UserDateOfBirth, UserMixins::Identification, UserMixins::Memberships, UserProfile
- Defined in:
- app/models/user.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#add_to_corporation ⇒ Object
Returns the value of attribute add_to_corporation.
-
#add_to_group ⇒ Object
Returns the value of attribute add_to_group.
-
#create_account ⇒ Object
Returns the value of attribute create_account.
Class Method Summary collapse
- .alive ⇒ Object
- .applicable_for_new_account ⇒ Object
- .deceased ⇒ Object
- .deceased_ids ⇒ Object
-
.find_all_by_email(email) ⇒ Object
This method finds all users having the given email attribute.
-
.find_all_by_name(name) ⇒ Object
This method finds all users having the given name attribute.
- .find_all_hidden ⇒ Object
- .find_all_non_hidden ⇒ Object
- .find_by_email(email) ⇒ Object
- .find_by_name(name) ⇒ Object
-
.find_by_title(title) ⇒ Object
This method returns the first user matching the given title.
- .hidden ⇒ Object
- .joins_groups ⇒ Object
- .with_email ⇒ Object
- .with_group_flag(flag) ⇒ Object
- .with_group_flags ⇒ Object
- .without_account ⇒ Object
Instance Method Summary collapse
- #accept_terms(terms_stamp) ⇒ Object
- #accepted_terms?(terms_stamp) ⇒ Boolean
-
#activate_account ⇒ Object
This method activates the user account, i.e.
- #address_label ⇒ Object
-
#admin_of ⇒ Object
This method finds all objects the user is an administrator of.
-
#admin_of?(structureable) ⇒ Boolean
This method verifies if the user is administrator of the given structureable object.
-
#admin_of_anything? ⇒ Boolean
Admins ——————————————————————————————.
-
#administrated_objects(role = :admin) ⇒ Object
This method returns all structureable objects the user is administrator of.
-
#alias ⇒ Object
The UserAlias class inherits from String, but has some more methods, e.g.
- #alive? ⇒ Boolean
-
#bookmarked_objects ⇒ Object
This method lists all bookmarked objets of this user.
-
#capitalize_name ⇒ Object
This method will make the first_name and the last_name capitalized.
-
#corporate_vita_memberships_in(corporation) ⇒ Object
Corporate Vita ==========================================================================================.
-
#corporations ⇒ Object
This returns all corporations of the user.
-
#current_corporations ⇒ Object
This returns the corporations the user is currently member of.
- #current_status_group_in(corporation) ⇒ Object
- #current_status_membership_in(corporation) ⇒ Object
-
#date_of_death ⇒ Object
Date of Death The date of death is localized already! Why?.
-
#deactivate_account ⇒ Object
This method deactivates the user account, i.e.
- #dead? ⇒ Boolean
- #developer ⇒ Object
- #developer=(mark_as_developer) ⇒ Object
-
#developer? ⇒ Boolean
This method returns whether the user is a developer.
-
#directly_administrated_objects(role = :admin) ⇒ Object
This method returns all structureable objects the user is directly administrator of, i.e.
- #end_all_non_corporation_memberships(options = {}) ⇒ Object
-
#find_or_build_last_seen_activity ⇒ Object
Activities ——————————————————————————————.
-
#first_corporation ⇒ Object
This returns the first corporation where the user is still member of or nil.
-
#former_member_of_corporation?(corporation) ⇒ Boolean
Former Member ==========================================================================================.
-
#gender ⇒ Object
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.
- #gender=(new_gender) ⇒ Object
- #generate_alias ⇒ Object
- #generate_alias! ⇒ Object
-
#global_admin ⇒ Object
Global Admin Switch ==========================================================================================.
- #global_admin=(new_setting) ⇒ Object
- #global_admin? ⇒ Boolean
-
#group_flags ⇒ Object
This efficiently returns all flags of the groups the user is currently in.
-
#guest_of?(group) ⇒ Boolean
This method says if the user (self) is a guest of the given group.
-
#has_account? ⇒ Boolean
A user stored in the database does not necessarily possess the right to log in, i.e.
- #has_no_account? ⇒ Boolean
- #hidden ⇒ Object
- #hidden=(hidden) ⇒ Object
-
#hidden? ⇒ Boolean
Hidden ==========================================================================================.
-
#inspect ⇒ Object
The string returned by this method represents the user in the rails console.
-
#join(event_or_group) ⇒ Object
This makes the user join an event or a grop.
- #last_group_in_first_corporation ⇒ Object
- #leave(event_or_group) ⇒ Object
-
#main_admin_of?(structureable) ⇒ Boolean
This method says whether the user (self) is a main admin of the given structureable object.
- #male? ⇒ Boolean
-
#mark_as_deceased(options = {}) ⇒ Object
Example:.
-
#markable_as_deceased? ⇒ Boolean
Defines whether the user can be marked as deceased (by a workflow).
-
#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.
-
#my_groups_in_first_corporation ⇒ Object
This returns the groups within the first corporation where the user is still member of in the order of entering the group.
-
#name ⇒ Object
The name of the user, i.e.
- #name_affix ⇒ Object
-
#news_pages ⇒ Object
List news (Pages) that concern the user.
- #postal_address_with_name_surrounding ⇒ Object
-
#relationships ⇒ Object
This returns all relationship opjects.
-
#role_for(structureable) ⇒ Object
This method returns the role the user (self) has for a given structureable object.
- #set_date_of_death_if_unset(new_date_of_death) ⇒ Object
-
#sorted_current_corporations ⇒ Object
This returns the same as ‘current_corporations`, but sorted by the date of joining the corporations, earliest joining first.
- #status_group_in_primary_corporation ⇒ Object
- #status_group_memberships ⇒ Object
-
#status_groups(options = {}) ⇒ Object
This returns all status groups of the user, i.e.
-
#title ⇒ Object
This method returns a kind of label for the user, e.g.
-
#to_param ⇒ Object
This sets the format of the User urls to be.
-
#upcoming_events ⇒ Object
This method lists all upcoming events of the groups the user is member of.
- #update_last_seen_activity(description = nil, object = nil) ⇒ Object
-
#workflows ⇒ Object
This method returns all workflows applicable for this user, i.e.
- #workflows_by_corporation ⇒ Object
- #workflows_for(group) ⇒ Object
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
Instance Attribute Details
#add_to_corporation ⇒ Object
Returns the value of attribute add_to_corporation.
8 9 10 |
# File 'app/models/user.rb', line 8 def add_to_corporation @add_to_corporation end |
#add_to_group ⇒ Object
Returns the value of attribute add_to_group.
8 9 10 |
# File 'app/models/user.rb', line 8 def add_to_group @add_to_group end |
#create_account ⇒ Object
Returns the value of attribute create_account.
8 9 10 |
# File 'app/models/user.rb', line 8 def create_account @create_account end |
Class Method Details
.alive ⇒ Object
832 833 834 835 836 837 838 |
# File 'app/models/user.rb', line 832 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_account ⇒ Object
848 849 850 |
# File 'app/models/user.rb', line 848 def self.applicable_for_new_account self.alive.without_account.with_email end |
.deceased ⇒ Object
824 825 826 |
# File 'app/models/user.rb', line 824 def self.deceased self.joins(:profile_fields).where(:profile_fields => {label: 'date_of_death'}) end |
.deceased_ids ⇒ Object
828 829 830 |
# File 'app/models/user.rb', line 828 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
800 801 802 803 804 805 806 |
# File 'app/models/user.rb', line 800 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
793 794 795 |
# File 'app/models/user.rb', line 793 def self.find_all_by_name( name ) # TODO: Test this self.where("CONCAT(first_name, ' ', last_name) = ?", name) end |
.find_all_hidden ⇒ Object
724 725 726 |
# File 'app/models/user.rb', line 724 def self.find_all_hidden self.where(id: Group.hidden_users.member_ids) end |
.find_all_non_hidden ⇒ Object
728 729 730 731 |
# File 'app/models/user.rb', line 728 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
808 809 810 |
# File 'app/models/user.rb', line 808 def self.find_by_email( email ) self.find_all_by_email(email).first end |
.find_by_name(name) ⇒ Object
786 787 788 |
# File 'app/models/user.rb', line 786 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.
780 781 782 783 784 |
# File 'app/models/user.rb', line 780 def self.find_by_title( title ) self.where("? LIKE CONCAT('%', first_name, ' ', last_name, '%')", title).select do |user| user.title == title end.first end |
.hidden ⇒ Object
820 821 822 |
# File 'app/models/user.rb', line 820 def self.hidden self.with_group_flag('hidden_users') end |
.joins_groups ⇒ Object
852 853 854 |
# File 'app/models/user.rb', line 852 def self.joins_groups self.joins(:groups).where('dag_links.valid_to IS NULL') end |
.with_email ⇒ Object
844 845 846 |
# File 'app/models/user.rb', line 844 def self.with_email self.joins(:profile_fields).where('profile_fields.type = ? AND profile_fields.value != ?', 'ProfileFieldTypes::Email', '') end |
.with_group_flag(flag) ⇒ Object
816 817 818 |
# File 'app/models/user.rb', line 816 def self.with_group_flag(flag) self.with_group_flags.where("flags.key = ?", flag) end |
.with_group_flags ⇒ Object
812 813 814 |
# File 'app/models/user.rb', line 812 def self.with_group_flags self.joins(:groups => :flags) end |
.without_account ⇒ Object
840 841 842 |
# File 'app/models/user.rb', line 840 def self.without_account self.includes(:account).where(:user_accounts => { :user_id => nil }) end |
Instance Method Details
#accept_terms(terms_stamp) ⇒ Object
856 857 858 859 860 |
# File 'app/models/user.rb', line 856 def accept_terms(terms_stamp) self.accepted_terms = terms_stamp self.accepted_terms_at = Time.zone.now save! end |
#accepted_terms?(terms_stamp) ⇒ Boolean
861 862 863 |
# File 'app/models/user.rb', line 861 def accepted_terms?(terms_stamp) self.accepted_terms == terms_stamp end |
#activate_account ⇒ Object
This method activates the user account, i.e. grants the user the right to log in.
238 239 240 241 242 243 244 |
# File 'app/models/user.rb', line 238 def activate_account unless self.account self.account = self.build_account self.save end return self.account end |
#address_label ⇒ Object
185 186 187 188 189 190 |
# File 'app/models/user.rb', line 185 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_of ⇒ Object
This method finds all objects the user is an administrator of.
623 624 625 |
# File 'app/models/user.rb', line 623 def admin_of self.administrated_objects end |
#admin_of?(structureable) ⇒ Boolean
This method verifies if the user is administrator of the given structureable object.
629 630 631 |
# File 'app/models/user.rb', line 629 def admin_of?( structureable ) self.admin_of.include? structureable end |
#admin_of_anything? ⇒ Boolean
Admins
617 618 619 |
# File 'app/models/user.rb', line 617 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.
651 652 653 654 655 656 657 658 659 660 661 |
# File 'app/models/user.rb', line 651 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 |
#alias ⇒ Object
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.
203 204 205 |
# File 'app/models/user.rb', line 203 def alias UserAlias.new(super) if super.present? end |
#alive? ⇒ Boolean
150 151 152 |
# File 'app/models/user.rb', line 150 def alive? not dead? end |
#bookmarked_objects ⇒ Object
This method lists all bookmarked objets of this user.
569 570 571 |
# File 'app/models/user.rb', line 569 def bookmarked_objects self.bookmarks.collect { |bookmark| bookmark.bookmarkable } end |
#capitalize_name ⇒ Object
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"
71 72 73 74 75 |
# File 'app/models/user.rb', line 71 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
416 417 418 419 420 421 |
# File 'app/models/user.rb', line 416 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 |
#corporations ⇒ Object
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.
345 346 347 348 349 350 351 |
# File 'app/models/user.rb', line 345 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_corporations ⇒ Object
This returns the corporations the user is currently member of.
355 356 357 358 359 360 361 |
# File 'app/models/user.rb', line 355 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
449 450 451 |
# File 'app/models/user.rb', line 449 def current_status_group_in(corporation) StatusGroup.find_by_user_and_corporation(self, corporation) if corporation end |
#current_status_membership_in(corporation) ⇒ Object
443 444 445 446 447 |
# File 'app/models/user.rb', line 443 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_death ⇒ Object
Date of Death The date of death is localized already! Why?
137 138 139 |
# File 'app/models/user.rb', line 137 def date_of_death cached { profile_fields.where(label: 'date_of_death').first.try(:value) } end |
#deactivate_account ⇒ Object
This method deactivates the user account, i.e. destroys the associated object and prevents the user from logging in.
249 250 251 252 253 |
# File 'app/models/user.rb', line 249 def deactivate_account raise "no user account exists, therefore it can't be destroyed." if not self.account self.account.destroy self.account = nil end |
#dead? ⇒ Boolean
147 148 149 |
# File 'app/models/user.rb', line 147 def dead? date_of_death ? true : false end |
#developer ⇒ Object
693 694 695 |
# File 'app/models/user.rb', line 693 def developer self.member_of? Group.developers end |
#developer=(mark_as_developer) ⇒ Object
696 697 698 699 700 701 702 |
# File 'app/models/user.rb', line 696 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.
690 691 692 |
# File 'app/models/user.rb', line 690 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.
636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'app/models/user.rb', line 636 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
174 175 176 177 178 179 |
# File 'app/models/user.rb', line 174 def end_all_non_corporation_memberships( = {}) date = [: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_activity ⇒ Object
Activities
284 285 286 |
# File 'app/models/user.rb', line 284 def find_or_build_last_seen_activity last_seen_activities.last || last_seen_activities.build end |
#first_corporation ⇒ Object
This returns the first corporation where the user is still member of or nil
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'app/models/user.rb', line 376 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
736 737 738 |
# File 'app/models/user.rb', line 736 def former_member_of_corporation?( corporation ) corporation.becomes(Corporation).former_members.include? self end |
#gender ⇒ Object
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.)
117 118 119 120 |
# File 'app/models/user.rb', line 117 def gender return :female if female? return :male end |
#gender=(new_gender) ⇒ Object
121 122 123 124 125 126 127 |
# File 'app/models/user.rb', line 121 def gender=( new_gender ) if new_gender.to_s == "female" self.female = true else self.female = false end end |
#generate_alias ⇒ Object
207 208 209 |
# File 'app/models/user.rb', line 207 def generate_alias UserAlias.generate_for(self) end |
#generate_alias! ⇒ Object
211 212 213 |
# File 'app/models/user.rb', line 211 def generate_alias! self.alias = self.generate_alias end |
#global_admin ⇒ Object
Global Admin Switch
758 759 760 |
# File 'app/models/user.rb', line 758 def global_admin self.in? Group.everyone.admins end |
#global_admin=(new_setting) ⇒ Object
764 765 766 767 768 769 770 771 |
# File 'app/models/user.rb', line 764 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
761 762 763 |
# File 'app/models/user.rb', line 761 def global_admin? self.global_admin end |
#group_flags ⇒ Object
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'
750 751 752 |
# File 'app/models/user.rb', line 750 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.
679 680 681 682 |
# File 'app/models/user.rb', line 679 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.
228 229 230 231 |
# File 'app/models/user.rb', line 228 def has_account? return true if self.account return false end |
#has_no_account? ⇒ Boolean
232 233 234 |
# File 'app/models/user.rb', line 232 def has_no_account? not self.account.present? end |
#hidden ⇒ Object
715 716 717 |
# File 'app/models/user.rb', line 715 def hidden cached { self.member_of? Group.hidden_users } end |
#hidden=(hidden) ⇒ Object
719 720 721 722 |
# File 'app/models/user.rb', line 719 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.
711 712 713 |
# File 'app/models/user.rb', line 711 def hidden? self.hidden end |
#inspect ⇒ Object
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, ...
873 874 875 |
# File 'app/models/user.rb', line 873 def inspect "User: #{self.id} #{self.alias}" end |
#join(event_or_group) ⇒ Object
This makes the user join an event or a grop.
514 515 516 517 518 519 520 |
# File 'app/models/user.rb', line 514 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_corporation ⇒ Object
408 409 410 |
# File 'app/models/user.rb', line 408 def last_group_in_first_corporation my_groups_in_first_corporation.last end |
#leave(event_or_group) ⇒ Object
521 522 523 524 525 526 527 528 529 530 |
# File 'app/models/user.rb', line 521 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.
669 670 671 |
# File 'app/models/user.rb', line 669 def main_admin_of?( structureable ) self.administrated_objects( :main_admin ).include? structureable end |
#male? ⇒ Boolean
128 129 130 |
# File 'app/models/user.rb', line 128 def male? not female? end |
#mark_as_deceased(options = {}) ⇒ Object
Example:
user.mark_as_deceased at: "2014-03-05".to_datetime
158 159 160 161 162 163 164 165 166 |
# File 'app/models/user.rb', line 158 def mark_as_deceased( = {}) date = [: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) account.try(:destroy) end |
#markable_as_deceased? ⇒ Boolean
Defines whether the user can be marked as deceased (by a workflow).
170 171 172 |
# File 'app/models/user.rb', line 170 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.
602 603 604 605 606 607 608 609 610 611 612 |
# File 'app/models/user.rb', line 602 def member_of?( object, = {} ) if object.kind_of? Group if [:with_invalid] or [: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_corporation ⇒ Object
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.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'app/models/user.rb', line 392 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{ |membership| membership.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 |
#name ⇒ Object
The name of the user, i.e. first_name and last_name.
59 60 61 |
# File 'app/models/user.rb', line 59 def name first_name + " " + last_name if first_name && last_name end |
#name_affix ⇒ Object
92 93 94 |
# File 'app/models/user.rb', line 92 def name_affix title.gsub(name, '').strip end |
#news_pages ⇒ Object
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
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'app/models/user.rb', line 547 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_surrounding ⇒ Object
181 182 183 |
# File 'app/models/user.rb', line 181 def postal_address_with_name_surrounding address_label.to_s end |
#relationships ⇒ Object
This returns all relationship opjects.
467 468 469 |
# File 'app/models/user.rb', line 467 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.
582 583 584 585 586 587 |
# File 'app/models/user.rb', line 582 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
141 142 143 144 145 146 |
# File 'app/models/user.rb', line 141 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_corporations ⇒ Object
This returns the same as ‘current_corporations`, but sorted by the date of joining the corporations, earliest joining first.
366 367 368 369 370 371 372 |
# File 'app/models/user.rb', line 366 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_corporation ⇒ Object
453 454 455 456 457 458 459 |
# File 'app/models/user.rb', line 453 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_memberships ⇒ Object
437 438 439 440 441 |
# File 'app/models/user.rb', line 437 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
433 434 435 |
# File 'app/models/user.rb', line 433 def status_groups( = {}) StatusGroup.find_all_by_user(self, ) end |
#title ⇒ Object
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.
88 89 90 |
# File 'app/models/user.rb', line 88 def title name end |
#to_param ⇒ Object
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.
108 109 110 |
# File 'app/models/user.rb', line 108 def to_param "#{id} #{title}".parameterize end |
#upcoming_events ⇒ Object
This method lists all upcoming events of the groups the user is member of.
508 509 510 |
# File 'app/models/user.rb', line 508 def upcoming_events Event.upcoming.find_all_by_groups( self.groups ).direct end |
#update_last_seen_activity(description = nil, object = nil) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'app/models/user.rb', line 288 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 # 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 |
#workflows ⇒ Object
This method returns all workflows applicable for this user, i.e. this returns all workflows of all groups the user is a member of.
478 479 480 481 482 483 484 |
# File 'app/models/user.rb', line 478 def workflows my_workflows = [] self.groups.each do |group| my_workflows += group.child_workflows end return my_workflows end |
#workflows_by_corporation ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 |
# File 'app/models/user.rb', line 491 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
486 487 488 489 |
# File 'app/models/user.rb', line 486 def workflows_for(group) (([group.becomes(Group)] + group.descendant_groups) & self.groups) .collect { |g| g.child_workflows }.select { |w| not w.nil? }.flatten end |