From e26becde87269dc670947a4f900c2fc70d18ab37 Mon Sep 17 00:00:00 2001 From: kirykr Date: Tue, 12 Dec 2023 16:21:24 +0700 Subject: [PATCH] Update client enrollment field properties checkboxes functionality --- app/controllers/clients_controller.rb | 2 +- app/helpers/custom_field_properties_helper.rb | 99 ++++++++++--------- app/helpers/custom_form_builder_helper.rb | 20 ++-- app/views/client_enrollments/show.haml | 3 +- app/views/shared/fields/_checkbox_group.haml | 5 +- 5 files changed, 68 insertions(+), 61 deletions(-) diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 5771ed93b7..45b7d3746b 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -205,7 +205,7 @@ def create def update new_params = @client.current_family_id ? client_params : client_params.except(:family_ids) - if @client.update_attributes(client_params.except(:family_ids)) + if @client.update_attributes(new_params) if params[:client][:assessment_id] @assessment = Assessment.find(params[:client][:assessment_id]) redirect_to client_assessment_path(@client, @assessment), notice: t('.assessment_successfully_created') diff --git a/app/helpers/custom_field_properties_helper.rb b/app/helpers/custom_field_properties_helper.rb index db3058244a..8cd8e19149 100644 --- a/app/helpers/custom_field_properties_helper.rb +++ b/app/helpers/custom_field_properties_helper.rb @@ -58,9 +58,15 @@ def remove_field_prop_unicode(field_props) end def remove_local_field_prop_unicode(field_props) - return field_props['label'] if field_props["local_label"].nil? + return field_props['label'] if field_props['local_label'].nil? - field = field_props["local_label"].gsub(/\>\;|\<\;|\&\;|\"/, '<' => '<', '>' => '>', '&' => '&', '"' => '%22') + field = field_props['local_label'].gsub(/\>\;|\<\;|\&\;|\"/, '<' => '<', '>' => '>', '&' => '&', '"' => '%22') + end + + def is_field_checked?(obj, field_prop, field, local_field) + return true if obj.to_h[field_prop['label'].to_sym] && obj.to_h[field_prop['label'].to_sym].include?(field) + + obj.to_h[field_prop['local_label'].to_sym] && obj.to_h[field_prop['local_label'].to_sym].include?(local_field) end def mapping_custom_field_values(field_props) @@ -87,61 +93,62 @@ def display_custom_formable_lebel(klass_object) end private - def form_builder_selection_options_custom_form(custom_field) - field_types = group_field_types_custom_form(custom_field) - @select_field_custom_form = field_types["select"] - @checkbox_field_custom_form = field_types["checkbox-group"] - @radio_field_custom_form = field_types["radio-group"] - end - def group_field_types_custom_form(custom_field) - group_field_types = Hash.new{|h,k| h[k] = []} - group_by_option_type_label = form_builder_group_by_options_type_label_custom_form(custom_field) - group_selection_field_types = group_selection_field_types_custom_form(custom_field) - if group_selection_field_types.present? - group_selection_field_types&.compact.each do |selection_field_types| - group_by_option_type_label.each do |type,labels| - next unless labels.present? - labels.each do |label| - next if selection_field_types[label].blank? - group_field_types[type] << selection_field_types[label] - end + def form_builder_selection_options_custom_form(custom_field) + field_types = group_field_types_custom_form(custom_field) + @select_field_custom_form = field_types['select'] + @checkbox_field_custom_form = field_types['checkbox-group'] + @radio_field_custom_form = field_types['radio-group'] + end + + def group_field_types_custom_form(custom_field) + group_field_types = Hash.new { |h, k| h[k] = [] } + group_by_option_type_label = form_builder_group_by_options_type_label_custom_form(custom_field) + group_selection_field_types = group_selection_field_types_custom_form(custom_field) + if group_selection_field_types.present? + group_selection_field_types&.compact.each do |selection_field_types| + group_by_option_type_label.each do |type, labels| + next unless labels.present? + labels.each do |label| + next if selection_field_types[label].blank? + group_field_types[type] << selection_field_types[label] end end end - group_field_types = group_field_types.transform_values(&:flatten) - group_field_types.transform_values(&:uniq) end + group_field_types = group_field_types.transform_values(&:flatten) + group_field_types.transform_values(&:uniq) + end - def group_selection_field_types_custom_form(custom_field) - group_value_field_types = [] - custom_field.custom_field_properties.each do |custom_field_property| - group_value_field_types << custom_field_property.properties if custom_field_property.properties.present? - end - return group_value_field_types + def group_selection_field_types_custom_form(custom_field) + group_value_field_types = [] + custom_field.custom_field_properties.each do |custom_field_property| + group_value_field_types << custom_field_property.properties if custom_field_property.properties.present? end + return group_value_field_types + end - def form_builder_group_by_options_type_label_custom_form(custom_field) - group_options_type_label = Hash.new{|h,k| h[k] = []} - form_builder_option = form_builder_options_custom_form(custom_field) - form_builder_option["type"].each_with_index do |type_option,i| - group_options_type_label[type_option] << form_builder_option["label"][i] - end - group_options_type_label + def form_builder_group_by_options_type_label_custom_form(custom_field) + group_options_type_label = Hash.new { |h, k| h[k] = [] } + form_builder_option = form_builder_options_custom_form(custom_field) + form_builder_option['type'].each_with_index do |type_option, i| + group_options_type_label[type_option] << form_builder_option['label'][i] end + group_options_type_label + end - def form_builder_options_custom_form(custom_field) - form_builder_options = Hash.new{|h,k| h[k] = []} - custom_field.fields.each do |field| - field.each do |k,v| - next unless k[/^(type|label)$/i] - form_builder_options[k] << v - end + def form_builder_options_custom_form(custom_field) + form_builder_options = Hash.new { |h, k| h[k] = [] } + custom_field.fields.each do |field| + field.each do |k, v| + next unless k[/^(type|label)$/i] + form_builder_options[k] << v end - return form_builder_options end + return form_builder_options + end - def is_custom_field_property_editable?(custom_field_property) - Organization.ratanak? && !current_user.admin? ? custom_field_editable?(@custom_field) && custom_field_property.is_editable? : custom_field_editable?(@custom_field) - end + def is_custom_field_property_editable?(custom_field_property) + Organization.ratanak? && !current_user.admin? ? custom_field_editable?(@custom_field) && custom_field_property.is_editable? : custom_field_editable?(@custom_field) + end end diff --git a/app/helpers/custom_form_builder_helper.rb b/app/helpers/custom_form_builder_helper.rb index bbc8ecb66b..f7a0b8fd5a 100644 --- a/app/helpers/custom_form_builder_helper.rb +++ b/app/helpers/custom_form_builder_helper.rb @@ -15,14 +15,14 @@ def field_message(field, errors) errors[field.to_sym].join(', ') if errors[field.to_sym].present? end - def display_custom_properties(value, type=nil) + def display_custom_properties(value, type = nil) div = content_tag :div do if value =~ /(\d{4}[-\/]\d{1,2}[-\/]\d{1,2})/ concat value.to_date.strftime('%d %B %Y') elsif value.is_a?(Array) return value.join(', ') if type == 'select' || type == 'checkbox-group' - value.reject{ |i| i.empty? }.each do |c| - concat content_tag(:strong, c.gsub('&qoute;', '"').html_safe, class: 'label label-margin') + value.reject { |i| i.empty? }.each do |c| + concat content_tag(:strong, c.gsub('&qoute;', '"').html_safe, class: 'label-margin') end elsif value.is_a?(Hash) display_custom_properties(value.values.flatten) @@ -31,7 +31,7 @@ def display_custom_properties(value, type=nil) end end content = div.gsub('&nbsp;', '') - content = content.gsub("\n",'
') + content = content.gsub("\n", '
') content = content.gsub('<', '<') content = content.gsub('>', '>') content.html_safe @@ -39,10 +39,10 @@ def display_custom_properties(value, type=nil) def custom_field_frequency(frequency, time_of_frequency) case frequency - when 'Daily' then time_of_frequency.day - when 'Weekly' then time_of_frequency.week + when 'Daily' then time_of_frequency.day + when 'Weekly' then time_of_frequency.week when 'Monthly' then time_of_frequency.month - when 'Yearly' then time_of_frequency.year + when 'Yearly' then time_of_frequency.year else 0.day end end @@ -50,10 +50,10 @@ def custom_field_frequency(frequency, time_of_frequency) def frequency_note(custom_field) return if custom_field.frequency.empty? frequency = case custom_field.frequency - when 'Daily' then 'day' - when 'Weekly' then 'week' + when 'Daily' then 'day' + when 'Weekly' then 'week' when 'Monthly' then 'month' - when 'Yearly' then 'year' + when 'Yearly' then 'year' end if custom_field.time_of_frequency == 1 "This needs to be done once every #{frequency}." diff --git a/app/views/client_enrollments/show.haml b/app/views/client_enrollments/show.haml index 9b71309e13..08ce125a9a 100644 --- a/app/views/client_enrollments/show.haml +++ b/app/views/client_enrollments/show.haml @@ -29,4 +29,5 @@ - if type == 'file' = render 'shared/form_builder/list_attachment', label: key, resource: @client_enrollment - else - = display_custom_properties(@client_enrollment.properties[key]) + %ul.list-group + = display_custom_properties(@client_enrollment.properties[key]) diff --git a/app/views/shared/fields/_checkbox_group.haml b/app/views/shared/fields/_checkbox_group.haml index 95603f51af..91cf62ff4a 100644 --- a/app/views/shared/fields/_checkbox_group.haml +++ b/app/views/shared/fields/_checkbox_group.haml @@ -3,11 +3,10 @@ %label.control-label{ class: required?(field_props['required'] || false) } %abbr{ title: 'required' }= '*' if field_props['required'] || false = label.html_safe - - if I18n.locale.to_s == I18n.default_locale.to_s = f.input remove_field_prop_unicode(field_props), collection: field_props['values'].map { |f| [ f['label'], f['label'], id: "custom_field_property_properties_#{field_props['label'].gsub('"', '&qoute;').html_safe}_#{f['label'].html_safe}", 'data-value': f['local_label']] }, as: :check_boxes, required: (field_props['required'] || false), label: false, input_html: { name: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}][]", title: field_props['description'], 'data-checkbox': "#{f.object_name}[#{remove_local_field_prop_unicode(field_props)}]".parameterize } - else - = f.input "Local_label #{remove_local_field_prop_unicode(field_props)}", collection: field_props['values'].map { |f| [ f['local_label'], f['local_label'], id: "custom_field_property_properties_#{field_props['label'].gsub('"', '&qoute;').html_safe}_#{f['label'].html_safe}", 'data-value': f['label']] }, as: :check_boxes, required: (field_props['required'] || false), input_html: { name: "#{f.object_name}[Local_label #{remove_local_field_prop_unicode(field_props)}][]", 'data-checkbox': "#{f.object_name}[#{remove_field_prop_unicode(field_props)}]".parameterize }, label: false - = f.input remove_field_prop_unicode(field_props), collection: field_props['values'].map { |f| [ f['label'], f['label'], id: "custom_field_property_properties_#{field_props['label'].gsub('"', '&qoute;').html_safe}_#{f['label'].html_safe}"] }, required: (field_props['required'] || false), label: false, input_html: { name: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}][]", title: field_props['description'], id: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}]".parameterize, multiple: true, class: 'd-none' } + = f.input remove_local_field_prop_unicode(field_props), collection: field_props['values'].map { |field| [ field['local_label'], field['local_label'], checked: is_field_checked?(f.object, field_props, field['label'], field['local_label']), id: "custom_field_property_properties_#{field_props['label'].gsub('"', '&qoute;').html_safe}_#{field['label'].html_safe}", 'data-value': field['local_label']] }, as: :check_boxes, required: (field_props['required'] || false), label: false, input_html: { name: "#{f.object_name}[Local_label #{remove_local_field_prop_unicode(field_props)}][]", title: field_props['description'], 'data-checkbox': "#{f.object_name}[#{remove_local_field_prop_unicode(field_props)}]".parameterize } + = f.input remove_field_prop_unicode(field_props), collection: field_props['values'].map { |f| [ f['label'], f['label'], id: "custom_field_property_properties_#{field_props['label'].gsub('"', '&qoute;').html_safe}_#{f['label'].html_safe}", 'data-value': f['local_label']] }, required: (field_props['required'] || false), label: false, input_html: { name: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}][]", title: field_props['description'], id: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}]".parameterize, multiple: true, class: 'd-none' } %span.help-block = field_message(field_props['label'], errors)