Module: UsersHelper

Defined in:
app/helpers/users_helper.rb

Overview

Provides helper methods for use by views under UsersController.

Instance Method Summary collapse

Instance Method Details

#avatar_url(user, size = 16) ⇒ String

Get a URL to the avatar for the selected user.

Parameters:

  • user (User)
  • size (Integer) (defaults to: 16)

    Image side length, in pixels. Does not apply to uploaded avatars - size attributes must still be set in HTML.

Returns:

  • (String)


9
10
11
12
13
14
15
16
17
# File 'app/helpers/users_helper.rb', line 9

def avatar_url(user, size = 16)
  if deleted_user?(user)
    user_auto_avatar_url(letter: 'X', color: '#E73737FF', size: size, format: :png)
  elsif user&.avatar&.attached?
    uploaded_url(user.avatar.blob.key)
  else
    user_auto_avatar_url(user, size: size, format: :png)
  end
end

#can_change_category(user, target) ⇒ Boolean

Can the specified user change a post’s category to the specified target category?

Parameters:

Returns:

  • (Boolean)


32
33
34
35
# File 'app/helpers/users_helper.rb', line 32

def can_change_category(user, target)
  user.privilege?('flag_curate') &&
    (user.is_moderator || user.is_admin || target.min_trust_level.nil? || target.min_trust_level <= user.trust_level)
end

#default_filter(user_id, category_id) ⇒ Filter?

Get the default filter for the specified user and category.

Parameters:

  • user_id (Integer)
  • category_id (Category)

Returns:



55
56
57
# File 'app/helpers/users_helper.rb', line 55

def default_filter(user_id, category_id)
  CategoryFilterDefault.find_by(user_id: user_id, category_id: category_id)&.filter
end

#deleted_user?(user) ⇒ Boolean?

Is the specified user deleted, either globally or on the current community?

Parameters:

Returns:

  • (Boolean, nil)

    True/false, or nil if the user is nil.



93
94
95
96
97
# File 'app/helpers/users_helper.rb', line 93

def deleted_user?(user)
  return nil if user.nil?

  user.deleted? || user.community_user&.deleted?
end

#devise_sign_in_enabled?Boolean

Is Devise sign in enabled for the current community?

Returns:

  • (Boolean)


135
136
137
# File 'app/helpers/users_helper.rb', line 135

def 
  SiteSetting['MixedSignIn'] || !
end

#preference_choice(pref_config) ⇒ Array<Array<String, String>>

Generate <select> options for a user preference with a custom choice defined in preferences.yml.

Returns:

  • (Array<Array<String, String>>)


40
41
42
43
44
45
46
47
48
# File 'app/helpers/users_helper.rb', line 40

def preference_choice(pref_config)
  pref_config['choice'].map do |c|
    if c.is_a? Hash
      [c['name'], c['value']]
    else
      [c.humanize, c]
    end
  end
end

#rtl_safe_username(user) ⇒ String

Get a RTL-safe string of the specified user’s username. Appends an RTL terminator to the username.

Parameters:

Returns:

  • (String)


103
104
105
# File 'app/helpers/users_helper.rb', line 103

def rtl_safe_username(user)
  deleted_user?(user) ? 'deleted user' : user&.rtl_safe_username
end

#set_filter_default(user_id, filter_id, category_id, keep) ⇒ ActiveRecord::Relation<CategoryFilterDefault>

Set a default filter for the specified user and category. Can also be used to remove a default filter.

Parameters:

  • user_id (Integer)
  • filter_id (Integer)
  • category_id (Integer)
  • keep (Boolean)

    Pass true to set a new default; pass false to remove the default for the specified user and category.

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/users_helper.rb', line 67

def set_filter_default(user_id, filter_id, category_id, keep)
  if keep
    CategoryFilterDefault.create_with(filter_id: filter_id)
                         .find_or_create_by(user_id: user_id, category_id: category_id)
                         .update(filter_id: filter_id)
  else
    CategoryFilterDefault.where(user_id: user_id, category_id: category_id)
                         .destroy_all
  end
end

#sso_sign_in_enabled?Boolean

Is SSO sign in enabled for the current community?

Returns:

  • (Boolean)


128
129
130
# File 'app/helpers/users_helper.rb', line 128

def 
  SiteSetting['SsoSignIn']
end

#stack_oauth_urlString

Get an OAuth URL to Stack Exchange.

Returns:

  • (String)


22
23
24
25
# File 'app/helpers/users_helper.rb', line 22

def stack_oauth_url
  "https://stackoverflow.com/oauth?client_id=#{SiteSetting['SEApiClientId']}" \
    "&scope=&redirect_uri=#{stack_redirect_url}"
end

Get a link to the specified user’s profile.

Parameters:

  • user (User)
  • url_opts (Hash) (defaults to: {})

    Options to pass to user_url.

  • link_opts (Hash)

    a customizable set of options

Options Hash (**link_opts):

  • :anchortext (String)

    Custom link text. Remaining link_opts params will be passed to link_to.

Returns:

  • (ActiveSupport::SafeBuffer)


113
114
115
116
117
118
119
120
121
122
123
# File 'app/helpers/users_helper.rb', line 113

def user_link(user, url_opts = {}, **link_opts)
  anchortext = link_opts[:anchortext]
  link_opts_reduced = { dir: 'ltr' }.merge(link_opts).except(:anchortext)
  if deleted_user?(user) || user.nil?
    link_to 'deleted user', '#', link_opts_reduced
  elsif !anchortext.nil?
    link_to anchortext, user_url(user, **url_opts), { dir: 'ltr' }.merge(link_opts)
  else
    link_to user.rtl_safe_username, user_url(user, **url_opts), link_opts_reduced
  end
end

#user_preference(name, community: false) ⇒ Object?

Get the current user’s setting for the specified preference. Returns nil if no user is signed in.

Parameters:

  • name (String)

    The name of the preference to query.

  • community (Boolean) (defaults to: false)

    Is this a community-specific preference?

Returns:

  • (Object, nil)


83
84
85
86
87
# File 'app/helpers/users_helper.rb', line 83

def user_preference(name, community: false)
  return nil if current_user.nil?

  current_user&.preference(name, community: community)
end

#user_with_me(user_id) ⇒ User

Returns a user corresponding to the ID provided, with the caveat that if user_id is ‘me’ and there is a user signed in, the signed in user will be returned. Use for /users/me links.

Parameters:

  • user_id (String)

    The user ID to find, from params

Returns:

  • (User)

    The User object



144
145
146
147
148
149
150
# File 'app/helpers/users_helper.rb', line 144

def user_with_me(user_id)
  if user_id == 'me' && user_signed_in?
    current_user
  else
    User.find(user_id)
  end
end