Module: UsersHelper
- Defined in:
- app/helpers/users_helper.rb
Overview
Provides helper methods for use by views under UsersController
.
Instance Method Summary collapse
-
#avatar_url(user, size = 16) ⇒ String
Get a URL to the avatar for the selected user.
-
#can_change_category(user, target) ⇒ Boolean
Can the specified user change a post’s category to the specified target category?.
-
#default_filter(user_id, category_id) ⇒ Filter?
Get the default filter for the specified user and category.
-
#deleted_user?(user) ⇒ Boolean?
Is the specified user deleted, either globally or on the current community?.
-
#devise_sign_in_enabled? ⇒ Boolean
Is Devise sign in enabled for the current community?.
-
#preference_choice(pref_config) ⇒ Array<Array<String, String>>
Generate <select> options for a user preference with a custom
choice
defined in preferences.yml. -
#rtl_safe_username(user) ⇒ String
Get a RTL-safe string of the specified user’s username.
-
#set_filter_default(user_id, filter_id, category_id, keep) ⇒ ActiveRecord::Relation<CategoryFilterDefault>
Set a default filter for the specified user and category.
-
#sso_sign_in_enabled? ⇒ Boolean
Is SSO sign in enabled for the current community?.
-
#stack_oauth_url ⇒ String
Get an OAuth URL to Stack Exchange.
-
#user_link(user, url_opts = {}, **link_opts) ⇒ ActiveSupport::SafeBuffer
Get a link to the specified user’s profile.
-
#user_preference(name, community: false) ⇒ Object?
Get the current user’s setting for the specified preference.
-
#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.
Instance Method Details
#avatar_url(user, size = 16) ⇒ String
Get a URL to the avatar for the selected user.
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?
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.
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?
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?
135 136 137 |
# File 'app/helpers/users_helper.rb', line 135 def devise_sign_in_enabled? SiteSetting['MixedSignIn'] || !sso_sign_in_enabled? end |
#preference_choice(pref_config) ⇒ Array<Array<String, String>>
Generate <select> options for a user preference with a custom choice
defined in preferences.yml.
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.
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.
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?
128 129 130 |
# File 'app/helpers/users_helper.rb', line 128 def sso_sign_in_enabled? SiteSetting['SsoSignIn'] end |
#stack_oauth_url ⇒ String
Get an OAuth URL to Stack Exchange.
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 |
#user_link(user, url_opts = {}, **link_opts) ⇒ ActiveSupport::SafeBuffer
Get a link to the specified user’s profile.
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.
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.
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 |