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

#attribute_required?(task, parameter_name) ⇒ Boolean

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.

Checks if an attribute is required for a given Task.

Parameters:

Returns:

  • (Boolean)

    Whether the attribute is required.



197
198
199
200
201
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 197

def attribute_required?(task, parameter_name)
  task.class.validators_on(parameter_name).any? do |validator|
    validator.kind == :presence
  end
end

#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



100
101
102
103
104
105
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 100

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.



179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 179

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.



87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 87

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 153

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

  case form_builder.object.class.attribute_types[parameter_name]
  when ActiveModel::Type::Integer
    form_builder.number_field(parameter_name, class: "input")
  when ActiveModel::Type::Decimal, ActiveModel::Type::Float
    form_builder.number_field(parameter_name, { step: "any", class: "input" })
  when ActiveModel::Type::DateTime
    form_builder.datetime_field(parameter_name, class: "input") + datetime_field_help_text
  when ActiveModel::Type::Date
    form_builder.date_field(parameter_name, class: "input")
  when ActiveModel::Type::Time
    form_builder.time_field(parameter_name, class: "input")
  when ActiveModel::Type::Boolean
    form_builder.check_box(parameter_name, class: "checkbox")
  else
    form_builder.text_area(parameter_name, class: "textarea")
  end
    .then { |input| tag.div(input, class: "control") }
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", "mt-4"] + 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 (Task)

    The Task for which the value needs to be resolved.

  • parameter_name (String)

    The parameter name.

Returns:

  • (Array)

    value of the resolved inclusion option.



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
147
148
149
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 122

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
65
66
67
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 62

def status_tag(status)
  tag.span(
    status.capitalize,
    class: ["tag", "has-text-weight-medium", "pr-2", "mr-4"] + 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.



75
76
77
# File 'app/helpers/maintenance_tasks/tasks_helper.rb', line 75

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