Module: MaintenanceTasks::TasksHelper Private

Defined in:
app/helpers/maintenance_tasks/tasks_helper.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helpers for formatting data in the maintenance_tasks views.

Constant Summary collapse

STATUS_COLOURS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "new" => ["is-primary"],
  "enqueued" => ["is-primary is-light"],
  "running" => ["is-info"],
  "interrupted" => ["is-info", "is-light"],
  "pausing" => ["is-warning", "is-light"],
  "paused" => ["is-warning"],
  "succeeded" => ["is-success"],
  "cancelling" => ["is-light"],
  "cancelled" => ["is-dark"],
  "errored" => ["is-danger"],
}

Instance Method Summary collapse

Instance Method Details

#csv_file_download_path(run) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a download link for a Run’s CSV attachment



97
98
99
100
101
102
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 97

def csv_file_download_path(run)
  Rails.application.routes.url_helpers.rails_blob_path(
    run.csv_file,
    only_path: true,
  )
end

#datetime_field_help_textObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return helper text for the datetime-local form field.



173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 173

def datetime_field_help_text
  text =
    if Time.zone_default.nil? || Time.zone_default.name == "UTC"
      "Timezone: UTC."
    else
      "Timezone: #{Time.now.zone}."
    end
  tag.div(
    tag.p(text),
    class: "content is-small",
  )
end

#format_backtrace(backtrace) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats a run backtrace.

Parameters:

  • backtrace (Array<String>)

    the backtrace associated with an exception on a Task that ran and raised.

Returns:

  • (String)

    the parsed, HTML formatted version of the backtrace.



28
29
30
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 28

def format_backtrace(backtrace)
  safe_join(backtrace.to_a, tag.br)
end

#highlight_code(code) ⇒ ActiveSupport::SafeBuffer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Very simple syntax highlighter based on Ripper.

It returns the same code except identifiers, keywords, etc. are wrapped in <span> tags with CSS classes that match the types returned by Ripper.lex.

Parameters:

  • code (String)

    the Ruby code source to syntax highlight.

Returns:

  • (ActiveSupport::SafeBuffer)

    HTML of the code.



84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 84

def highlight_code(code)
  tokens = Ripper.lex(code).map do |(_position, type, content, _state)|
    case type
    when :on_nl, :on_sp, :on_ignored_nl
      content
    else
      tag.span(content, class: type.to_s.sub("on_", "ruby-").dasherize)
    end
  end
  safe_join(tokens)
end

#parameter_field(form_builder, parameter_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the appropriate field tag for the parameter, based on its type. If the parameter has a ‘validates_inclusion_of` validator, return a dropdown list of options instead.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 150

def parameter_field(form_builder, parameter_name)
  inclusion_values = resolve_inclusion_value(form_builder.object, parameter_name)
  return form_builder.select(parameter_name, inclusion_values, prompt: "Select a value") if inclusion_values

  case form_builder.object.class.attribute_types[parameter_name]
  when ActiveModel::Type::Integer
    form_builder.number_field(parameter_name)
  when ActiveModel::Type::Decimal, ActiveModel::Type::Float
    form_builder.number_field(parameter_name, { step: "any" })
  when ActiveModel::Type::DateTime
    form_builder.datetime_field(parameter_name) + datetime_field_help_text
  when ActiveModel::Type::Date
    form_builder.date_field(parameter_name)
  when ActiveModel::Type::Time
    form_builder.time_field(parameter_name)
  when ActiveModel::Type::Boolean
    form_builder.check_box(parameter_name)
  else
    form_builder.text_area(parameter_name, class: "textarea")
  end
end

#progress(run) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Renders the progress bar.

The style of the progress tag depends on the Run status. It also renders an infinite progress when a Run is active but there is no total information to estimate completion.

Parameters:

  • run (Run)

    the Run which the progress bar will be based on.

Returns:

  • (String)

    the progress information properly formatted.

  • (nil)

    if the run has not started yet.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 42

def progress(run)
  return unless run.started?

  progress = Progress.new(run)

  progress_bar = tag.progress(
    value: progress.value,
    max: progress.max,
    class: ["progress"] + STATUS_COLOURS.fetch(run.status),
  )
  progress_text = tag.p(tag.i(progress.text))
  tag.div(progress_bar + progress_text, class: "block")
end

#resolve_inclusion_value(task, parameter_name) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves values covered by the inclusion validator for a task attribute. Supported option types:

  • Arrays

  • Procs and lambdas that optionally accept the Task instance, and return an Array.

  • Callable objects that receive one argument, the Task instance, and return an Array.

  • Methods that return an Array, called on the Task instance.

Other types are not supported and will return nil.

Returned values are used to populate a dropdown list of options.

Parameters:

  • task_class (Class<Task>)

    The task class for which the value needs to be resolved.

  • parameter_name (String)

    The parameter name.

Returns:

  • (Array)

    value of the resolved inclusion option.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 119

def resolve_inclusion_value(task, parameter_name)
  task_class = task.class
  inclusion_validator = task_class.validators_on(parameter_name).find do |validator|
    validator.kind == :inclusion
  end
  return unless inclusion_validator

  in_option = inclusion_validator.options[:in] || inclusion_validator.options[:within]
  resolved_in_option = case in_option
  when Proc
    if in_option.arity == 0
      in_option.call
    else
      in_option.call(task)
    end
  when Symbol
    method = task.method(in_option)
    method.call if method.arity.zero?
  else
    if in_option.respond_to?(:call)
      in_option.call(task)
    else
      in_option
    end
  end

  resolved_in_option if resolved_in_option.is_a?(Array)
end

#status_tag(status) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Renders a span with a Run’s status, with the corresponding tag class attached.

Parameters:

  • status (String)

    the status for the Run.

Returns:

  • (String)

    the span element containing the status, with the appropriate tag class attached.



62
63
64
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 62

def status_tag(status)
  tag.span(status.capitalize, class: ["tag"] + STATUS_COLOURS.fetch(status))
end

#time_running_in_words(run) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reports the approximate elapsed time a Run has been processed so far based on the Run’s time running attribute.

Parameters:

  • run (Run)

    the source of the time to be reported.

Returns:

  • (String)

    the description of the time running attribute.



72
73
74
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 72

def time_running_in_words(run)
  distance_of_time_in_words(0, run.time_running, include_seconds: true)
end