diff --git a/app/assets/javascripts/nested_setting.js b/app/assets/javascripts/nested_setting.js index ad94241..ef837fb 100644 --- a/app/assets/javascripts/nested_setting.js +++ b/app/assets/javascripts/nested_setting.js @@ -2,6 +2,25 @@ "use strict"; $(function(){ + if($('.nested-column.multiple').length === 0) return; + + var $setting = $('.nested-column.multiple:first'); + var counter = 0; + + $('.append', $setting).on('click', function(ev){ + ev.preventDefault(); + var $new = $setting.clone(true); + var elements = $('.form-control', $new); + _.each(elements, function(elm){ + elm.name = elm.name.replace("0", ++counter); + }); + var $close = $(this).clone().text('-'); + $close.on('click', function(){ + $new.remove(); + }); + $(".append", $new).replaceWith($close); + $new.appendTo($setting.parent()); + }); }); })(); diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index 773f558..5acca10 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -17,14 +17,19 @@ module SettingsHelper html << h(form.label(key)) child_data = form.object.class.children[key] klass = child_data[:class] + options = child_data[:options] children = form.object.send(key) || {"0" => {}} children.each_pair do |index, child| - # TODO: allow append/delete for multiple child - form.fields_for("#{key}[#{index}]", klass.new(child), class: "nested-column #{child_data[:multiple] ? "multiple" : ""} well well-sm") do |ff| + html << %Q!
! + if options[:multiple] + html << %Q!+! + end + form.fields_for("#{key}[#{index}]", klass.new(child)) do |ff| klass::KEYS.each do |k| html << field(ff, k) end end + html << "
" end else html << h(form.label(key)) diff --git a/app/models/fluentd/setting/common.rb b/app/models/fluentd/setting/common.rb index d1f883f..976b749 100644 --- a/app/models/fluentd/setting/common.rb +++ b/app/models/fluentd/setting/common.rb @@ -57,6 +57,18 @@ class Fluentd end end + def children_of(key) + meta = self.class.children[key] + return unless meta + klass = meta[:class] + data = send(key) || {"0" => {}} + children = [] + data.each_pair do |index, attrs| + children << klass.new(attrs) + end + children + end + def child_class(key) self.class.children[key][:class] end diff --git a/app/models/fluentd/setting/out_forward.rb b/app/models/fluentd/setting/out_forward.rb index 75605df..2c56350 100644 --- a/app/models/fluentd/setting/out_forward.rb +++ b/app/models/fluentd/setting/out_forward.rb @@ -12,12 +12,13 @@ class Fluentd flags :standby validates :host, presence: true + validates :port, presence: true end class Secondary include Common KEYS = [ - :path, :type + :type, :path ].freeze attr_accessor(*KEYS) @@ -37,24 +38,32 @@ class Fluentd attr_accessor(*KEYS) choice :heartbeat_type, %w(udp tcp) - nested :server, Server + nested :server, Server, multiple: true nested :secondary, Secondary validates :match, presence: true - validate :validate_at_least_one_server + validate :validate_has_at_least_one_server validate :validate_nested_values - def validate_at_least_one_server - # FIXME: real validation - true + def validate_has_at_least_one_server + if children_of(:server).reject{|s| s.empty_value? }.blank? + errors.add(:base, :out_forward_blank_server) + end end def validate_nested_values - # FIXME: real validation with child class instance - self.class.children.inject(true) do |result, child| - # result & child.valid? + self.class.children.inject(true) do |result, (key, _)| + children_of(key).each do |child| + if !child.empty_value? && !child.valid? + child.errors.full_messages.each do |message| + errors.add(:base, "(#{key})#{message}") + end + result = false + end + result + end + result end - true end end end diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index ae72251..c8281b5 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -373,6 +373,7 @@ en: lack_write_permission: doesn't writable is_a_directory: is a directory. should be a file duplicated_conf: Configurations is duplicated + out_forward_blank_server: server settings should be at least one activemodel: &activemodel errors: diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 0e3dc1f..8b5a347 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -370,6 +370,7 @@ ja: lack_write_permission: の書き込み権限がありません is_a_directory: はディレクトリです。ファイルを指定してください duplicated_conf: 同じ内容の設定がすでに存在しています + out_forward_blank_server: serverの設定がありません。 activemodel: &activemodel errors: