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

#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

#log(engine: nil, source: nil, action: nil, operation: nil, description: nil, session_id: nil, subject: nil) ⇒ Object

Register a audit log for the current user



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
# File 'app/models/concerns/lesli/user_extensions.rb', line 87

def log(
    engine:nil, # must be MyEngine
    source:nil, # must be self.class
    action:nil, # must be action_name
    operation:nil,   # two word action description
    description:nil, # human readable description
    session_id:nil,  # must come from server session
    subject:nil # resource related to the log
    )

    return unless defined?(LesliAudit)

    self.logs.create!({
        engine: engine,
        source: source,
        action: action,

        operation: operation,
        description: description,
        session_id: session_id,

        subject_type: subject&.class&.name,
        subject_id: subject&.id,

        account: self&.&.audit
    })
end

#nameObject



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

def name
    if first_name.present?
        first_name
    else
        email
    end
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.roles.pluck(:name).join(', ')
end