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
-
#attribute_required?(task, parameter_name) ⇒ Boolean
private
Checks if an attribute is required for a given Task.
-
#csv_file_download_path(run) ⇒ Object
private
Returns a download link for a Run’s CSV attachment.
-
#datetime_field_help_text ⇒ Object
private
Return helper text for the datetime-local form field.
-
#format_backtrace(backtrace) ⇒ String
private
Formats a run backtrace.
-
#highlight_code(code) ⇒ ActiveSupport::SafeBuffer
private
Very simple syntax highlighter based on Ripper.
-
#parameter_field(form_builder, parameter_name) ⇒ Object
private
Return the appropriate field tag for the parameter, based on its type.
-
#progress(run) ⇒ String?
private
Renders the progress bar.
-
#resolve_inclusion_value(task, parameter_name) ⇒ Array
private
Resolves values covered by the inclusion validator for a Task attribute.
-
#status_tag(status) ⇒ String
private
Renders a span with a Run’s status, with the corresponding tag class attached.
-
#time_running_in_words(run) ⇒ String
private
Reports the approximate elapsed time a Run has been processed so far based on the Run’s time running attribute.
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.
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_text ⇒ 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 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.
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.
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.
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) = 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_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.
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.[:in] || inclusion_validator.[: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.
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.
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 |