From 6dd7b2a85fa125937e647818fd7fa02cdfaf7415 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 14 Dec 2018 13:04:45 -0300 Subject: [PATCH] agregar grupos de checkboxes --- app/models/post.rb | 2 +- app/models/post/template_field.rb | 6 ++++++ app/views/posts/template_field/_check_box_group.haml | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 app/views/posts/template_field/_check_box_group.haml diff --git a/app/models/post.rb b/app/models/post.rb index 8710aed..6837683 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -327,7 +327,7 @@ class Post # Como get_front_matter pero con un valor por defecto def fetch_front_matter(name, default) - @front_matter.fetch(name, default) + get_front_matter(name) || default end # Trae el template a partir del layout diff --git a/app/models/post/template_field.rb b/app/models/post/template_field.rb index 99d5495..0215671 100644 --- a/app/models/post/template_field.rb +++ b/app/models/post/template_field.rb @@ -56,6 +56,8 @@ class Post @type = 'year' when text_area? @type = 'text_area' + when check_box_group? + @type = 'check_box_group' when string? @type = 'text' # TODO volver a hacer funcionar esto y ahorranos los multiple: @@ -176,6 +178,10 @@ class Post { key => empty_value } end + def check_box_group? + array? && contents.fetch('checkbox', false) + end + def array? value.is_a? Array end diff --git a/app/views/posts/template_field/_check_box_group.haml b/app/views/posts/template_field/_check_box_group.haml new file mode 100644 index 0000000..1a035e9 --- /dev/null +++ b/app/views/posts/template_field/_check_box_group.haml @@ -0,0 +1,12 @@ +-# Obtener el nombre del campo +- field_name = field_name_for_post_as_string(name) +-# Obtener todos los valores +- value = [] if value.nil? +- values = template.values.empty? ? [value].flatten : template.values +- values.each do |v| + .form-check + = check_box_tag field_name+'[]', + v, + @post.fetch_front_matter(name, []).include?(v), + class: 'form-check-input' + = label_tag field_name, v, class: 'form-check-label'