Module: RocketJobMissionControl::ApplicationHelper

Defined in:
app/helpers/rocket_job_mission_control/application_helper.rb

Constant Summary collapse

STATE_ICON_MAP =
{
  aborted:   'fas fa-stop',
  completed: 'fas fa-check',
  disabled:  'fas fa-stop',
  enabled:   'fas fa-check',
  failed:    'fas fa-exclamation-triangle',
  paused:    'fas fa-pause',
  pending:   'fas fa-inbox',
  queued:    'fas fa-inbox',
  running:   'fas fa-play',
  sleeping:  'fas fa-hourglass',
  scheduled: 'fas fa-clock',
  starting:  'fas fa-cogs',
  stopping:  'fas fa-stop',
  zombie:    'fas fa-hourglass'
}

Instance Method Summary collapse

Instance Method Details

#active_page(path) ⇒ Object



33
34
35
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 33

def active_page(path)
  'active' if current_page?(path)
end

#editable_field_html(klass, field_name, value, f, include_nil_selectors = false) ⇒ Object

Returns the editable field as html for use in editing dynamic fields from a Job class.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 59

def editable_field_html(klass, field_name, value, f, include_nil_selectors = false)
  # When editing a job the values are of the correct type.
  # When editing a dirmon entry values are strings.
  field = klass.fields[field_name.to_s]
  return unless field && field.type

  placeholder = field.default_val
  placeholder = nil if placeholder.is_a?(Proc)

  case field.type.name
  when 'Symbol', 'String', 'Integer'
    options = extract_inclusion_values(klass, field_name)
    str     = "[#{field.type.name}]\n".html_safe
    if options
      str + f.select(field_name, options, { include_blank: options.include?(nil) || include_nil_selectors, selected: value }, { class: 'selectize form-control' })
    else
      if field.type.name == 'Integer'
        str + f.number_field(field_name, value: value, class: 'form-control', placeholder: placeholder)
      else
        str + f.text_field(field_name, value: value, class: 'form-control', placeholder: placeholder)
      end
    end
  when 'Hash'
    "[JSON Hash]\n".html_safe +
      f.text_field(field_name, value: value ? value.to_json : '', class: 'form-control', placeholder: '{"key1":"value1", "key2":"value2", "key3":"value3"}')
  when 'Array'
    options = Array(value)
    "[Array]\n".html_safe +
      f.select(field_name, options_for_select(options, options), { include_hidden: false }, { class: 'selectize form-control', multiple: true })
  when 'Mongoid::Boolean'
    name = "#{field_name}_true".to_sym
    value = value.to_s
    str  = '<div class="radio-buttons">'.html_safe
    str << f.radio_button(field_name, 'true', checked: value == 'true')
    str << ' '.html_safe + f.label(name, 'true')
    str << ' '.html_safe + f.radio_button(field_name, 'false', checked: value == 'false')
    str << ' '.html_safe + f.label(name, 'false')
    # Allow this field to be unset (nil).
    if include_nil_selectors
      str << ' '.html_safe + f.radio_button(field_name, '', checked: value == '')
      str << ' '.html_safe + f.label(name, 'nil')
    end

    str << '</div>'.html_safe
  else
    "[#{field.type.name}]".html_safe +
      f.text_field(field_name, value: value, class: 'form-control', placeholder: placeholder)
  end
end

#extract_inclusion_values(klass, attribute) ⇒ Object

Returns [Array] list of inclusion values for this attribute. Returns nil when there are no inclusion values for this attribute.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 45

def extract_inclusion_values(klass, attribute)
  values = nil

  klass.validators_on(attribute).each do |validator|
    case validator
    when ActiveModel::Validations::InclusionValidator
      values = validator.options[:in]
    end
  end

  values
end

#pretty_print_array_or_hash(arguments) ⇒ Object



37
38
39
40
41
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 37

def pretty_print_array_or_hash(arguments)
  return arguments unless arguments.kind_of?(Array) || arguments.kind_of?(Hash)
  json_string_options = {space: ' ', indent: '  ', array_nl: '<br />', object_nl: '<br />'}
  JSON.generate(arguments, json_string_options).html_safe
end

#site_titleObject



24
25
26
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 24

def site_title
  'Rocket Job Mission Control'
end

#state_icon(state) ⇒ Object



20
21
22
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 20

def state_icon(state)
  STATE_ICON_MAP[state.to_sym] + ' ' + state.to_s
end

#titleObject



28
29
30
31
# File 'app/helpers/rocket_job_mission_control/application_helper.rb', line 28

def title
  @page_title ||= params[:controller].to_s.titleize
  h(@full_title || [@page_title, site_title].compact.join(' | '))
end