Module: AbilitiesHelper

Included in:
Ability
Defined in:
/var/apps/qpixel/app/helpers/abilities_helper.rb

Instance Method Summary collapse

Instance Method Details

#ability_err_msg(internal_id, action = nil) ⇒ String

Provides an error message for when a user is unable to complete an ability-restricted action, either because the user doesn’t have the ability or because it has been suspended.

Parameters:

  • internal_id (String)

    The internal_id attribute of the ability in question.

Returns:

  • (String)

    An error message appropriate to the circumstances.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File '/var/apps/qpixel/app/helpers/abilities_helper.rb', line 26

def ability_err_msg(internal_id, action = nil)
  ability = Ability.find_by internal_id: internal_id
  ua = current_user&.privilege(ability.internal_id)
  if ua&.suspended?
    if action.nil?
      "Your use of the #{ability.name} ability has been temporarily suspended. " \
        "See /abilities/#{ability.internal_id} for more information."
    else
      "Your use of the #{ability.name} ability has been temporarily suspended, so you cannot #{action}." \
        "See /abilities/#{ability.internal_id} for more information."
    end
  else
    if action.nil?
      "You need the #{ability.name} ability to do this." \
        "See /abilities/#{ability.internal_id} for more information."
    else
      "You need the #{ability.name} ability to #{action}." \
        "See /abilities/#{ability.internal_id} for more information."
    end
  end
end

Gets a maybe_ability to the specified ability

Parameters:

  • maybe_ability (Ability, String)

    ability or its internal id to link to

Returns:

  • (ActiveSupport::SafeBuffer)


5
6
7
8
9
10
# File '/var/apps/qpixel/app/helpers/abilities_helper.rb', line 5

def ability_link(maybe_ability)
  ability = maybe_ability.is_a?(String) ? Ability.find_by(internal_id: maybe_ability) : maybe_ability

  link_to ability.name,
          ability_url(ability.internal_id, host: ability.community.host)
end

#linearize_progress(score) ⇒ Float

Linearizes the Wilson-score progress used by ability calculations. For example, 0.98 and 0.99 are not far away on a linear scale, but mean a change of about 2x for the actual limit used by the algorithm. This method takes that into account and provides an indicator of progress on a linear scale, for use in progress bars.

Parameters:

  • score (Float)

    The Wilson score result to linearize.

Returns:

  • (Float)

    The linearized score.



17
18
19
20
# File '/var/apps/qpixel/app/helpers/abilities_helper.rb', line 17

def linearize_progress(score)
  linear_score = ((4 * score) - 2) / (1 - score)
  [0, linear_score].max.to_f
end