diff --git a/app/assets/javascripts/filesets.js b/app/assets/javascripts/filesets.js index d32a362..1cf8042 100644 --- a/app/assets/javascripts/filesets.js +++ b/app/assets/javascripts/filesets.js @@ -1,54 +1,47 @@ $(document).ready(function() { if ($('.include_files-plus-sign').size() > 0) { $(".include_files-plus-sign").click(function() { addIncludedFileTextArea(); }); }; if ($('.exclude_directions-plus-sign').size() > 0) { $(".exclude_directions-plus-sign").click(function() { addExcludeDirectionsTextArea(); }); }; if ($('.include_files-remove-sign').size() > 0) { $(".include_files-remove-sign").click(function() { - removeIncludedFileTextArea(); + removeIncludedFileTextArea($(this)); }); }; if ($('.exclude_directions-remove-sign').size() > 0) { $(".exclude_directions-remove-sign").click(function() { - removeExcludeDirectionsTextArea(); + removeExcludeDirectionsTextArea($(this)); }); }; }); function addIncludedFileTextArea() { - var textArrea = $('.include_files:last').clone(true).val(""); - $('.include_files-plus-sign:last').parent().hide(); - textArrea.insertAfter('.include_files:last'); - $('.include_files:last input').val(""); - $('.include_files-remove-sign:last').show(); + var textArrea = $('.templates .include_files:last').clone(true).val(""); + textArrea.insertBefore('.here-included'); + if ($('.include_files').size() > 1) { + $('.include_files-remove-sign').show(); + }; } function addExcludeDirectionsTextArea() { - var textArrea = $('.exclude_directions:last').clone(true).val(""); - $('.exclude_directions-plus-sign:last').parent().hide(); - textArrea.insertAfter('.exclude_directions:last'); + var textArrea = $('.templates .exclude_directions:last').clone(true).val(""); + textArrea.insertAfter('.here-excluded'); $('.exclude_directions:last input').val(""); - $('.exclude_directions-remove-sign:last').show(); } -function removeIncludedFileTextArea() { - $('.include_files:last').remove(); - $('.include_files-plus-sign:last').parent().show(); - if ($('.include_files').size() > 1) { - $('.include_files-remove-sign:last').parent().show(); +function removeIncludedFileTextArea(element) { + element.closest('div.include_files').remove(); + if ($('.include_files').size() <= 1) { + $('.include_files-remove-sign').hide(); }; } -function removeExcludeDirectionsTextArea() { - $('.exclude_directions:last').remove(); - $('.exclude_directions-plus-sign:last').parent().show(); - if ($('.exclude_directions').size() > 1) { - $('.exclude_directions-remove-sign:last').parent().show(); - }; +function removeExcludeDirectionsTextArea(element) { + element.closest('div.exclude_directions').remove() } diff --git a/app/controllers/filesets_controller.rb b/app/controllers/filesets_controller.rb index 383935e..3cde529 100644 --- a/app/controllers/filesets_controller.rb +++ b/app/controllers/filesets_controller.rb @@ -1,87 +1,94 @@ class FilesetsController < ApplicationController before_action :require_logged_in before_action :fetch_host, only: [:new, :create, :show, :edit, :update] before_action :fetch_job_id, only: [:new, :create, :edit, :update] before_action :fetch_fileset, only: [:show, :edit, :update] # GET /hosts/:host_id/filesets/new def new @fileset = @host.filesets.new @fileset.include_directions = { 'file' => [nil] } + @fileset.exclude_directions = [''] end # GET /hosts/:host_id/filesets/:id def show @fileset = @host.filesets.find(params[:id]) respond_to do |format| format.js {} end end # GET /hosts/:host_id/filesets/:id/edit def edit @fileset.include_files = @fileset.include_directions['file'] + @fileset.exclude_directions ||= [''] end # PATCH /hosts/:host_id/filesets/:id/ def update - if @fileset.update(fetch_params) + fileset_params = fetch_params + if fileset_params[:exclude_directions].nil? + fileset_params[:exclude_directions] = [] + end + + if @fileset.update(fileset_params) flash[:success] = 'Fileset updated successfully' participating_hosts = @fileset.participating_hosts if participating_hosts.size.nonzero? participating_hosts.each(&:recalculate) flash[:alert] = 'You will need to redeploy the afffected clients: ' + participating_hosts.map(&:name).join(', ') end if @job_id redirect_to edit_host_job_path(@host, @job_id, fileset_id: @fileset.id) else redirect_to new_host_job_path(@host, fileset_id: @fileset.id) end else render :edit end end # POST /hosts/:host_id/filesets def create @fileset = @host.filesets.new(fetch_params) if @fileset.save flash[:success] = 'Fileset created' if @job_id.present? redirect_to edit_host_job_path(@host, @job_id, fileset_id: @fileset.id) else redirect_to new_host_job_path(@host, fileset_id: @fileset.id) end else @fileset.include_files = nil - @fileset.exclude_directions = nil + @fileset.exclude_directions = [''] @fileset.include_directions = { 'file' => [nil] } render :new end end # DELETE /hosts/:host_id/filesets/:id def destroy end private def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_job_id @job_id = @host.job_templates.find(params[:job_id]).id if params[:job_id].present? end def fetch_fileset @fileset = @host.filesets.find(params[:id]) end def fetch_params params.require(:fileset).permit(:name, exclude_directions: [], include_files: []) end end diff --git a/app/helpers/filesets_helper.rb b/app/helpers/filesets_helper.rb index 518e9f6..293732e 100644 --- a/app/helpers/filesets_helper.rb +++ b/app/helpers/filesets_helper.rb @@ -1,43 +1,41 @@ module FilesetsHelper - # Creates a bootstrap form-group div with an additional 'Add' button next to the text field + # Creates a bootstrap form-group div with an additional 'Remove' button next to the text 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 placeholder[String] the text box's placeholder - def text_with_errors_and_plus(object, resource, attr, attr_name, placeholder, value=nil) + def text_with_errors_and_remove(object, resource, attr, + attr_name, placeholder, value=nil, no_sign=false) has_errors = object.errors[attr].present? content_tag(:div, class: "form-group #{attr} #{' has_error' if has_errors }") do attr_label = label(resource, attr, attr_name, class: 'control-label col-xs-3') text_div = content_tag(:div, class: 'col-xs-6') do text_part = text_field_tag([resource, attr].join('_').to_sym, value || object.send(attr.to_sym), placeholder: placeholder, name: "#{resource}[#{attr}][]", multiple: true, class: 'form-control' ) {} if has_errors text_part.concat(content_tag(:span, class: 'help-block') { object.errors[attr].first }) end text_part end actions_part = content_tag(:div, class: 'col-xs-1') do - add_link = content_tag(:a, href: '#', class: "#{attr}-plus-sign") do - content_tag(:span, class: 'glyphicon glyphicon-plus') {} - end + attrs = { href: '#', class: "#{attr}-remove-sign" } + attrs[:style] = "display:none" if value.nil? || no_sign - remove_link = content_tag(:a, href: '#', - class: "#{attr}-remove-sign", style: "display:none") do + remove_link = content_tag(:a, attrs) do content_tag(:span, class: 'glyphicon glyphicon-remove text-danger') {} end - add_link.concat(remove_link) end attr_label.concat(text_div).concat(actions_part) end end end diff --git a/app/views/filesets/_form.html.erb b/app/views/filesets/_form.html.erb index 85680b7..78c7f5f 100644 --- a/app/views/filesets/_form.html.erb +++ b/app/views/filesets/_form.html.erb @@ -1,27 +1,59 @@ <%= bootstrap_form_for(@fileset, url: url, method: method, layout: :horizontal, label_col: 'col-xs-3', control_col: 'col-xs-8') do |f| %>
<%= f.text_field :name %> + <% no_remove_sign = @fileset.include_directions['file'].length <= 1 %> + <% @fileset.include_directions['file'].each do |file| %> - <%= text_with_errors_and_plus(@fileset, :fileset, :include_files, 'Files', '/', file) %> + <%= text_with_errors_and_remove( + @fileset, :fileset, :include_files, 'Files', '/', file, no_remove_sign) %> <% end %> + +

+ +

+
+ <%= link_to '#', class: 'include_files-plus-sign' do %> + + <% end %> +
+
+
+ <% @fileset.exclude_directions.each do |file| %> - <%= text_with_errors_and_plus(@fileset, :fileset, :exclude_directions, 'Exclude', '/an_exluded_dir', file) %> + <%= text_with_errors_and_remove(@fileset, :fileset, :exclude_directions, 'Exclude', '/an_exluded_dir', file) %> <% end %> + +

+ +

+
+ <%= link_to '#', class: 'exclude_directions-plus-sign' do %> + + <% end %> +
+
+ <%= (hidden_field_tag :job_id, @job_id) if @job_id%>
+ +
<%= f.submit class: 'btn btn-success' %>
<%= link_to 'Cancel', @job_id.present? ? edit_host_job_path(@host, @job_id) : new_host_job_path(@host), role: 'button', class: 'btn btn-danger'%>
<% end %>