Skip to content

Commit

Permalink
Update client enrollment field properties checkboxes functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kirykr committed Dec 12, 2023
1 parent 9b92ccf commit e26becd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 61 deletions.
2 changes: 1 addition & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
99 changes: 53 additions & 46 deletions app/helpers/custom_field_properties_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\&gt\;|\&lt\;|\&amp\;|\"/, '&lt;' => '<', '&gt;' => '>', '&amp;' => '&', '"' => '%22')
field = field_props['local_label'].gsub(/\&gt\;|\&lt\;|\&amp\;|\"/, '&lt;' => '<', '&gt;' => '>', '&amp;' => '&', '"' => '%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)
Expand All @@ -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
20 changes: 10 additions & 10 deletions app/helpers/custom_form_builder_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('&amp;qoute;', '&quot;').html_safe, class: 'label label-margin')
value.reject { |i| i.empty? }.each do |c|
concat content_tag(:strong, c.gsub('&amp;qoute;', '&quot;').html_safe, class: 'label-margin')
end
elsif value.is_a?(Hash)
display_custom_properties(value.values.flatten)
Expand All @@ -31,29 +31,29 @@ def display_custom_properties(value, type=nil)
end
end
content = div.gsub('&amp;nbsp;', '')
content = content.gsub("\n",'<br />')
content = content.gsub("\n", '<br />')
content = content.gsub('&lt;', '<')
content = content.gsub('&gt;', '>')
content.html_safe
end

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

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}."
Expand Down
3 changes: 2 additions & 1 deletion app/views/client_enrollments/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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])
5 changes: 2 additions & 3 deletions app/views/shared/fields/_checkbox_group.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e26becd

Please sign in to comment.