Add primitive impl of mutilple <server> on out_forward

This commit is contained in:
uu59 2014-07-22 18:19:00 +09:00
parent 667b97c720
commit 6a4c551d77
6 changed files with 59 additions and 12 deletions

View File

@ -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());
});
});
})();

View File

@ -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!<div class="nested-column #{options[:multiple] ? "multiple" : ""} well well-sm">!
if options[:multiple]
html << %Q!<a class="btn btn-default append">+</a>!
end
form.fields_for("#{key}[#{index}]", klass.new(child)) do |ff|
klass::KEYS.each do |k|
html << field(ff, k)
end
end
html << "</div>"
end
else
html << h(form.label(key))

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -370,6 +370,7 @@ ja:
lack_write_permission: の書き込み権限がありません
is_a_directory: はディレクトリです。ファイルを指定してください
duplicated_conf: 同じ内容の設定がすでに存在しています
out_forward_blank_server: serverの設定がありません。
activemodel: &activemodel
errors: