Last active
May 14, 2021 18:45
-
-
Save nicolas-brousse/f4530e466c71f909e758c1817032cd39 to your computer and use it in GitHub Desktop.
Formtastic PostgreSQL Array input (inspered from https://gist.github.com/franks921/44509c65da3bea99bc49)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('form .input.array').each(function() { | |
var $wrapper = $('.array-inputs', this); | |
var $insertArea = $(".array-input button[data-action=add]").closest(".array-input"); | |
$(".array-input button[data-action=add]", $(this)).click(function(e) { | |
$('.array-input:first-child', $wrapper).clone(true).insertBefore($insertArea).find('input').val('').focus(); | |
}); | |
$('.array-input button[data-action=remove]', $wrapper).click(function() { | |
if ($('.array-input', $wrapper).length > 2) { | |
$(this).parent('.array-input').remove(); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrayInput | |
include Formtastic::Inputs::Base | |
def to_html | |
input_wrapping do | |
array_wrapping do | |
legend_html << | |
array_inputs_wrapping do | |
values.each_with_index.map do |value, i| | |
array_input_wrapping do | |
if value.nil? | |
action_button(false) | |
else | |
array_input_html(value) | |
end | |
end | |
end.join("\n").html_safe | |
end | |
end | |
end | |
end | |
private | |
def values | |
values = [] | |
@object[method].each_with_index do |v, x| | |
values << v | |
end | |
values << "" if values.empty? | |
values << nil | |
values | |
end | |
def array_input_html(value) | |
button = action_button | |
# template.content_tag(:div) do | |
template.text_field_tag("#{object_name}[#{method}][]", value, id: nil) << button | |
# end | |
end | |
def action_button(remove = true) | |
if remove | |
template.content_tag(:button, template.fa_icon("minus-circle"), type: "button", "data-action": "remove") | |
else | |
template.content_tag(:button, template.fa_icon("plus-circle"), type: "button", "data-action": "add") | |
end | |
end | |
def array_wrapping(&block) | |
template.content_tag(:fieldset, | |
template.capture(&block), | |
class: "array" | |
) | |
end | |
def array_inputs_wrapping(&block) | |
template.content_tag(:ol, | |
template.capture(&block), | |
class: "array-inputs" | |
) | |
end | |
def array_input_wrapping(&block) | |
template.content_tag(:li, | |
template.capture(&block), | |
class: "array-input" | |
) | |
end | |
def legend_html | |
if render_label? | |
template.content_tag(:legend, | |
template.content_tag(:label, label_text), | |
label_html_options.merge(:class => "label") | |
) | |
else | |
"".html_safe | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment