Module: UserExtensions

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

Overview

User extension methods Custom methods that belongs to a instance user

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)


83
84
85
86
# File 'app/models/concerns/user_extensions.rb', line 83

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_nameString

Returns The name of this user.

Examples:

my_user = current_user
puts my_user.name # can print John Doe
other_user = User.last
puts other_user.name # can print [email protected]

Returns:

  • (String)

    The name of this user.



99
100
101
# File 'app/models/concerns/user_extensions.rb', line 99

def full_name
    self.first_name.blank? ? email : self.first_name + " " + self.last_name.to_s
end

#full_name_initialsString

Returns The name initials of this user.

Examples:

puts current_user.full_name_initials # would print JD

Returns:

  • (String)

    The name initials of this user.



108
109
110
# File 'app/models/concerns/user_extensions.rb', line 108

def full_name_initials
    self.first_name.blank? ? "" : self.first_name[0].upcase + "" + (self.last_name.blank? ? "" : self.last_name[0].upcase)
end

#localeString

 of the platform will be returned

Examples:

locale = User.last.locle
will print something like: :es

Returns:

  • (String)


119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/concerns/user_extensions.rb', line 119

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_settingsvoid

This method returns an undefined value.

Example

user_mfa_settings = User.find(2).mfa_settings
puts user_mfa_settings
    { :mfa_enabled => true, :mfa_method => "email"}


144
145
146
147
148
149
150
151
# File 'app/models/concerns/user_extensions.rb', line 144

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.



59
60
61
# File 'app/models/concerns/user_extensions.rb', line 59

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.



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

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



133
134
135
# File 'app/models/concerns/user_extensions.rb', line 133

def role_names 
    user_roles = self.lesliroles.map(&:name).join(", ")
end

#set_aliasnil

Examples:

puts current_user.full_name # John Doe
puts current_user.set_alias # John D.

Returns:

  • (nil)


45
46
47
48
49
50
# File 'app/models/concerns/user_extensions.rb', line 45

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