Module: UsersHelper

Defined in:
/var/apps/qpixel/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 '/var/apps/qpixel/app/helpers/users_helper.rb', line 9

def avatar_url(user, size = 16)
  if deleted_user?(user) || user.nil?
    specific_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 '/var/apps/qpixel/app/helpers/users_helper.rb', line 32

def can_change_category(user, target)
  user.privilege?('flag_curate') &&
    (user.at_least_moderator? || 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 a given user and category id.

Parameters:

  • user_id (Integer)

    id of the user to get default filter for

  • category_id (Integer)

    id of the category to get default filter for

Returns:



54
55
56
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 54

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 a gven user deleted (globally or on the current community)?

Parameters:

  • user (User, nil)

    user to check

Returns:

  • (Boolean)

    check result - true if the user is nil.



92
93
94
95
96
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 92

def deleted_user?(user)
  return true 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)


134
135
136
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 134

def 
  SiteSetting['MixedSignIn'] || !
end

#posts_for(user_posts, type) ⇒ Integer

Extracts posts count for a given post type

Parameters:

  • user_posts (Hash{Integer => Integer})

    post counts by post type

  • type (PostType)

    post type to extract count for

Returns:

  • (Integer)

    posts count



155
156
157
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 155

def posts_for(user_posts, type)
  user_posts[type.post_type_id] || 0
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 '/var/apps/qpixel/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:

  • user (User, nil)

    user to get RTL-safe username for

Returns:

  • (String)


102
103
104
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 102

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:



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

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)


127
128
129
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 127

def 
  SiteSetting['SsoSignIn']
end

#stack_oauth_urlString

Get an OAuth URL to Stack Exchange.

Returns:

  • (String)


22
23
24
25
# File '/var/apps/qpixel/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)


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

def user_link(user, url_opts = {}, **link_opts)
  anchortext = link_opts[:anchortext]
  link_opts_reduced = { dir: 'ltr' }.merge(link_opts).except(:anchortext)
  if user.nil? || (deleted_user?(user) && standard?)
    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)


82
83
84
85
86
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 82

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)

    id of the user to find, from params

Returns:

  • (User)

    The User object



143
144
145
146
147
148
149
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 143

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

#votes_for(user_votes, type) ⇒ Integer

Extracts votes count for a given post type

Parameters:

  • user_votes (Hash{Integer => Integer})

    vote counts by post type

  • type (PostType)

    post type to extract count for

Returns:

  • (Integer)

    votes count



163
164
165
# File '/var/apps/qpixel/app/helpers/users_helper.rb', line 163

def votes_for(user_votes, type)
  user_votes[type.post_type_id] || 0
end