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)


54
55
56
57
# File 'app/models/concerns/user_extensions.rb', line 54

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.



76
77
78
# File 'app/models/concerns/user_extensions.rb', line 76

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.



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

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)


109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/concerns/user_extensions.rb', line 109

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"}


129
130
131
132
133
134
135
136
# File 'app/models/concerns/user_extensions.rb', line 129

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

#name2String

Returns The first name of this user.

Returns:

  • (String)

    The first name of this user.



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

def name2
    self.first_name
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.



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

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

#set_aliasnil

Examples:

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

Returns:

  • (nil)


95
96
97
98
99
100
# File 'app/models/concerns/user_extensions.rb', line 95

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