Module: DeprecationHelper

Defined in:
app/helpers/deprecation_helper.rb

Overview

Groups together tools for deprecating areas of the interface

Defined Under Namespace

Classes: WarningLevel

Constant Summary collapse

LEVELS =

Contains the stylings which will be applied to the deprecation warning candidate: We haven't set a date for deprecation yet scheduled: A date has been set, but its more than a week away. imminent: Deprecation is less than a week away

{
  candidate: WarningLevel.new('info', 'Request for feedback', 'info-circle').freeze,
  scheduled: WarningLevel.new('warning', 'Scheduled for removal in %s days', 'exclamation-circle').freeze,
  imminent: WarningLevel.new('danger', 'Scheduled for removal in %s days', 'exclamation-triangle').freeze
}.freeze
IMMINENT_THRESHOLD =

The threshold at which to switch to imminent styling

7

Instance Method Summary collapse

Instance Method Details

#_deprecation_level(date) ⇒ Object


67
68
69
70
71
72
73
# File 'app/helpers/deprecation_helper.rb', line 67

def _deprecation_level(date)
  if date.nil? then LEVELS[:candidate]
  elsif (date - Date.current) < IMMINENT_THRESHOLD then LEVELS[:imminent]
  else
    LEVELS[:scheduled]
  end
end

#deprecate_section(date: nil, message: '', replaced_by: nil, custom_title: nil, custom_style: nil, &block) ⇒ String

Renders a card surrounding the enclosed block which informs the user of upcoming deprecation. The user will be presented with a button to let us know they still need the feature, and a link to the alternative if provided. The warning will become increasingly prominent as the date approaches.

Once date has been reached, the section will be automatically hidden. It will also log everytime it gets hit to allow for removal.

Examples:

sample_registration.html.erb

<%= deprecate_section(
   date: Date.parse('20190708'),
   message: 'Sample registration has been replaced by sample manifests. These provide added features',
   replaced_by: sample_manifest_path) do %>
   <p>Old we content</p>
<% end %>

Parameters:

  • date: (defaults to: nil)

    nil [nil,Date] The date at which the section will be hidden.

  • message: (String) (defaults to: '')

    Body explaining why the feature is being removed, and where the functionality can be found elsewhere.

  • replaced_by: (String, nil) (defaults to: nil)

    URL of the replacement (if applicable).

  • custom_title: (String) (defaults to: nil)

    Override the title determined by the level.

  • custom_style: (String) (defaults to: nil)

    Overide the styles determined by the level.

  • &block (lambda)

    The contents of the card (ie. the feture to be deprecated)

Returns:

  • (String)

    The HTML to render


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/deprecation_helper.rb', line 44

def deprecate_section(date: nil, message: '', replaced_by: nil, custom_title: nil, custom_style: nil, &block)
  # If we're past the date just hide the section
  if date && Date.current > date
    Rails.logger.warn "Deprecated section past deadline: #{Kernel.caller.first}"
    return ''
  end

  level = _deprecation_level(date)
  remaining = date && (date - Date.current).to_i
  title = (custom_title || level.title) % remaining
  style = custom_style || level.style

  (:div, class: ['card', "border-#{style}", 'mb-3']) do
    concat((:div, class: ['card-body', "bg-#{style}", 'text-white']) do
      concat icon('fas', level.icon, title)
      concat (:p, message)
      concat link_to 'See the alternative', replaced_by, class: %w[btn btn-block btn-outline-light] if replaced_by
      concat mail_to configatron.admin_email, icon('far', 'envelope ', 'Let us know if you still need this'), class: %w[btn btn-block btn-outline-light]
    end)
    concat (:div, class: 'card-body', &block)
  end
end