Module: Naf::ApplicationHelper

Includes:
ActionView::Helpers::TextHelper
Included in:
HistoricalJobsController, MachinesController
Defined in:
app/helpers/naf/application_helper.rb

Constant Summary collapse

NAF_DESTROY_BLOCKED_RESOURCES =
["historical_jobs",
"applications",
"machines",
"historical_job_affinity_tabs",
"janitorial_assignments"]
NAF_READ_ONLY_RESOURCES =
[]
NAF_CREATE_BLOCKED_RESOURCES =
[]
NAF_ALL_VISIBLE_RESOURCES =
{
  'historical_jobs' => '',
  'applications' => '',
  'machines' => '',
  'runners' => ['machine_runners',
                'machine_runner_invocations'],
  'affinities' => '',
  'loggers' => ['logger_styles', 'logger_names'],
  'janitorial_assignments' => ['janitorial_archive_assignments',
                               'janitorial_create_assignments',
                               'janitorial_drop_assignments'],
  'status' => ''
}

Instance Method Summary collapse

Instance Method Details

#add_color(status) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'app/helpers/naf/application_helper.rb', line 237

def add_color(status)
  if status == 'Running'
    'running'
  elsif status == 'Queued' || status == 'Waiting'
    'queued'
  elsif status =~ /Terminat/ || status == 'Error' || status == 'Failed to Start'
    'dead'
  end
end

#http_protocolObject



247
248
249
250
251
252
253
# File 'app/helpers/naf/application_helper.rb', line 247

def http_protocol
  if ['staging', 'production'].include?(Rails.env)
    'https://'
  else
    'http://'
  end
end

#naf_display_job_search_link?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'app/helpers/naf/application_helper.rb', line 181

def naf_display_job_search_link?
  current_page?(naf.root_url) or current_page?(controller: 'historical_jobs', action: 'index')
end


200
201
202
# File 'app/helpers/naf/application_helper.rb', line 200

def naf_generate_back_link
  link_to "Back to #{naf_make_header(controller_name)}", { controller: controller_name, action: 'index' }, class: 'back'
end


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/helpers/naf/application_helper.rb', line 127

def naf_generate_child_resources_link
  case controller_name
    when "historical_jobs"
      link_to "Historical Job Affinity Tabs",
        controller: 'historical_job_affinity_tabs',
        action: 'index',
        historical_job_id: params[:id]
    when "applications"
      if @record.application_schedule
        link_to "Application Schedule Affinity Tabs",
          controller: 'application_schedule_affinity_tabs',
          action: 'index',
          application_schedule_id: @record.application_schedule.id,
          application_id: @record.id
      else
        ""
      end
    when "machines"
      link_to "Machine Affinity Slots",
        controller: 'machine_affinity_slots',
        action: 'index',
        machine_id: params[:id]
    else
      ""
  end
end


175
176
177
178
179
# File 'app/helpers/naf/application_helper.rb', line 175

def naf_generate_create_link
  return "" if NAF_READ_ONLY_RESOURCES.include?(controller_name) or NAF_CREATE_BLOCKED_RESOURCES.include?(controller_name)
  return link_to "Add a Job", naf.new_historical_job_path, { class: 'add_job' } if naf_display_job_search_link?
  link_to "Create new #{naf_model_name}", { controller: controller_name, action: 'new' }
end


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/helpers/naf/application_helper.rb', line 204

def naf_generate_destroy_link
  return "" if NAF_READ_ONLY_RESOURCES.include?(controller_name) or NAF_DESTROY_BLOCKED_RESOURCES.include?(controller_name)
  case controller_name
    when "application_schedule_affinity_tabs"
      link_to "Destroy", application_application_schedule_application_schedule_affinity_tab_url(@application, @application_schedule, @record),
        { confirm: "Are you sure you want to destroy this #{naf_model_name}?",
          method: :delete,
          class: 'destroy' }
    when "machine_affinity_slots"
      link_to "Destroy", machine_machine_affinity_slot_url(@machine, @record),
        { confirm: "Are you sure you want to destroy this #{naf_model_name}?",
          method: :delete,
          class: 'destroy' }
    else
      link_to "Destroy", @record,
        { confirm: "Are you sure you want to destroy this #{naf_model_name}?",
          method: :delete,
          class: 'destroy' }
  end
end


195
196
197
198
# File 'app/helpers/naf/application_helper.rb', line 195

def naf_generate_edit_link
  return "" if NAF_READ_ONLY_RESOURCES.include?(controller_name)
  link_to "Edit", { controller: controller_name, action: 'edit', id: params[:id] }, class: 'edit'
end


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/naf/application_helper.rb', line 154

