diff --git a/app/assets/javascripts/error_templates.js b/app/assets/javascripts/error_templates.js new file mode 100644 index 00000000..1b9b5234 --- /dev/null +++ b/app/assets/javascripts/error_templates.js @@ -0,0 +1,18 @@ +$(function() { + if ($.isController('error_templates')) { + $('#add-attribute').find('button').on('click', function () { + $.ajax(location + '/attribute.json', { + method: 'POST', + data: { + _method: 'PUT', + dataType: 'json', + error_template_attribute_id: $('#add-attribute').find('select').val() + } + }).success(function () { + location.reload(); + }).error(function (error) { + $.flash.danger({text: error.statusText}); + }); + }); + } +}); \ No newline at end of file diff --git a/app/assets/stylesheets/error_templates.scss b/app/assets/stylesheets/error_templates.scss new file mode 100644 index 00000000..29b2f96c --- /dev/null +++ b/app/assets/stylesheets/error_templates.scss @@ -0,0 +1,9 @@ +#add-attribute { + display: flex; + max-width: 400px; + margin-top: 30px; + + button { + margin-left: 10px; + } +} \ No newline at end of file diff --git a/app/controllers/error_templates_controller.rb b/app/controllers/error_templates_controller.rb index 6bf9a4f2..2632abf9 100644 --- a/app/controllers/error_templates_controller.rb +++ b/app/controllers/error_templates_controller.rb @@ -1,5 +1,5 @@ class ErrorTemplatesController < ApplicationController - before_action :set_error_template, only: [:show, :edit, :update, :destroy] + before_action :set_error_template, only: [:show, :edit, :update, :destroy, :add_attribute, :remove_attribute] def authorize! authorize(@error_templates || @error_template) @@ -73,6 +73,24 @@ class ErrorTemplatesController < ApplicationController end end + def add_attribute + authorize! + @error_template.error_template_attributes << ErrorTemplateAttribute.find(params['error_template_attribute_id']) + respond_to do |format| + format.html { redirect_to @error_template } + format.json { head :no_content } + end + end + + def remove_attribute + authorize! + @error_template.error_template_attributes.delete(ErrorTemplateAttribute.find(params['error_template_attribute_id'])) + respond_to do |format| + format.html { redirect_to @error_template } + format.json { head :no_content } + end + end + private # Use callbacks to share common setup or constraints between actions. def set_error_template diff --git a/app/policies/error_template_policy.rb b/app/policies/error_template_policy.rb index 30bdbd24..908be08e 100644 --- a/app/policies/error_template_policy.rb +++ b/app/policies/error_template_policy.rb @@ -1,3 +1,9 @@ class ErrorTemplatePolicy < AdminOnlyPolicy + def add_attribute? + admin? + end + def remove_attribute? + admin? + end end diff --git a/app/views/error_templates/show.html.slim b/app/views/error_templates/show.html.slim index cc262171..5b513e6c 100644 --- a/app/views/error_templates/show.html.slim +++ b/app/views/error_templates/show.html.slim @@ -31,4 +31,10 @@ h3 td = attribute.description td = attribute.regex td = link_to(t('shared.show'), attribute) - td = "Remove" + td = link_to(t('shared.destroy'), attribute_error_template_url(:error_template_attribute_id => attribute.id), :method => :delete) + +#add-attribute + = collection_select({}, :error_template_attribute_id, + ErrorTemplateAttribute.where.not(id: @error_template.error_template_attributes.select(:id).to_a).order(:important, :key), + :id, :key, {include_blank: false}, class: '') + button.btn.btn-default = t('error_templates.add_attribute') diff --git a/config/locales/de.yml b/config/locales/de.yml index edb5610f..c2414a16 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -600,3 +600,4 @@ de: hints: signature: "Ein regulärer Ausdruck in Ruby-Syntax und ohne führende und schließende \"/\"" attributes: "Attribute" + add_attribute: "Attribut hinzufügen" diff --git a/config/locales/en.yml b/config/locales/en.yml index 7cfe4c48..d76774d2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -621,3 +621,4 @@ en: hints: signature: "A regular expression in Ruby syntax without leading and trailing \"/\"" attributes: "Attributes" + add_attribute: "Add attribute" diff --git a/config/routes.rb b/config/routes.rb index c614a50c..24a9671d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,12 @@ FILENAME_REGEXP = /[\w\.]+/ unless Kernel.const_defined?(:FILENAME_REGEXP) Rails.application.routes.draw do resources :error_template_attributes - resources :error_templates + resources :error_templates do + member do + put 'attribute', to: 'error_templates#add_attribute' + delete 'attribute', to: 'error_templates#remove_attribute' + end + end resources :file_templates do collection do get 'by_file_type/:file_type_id', as: :by_file_type, action: :by_file_type