Module: Lesli::UserExtensions

Extended by:
ActiveSupport::Concern
Included in:
User
Defined in:
app/models/concerns/lesli/user_extensions.rb

Instance Method Summary collapse

Instance Method Details

#calendar(source_code: :lesli) ⇒ CloudDriver::Calendar

If source_code is provided the method return the specified source calendar.

Returns:

  • (CloudDriver::Calendar)


128
129
130
131
# File 'app/models/concerns/lesli/user_extensions.rb', line 128

def calendar source_code: :lesli
    return Courier::Driver::Calendar.get_user_calendar(self, source_code: source_code, default: true) if source_code == :lesli
    Courier::Driver::Calendar.get_user_calendar(self, source_code: source_code)
end

#full_nameObject

Returns the user’s full name if available, or their email as a fallback.



48
49
50
51
52
53
54
# File 'app/models/concerns/lesli/user_extensions.rb', line 48

def full_name
    if first_name.present?
        [first_name, last_name.presence].compact.join(" ")
    else
        email
    end
end

#full_name_initialsObject

Retrieves and returns the name initials of the user depending on the available information.



58
59
60
61
62
63
64
# File 'app/models/concerns/lesli/user_extensions.rb', line 58

def full_name_initials
    return "" if first_name.blank?

    initials = first_name.strip[0]&.upcase || ""
    initials += last_name.strip[0]&.upcase if last_name.present?
    initials
end

#localeObject

Returns the local configuration for the user, if there is no locale the default local of the platform will be returned



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/concerns/lesli/user_extensions.rb', line 68

def locale
    user_locale = self.settings.find_by(name: "locale")

    # return the desire locale by the user
    return user_locale.value.to_sym if user_locale

    # create a desire locale if the record does not exist 
    self.settings.create_with(:value => I18n.locale).find_or_create_by(:name => "locale")

    # reevaluate
    self.locale()
end

#mfa_settingsObject

Returns MFA settings configured by the user



90
91
92
93
94
95
96
97
# File 'app/models/concerns/lesli/user_extensions.rb', line 90

def mfa_settings
    mfa_enabled = self.settings.create_with(:value => false).find_or_create_by(:name => "mfa_enabled")
    mfa_method = self.settings.create_with(:value => :email).find_or_create_by(:name => "mfa_method")
    {
        :enabled => mfa_enabled.nil? ? false : mfa_enabled.value == 't',
        :method => mfa_method.nil? ? nil : mfa_method.value.to_sym
    }
end

#notification(subject, body: nil, url: nil, category: "info") ⇒ void

This method returns an undefined value.

Parameters:

  • subject

    String Short notification description

  • body (defaults to: nil)

    String Long notification description

  • url (defaults to: nil)

    String Link to notified object

  • category (defaults to: "info")

    String Kind of notification: info, warning, danger, success.



105
106
107
# File 'app/models/concerns/lesli/user_extensions.rb', line 105

def notification subject, body:nil, url:nil, category:"info"
    Courier::Bell::Notification.new(self, subject, body:body, url:url, category:category)
end

#notifications(quantity = 5, category: "info") ⇒ void

This method returns an undefined value.

Parameters:

  • subject

    String Short notification description

  • body

    String Long notification description

  • url

    String Link to notified object

  • category (defaults to: "info")

    String Kind of notification: info, warning, danger, success.



115
116
117
118
119
120
121
122
123
# File 'app/models/concerns/lesli/user_extensions.rb', line 115

def notifications quantity=5, category:"info"
    query = {
        :pagination => {
            :perPage => quantity,
            :page => 1
        }
    }
    Lesli::Courier.new(:lesli_bell, []).from(:notification_service, self, query).call(:index)
end

#role_namesObject

Return a string with the names of all the roles assigned to the user



82
83
84
# File 'app/models/concerns/lesli/user_extensions.rb', line 82

def role_names 
    self.lesliroles.pluck(:name).join(', ')
end

#set_aliasObject

Set the user alias based on the full_name.



40
41
42
43
44
45
# File 'app/models/concerns/lesli/user_extensions.rb', line 40

def set_alias
    if self.alias.blank?
        self.alias = full_name_initials() 
        self.save
    end
end