def naf_generate_index_link(name)
  case name
    when "historical_jobs"
      link_to "Jobs", main_app.naf_path
    when "loggers"
      link_to "Loggers", naf.logger_styles_path
    when "runners"
      link_to "Runners", naf.machine_runners_path
    when "janitorial_assignments"
      link_to "Janitorial Assignments", naf.janitorial_archive_assignments_path
    when "janitorial_archive_assignments"
      link_to "Janitorial Archive Assignments", naf.janitorial_archive_assignments_path
    when "janitorial_create_assignments"
      link_to "Janitorial Create Assignments", naf.janitorial_create_assignments_path
    when "janitorial_drop_assignments"
      link_to "Janitorial Drop Assignments", naf.janitorial_drop_assignments_path
    else
      link_to name.split('_').map(&:capitalize).join(' '), { controller: name, action: 'index'}
  end
end

#naf_highlight_tab?(tab) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/naf/application_helper.rb', line 49

def naf_highlight_tab?(tab)
  case tab
    when "machines"
      [tab, "machine_affinity_slots"].include?(controller_name)
    when "runners"
      ["machine_runners", "machine_runner_invocations"].include?(controller_name)
    when "historical_jobs"
      [tab, "historical_job_affinity_tabs"].include?(controller_name)
    when "applications"
      [tab, "application_schedule_affinity_tabs"].include?(controller_name)
    when "loggers"
      ["logger_styles", "logger_names"].include?(controller_name)
    when "janitorial_assignments"
      ["Naf::JanitorialArchiveAssignment",
       "Naf::JanitorialCreateAssignment",
       "Naf::JanitorialDropAssignment"].include?(params[:type])
    when "janitorial_archive_assignments"
      "Naf::JanitorialArchiveAssignment" == params[:type]
    when "janitorial_create_assignments"
      "Naf::JanitorialCreateAssignment" == params[:type]
    when "janitorial_drop_assignments"
      "Naf::JanitorialDropAssignment" == params[:type]
    else
      tab == controller_name
  end
end


40
41
42
43
44
45
46
47
# File 'app/helpers/naf/application_helper.rb', line 40

def naf_last_queued_at_link(app)
  if historical_job = app.last_queued_job
    link_to "#{time_ago_in_words(historical_job.created_at, true)} ago, #{historical_job.created_at.localtime.strftime("%Y-%m-%d %r")}",
      naf.historical_job_path(historical_job)
  else
    ""
  end
end


229
230
231
232
233
234
235
# File 'app/helpers/naf/application_helper.rb', line 229

def naf_link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object, child_index: "new_#{association}") do |builder|
    render(association.to_s, f: builder)
  end
  link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", id: 'add_prerequisite')
end


225
226
227
# File 'app/helpers/naf/application_helper.rb', line 225

def naf_link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end

#naf_make_header(attribute) ⇒ Object



191
192
193
# File 'app/helpers/naf/application_helper.rb', line 191

def naf_make_header(attribute)
  attribute.to_s.split('_').map(&:capitalize).join(' ')
end

#naf_model_nameObject



185
186
187
188
189
# File 'app/helpers/naf/application_helper.rb', line 185

def naf_model_name
  name_pieces = controller_name.split('_')
  name_pieces[name_pieces.size - 1] = name_pieces.last.singularize
  name_pieces.map(&:capitalize).join(' ')
end

#naf_nested_resource_index?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'app/helpers/naf/application_helper.rb', line 98

def naf_nested_resource_index?
  ["historical_job_affinity_tabs",
   "application_schedule_affinity_tabs",
   "machine_affinity_slots"].include?(controller_name) and !params[:id]
end


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/naf/application_helper.rb', line 76

def naf_parent_resource_link
  case controller_name
    when "historical_job_affinity_tabs"
      link_to "Back to Historical Job",
        controller: 'historical_jobs',
        action: 'show',
        id: params[:historical_job_id]
    when "application_schedule_affinity_tabs"
      link_to "Back to Application",
        controller: 'applications',
        action: 'show',
        id: params[:application_id]
    when "machine_affinity_slots"
      link_to "Back to Machine",
        controller: 'machines',
        action: 'show',
        id: params[:machine_id]
    else
      ""
  end
end

#naf_table_titleObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/naf/application_helper.rb', line 104

def naf_table_title
  if current_page?(naf.janitorial_archive_assignments_path)
    "Janitorial Archive Assignment"
  elsif current_page?(naf.janitorial_create_assignments_path)
    "Janitorial Create Assignment"
  elsif current_page?(naf.janitorial_drop_assignments_path)
    "Janitorial Drop Assignment"
  elsif current_page?(main_app.naf_path)
    "Jobs"
  else
    case controller_name
      when "application_schedule_affinity_tabs"
        Application.find(params[:application_id]).title + ", Affinity Tabs"
      when "machine_affinity_slots"
        machine = Machine.find(params[:machine_id])
        name = machine.server_name
        ((name and name.length > 0) ? name : machine.server_address) + ", Affinity Slots"
      else
        naf_make_header(controller_name)
    end
  end
end

#naf_tabsObject



36
37
38
# File 'app/helpers/naf/application_helper.rb', line 36

def naf_tabs
  NAF_ALL_VISIBLE_RESOURCES
end