diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5fed47d..975cb36 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,11 +1,43 @@ module ApplicationHelper # Custom helper for better display of big numbers # @example number_by_magnitude(4242) # "4.2K" # # @param number[Numeric] # @return [String] human friendly respresentation def number_by_magnitude(number) number_to_human(number, units: { thousand: :K, million: :M, billion: :G }) end + + # Creates a bootstrap form-group div with an additional 'Add' button next to the select field + # + # @param object[ActiveRecord::Object] the form's subject + # @param resource[Symbol] the objects class + # @param attr[Symbol] the select box's attribute + # @param attr_name[String] the attribute's display name + # @param options[Array] the select box options + # @param path[String] the add button's path + def select_with_errors_and_button(object, resource, attr, attr_name, options, path) + has_errors = object.errors[attr].present? + content_tag(:div, class: "form-group #{' has-error' if has_errors }") do + attr_label = label(resource, attr, attr_name, class: 'control-label col-xs-4 required') + select_div = content_tag(:div, class: 'col-xs-6') do + select_part = select_tag([resource, attr].join('_').to_sym, + options, + name: "#{resource}[#{attr}]", + class: 'form-control' + ) + if has_errors + select_part.concat(content_tag(:span, class: 'help-block') { object.errors[attr].first }) + end + select_part + end + + button_part = content_tag(:div, class: 'col-xs-1') do + link_to 'Add', path, class: 'btn btn-primary', role: 'button' + end + + attr_label.concat(select_div).concat(button_part) + end + end end diff --git a/app/views/jobs/_form.html.erb b/app/views/jobs/_form.html.erb index a1ed002..a958d31 100644 --- a/app/views/jobs/_form.html.erb +++ b/app/views/jobs/_form.html.erb @@ -1,53 +1,25 @@ <%= bootstrap_form_for(@job, url: @job.new_record? ? host_jobs_path : host_job_path(@host, @job), layout: :horizontal, label_col: 'col-xs-4', control_col: 'col-xs-6') do |f| %> <%= f.text_field :name %> -
- <%= label :job_template, :fileset_id, 'Fileset', class: 'control-label col-xs-4 required' %> -
- <%= select_tag(:job_template_fileset_id, - options_for_select(@host.filesets.pluck(:name, :id)), - name: 'job_template[fileset_id]', - class: 'form-control' - ) %> - <% if @job.errors[:fileset_id].present? %> - - <%= @job.errors[:fileset_id].first %> - - <% end %> -
- -
- <%= link_to 'Add', new_host_fileset_path(@host), class: 'btn btn-primary', role: 'button' %> -
-
+ <%= select_with_errors_and_button( + @job, :job_template, :fileset_id, 'Fileset', + options_from_collection_for_select(@host.filesets, :id, :name, @job.fileset_id), + new_host_fileset_path(@host)) + %> <% if !@job.restore? %> -
- <%= label :job_template, :schedule_id, 'Schedule Time', class: 'control-label col-xs-4' %> -
- <%= select_tag(:job_template_schedule_id, - options_for_select(@host.schedules.pluck(:name, :id)), - name: 'job_template[schedule_id]', - class: 'form-control' - ) %> - <% if @job.errors[:schedule_id].present? %> - - <%= @job.errors[:schedule_id].first %> - - <% end %> -
- -
- <%= link_to 'Add', new_host_schedule_path(@host), class: 'btn btn-primary', role: 'button' %> -
-
+ <%= select_with_errors_and_button( + @job, :job_template, :schedule_id, + 'Schedule', options_from_collection_for_select(@host.schedules, :id, :name, @job.schedule_id), + new_host_schedule_path(@host)) + %> <% end %> <%= f.text_field :restore_location, label: 'Restore Location', placeholder: '/tmp/bacula' %>
<%= f.submit class: "btn btn-success" %>
<% end %>