From 2f17d00ae169a340689c36a0070277d6fba5a860 Mon Sep 17 00:00:00 2001 From: kirykr Date: Fri, 4 Nov 2022 15:47:37 +0700 Subject: [PATCH] Migrate local fields to custom fields --- app/controllers/program_streams_controller.rb | 9 ++-- app/helpers/client_enrollment_helper.rb | 5 +- app/helpers/custom_form_builder_helper.rb | 2 +- app/helpers/program_stream_helper.rb | 4 +- .../_form.haml | 2 +- .../_all_ngos_program_streams_tab.haml | 2 +- app/views/program_streams/_tracking.haml | 4 +- app/views/shared/fields/_select.haml | 22 +++++++-- lib/tasks/custom_form_field.rake | 47 +++++++++++++++++++ 9 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 lib/tasks/custom_form_field.rake diff --git a/app/controllers/program_streams_controller.rb b/app/controllers/program_streams_controller.rb index ea530c69ef..fb776faa79 100644 --- a/app/controllers/program_streams_controller.rb +++ b/app/controllers/program_streams_controller.rb @@ -12,7 +12,7 @@ class ProgramStreamsController < AdminController def index @program_streams = paginate_collection(decorate_programs(column_order)).page(params[:page_1]).per(20) - @ngos_program_streams = paginate_collection(decorate_programs(program_stream_ordered)).page(params[:page_2]).per(20) + @ngos_program_streams = paginate_collection(program_stream_ordered).page(params[:page_2]).per(20) @demo_program_streams = paginate_collection(decorate_programs(program_stream_ordered('demo'))).page(params[:page_3]).per(20) unless current_organization.short_name == 'demo' end @@ -145,7 +145,10 @@ def find_program_stream_organizations(org = '') organizations = org == 'demo' ? Organization.where(short_name: 'demo') : Organization.oscar.order(:full_name) program_streams = organizations.map do |org| Organization.switch_to org.short_name - ProgramStream.all.reload + programs = ProgramStream.includes(:services).all.reload.map do |program_stream| + OpenStruct.new({ **program_stream.attributes.symbolize_keys, services: program_stream.services, domains: program_stream.domains }) + end + decorate_programs(programs) end Organization.switch_to(current_org_name) program_streams.flatten @@ -171,7 +174,7 @@ def column_order column == "quantity" ? "#{column}" : "lower(#{column})" (order_string = "#{column} #{sort_by}") if column.present? - ProgramStream.ordered_by(order_string) + ProgramStream.includes(:services, :client_enrollments).ordered_by(order_string) end def program_stream_ordered(org = '') diff --git a/app/helpers/client_enrollment_helper.rb b/app/helpers/client_enrollment_helper.rb index 0af16f838c..bd14fcc0ee 100644 --- a/app/helpers/client_enrollment_helper.rb +++ b/app/helpers/client_enrollment_helper.rb @@ -48,13 +48,13 @@ def client_enrollment_edit_link def client_enrollment_new_link(program_stream) if program_permission_editable?(program_stream) && policy(@client).create? link_to new_client_client_enrollment_path(@client, program_stream_id: program_stream.id) do - content_tag :div, class: 'btn btn-primary btn-xs btn-width' do + content_tag :div, class: 'btn btn-primary btn-xs btn-width' do t('.enroll') end end else link_to_if false, new_client_client_enrollment_path(@client, program_stream_id: program_stream.id) do - content_tag :div, class: 'btn btn-primary btn-xs btn-width disabled' do + content_tag :div, class: 'btn btn-primary btn-xs btn-width disabled' do t('.enroll') end end @@ -79,6 +79,5 @@ def client_enrollment_destroy_link def field_label(props, index = nil) label = I18n.locale.to_s == I18n.default_locale.to_s ? props['label'] : props['local_label'] - label end end diff --git a/app/helpers/custom_form_builder_helper.rb b/app/helpers/custom_form_builder_helper.rb index 78e6d2b9eb..bbc8ecb66b 100644 --- a/app/helpers/custom_form_builder_helper.rb +++ b/app/helpers/custom_form_builder_helper.rb @@ -7,7 +7,7 @@ def disable_action_on_custom_form(custom_field) used_custom_form?(custom_field) ? 'disabled' : '' end - def field_with(field,errors) + def field_with(field, errors) errors.has_key?(field.to_sym) ? 'has-error' : '' end diff --git a/app/helpers/program_stream_helper.rb b/app/helpers/program_stream_helper.rb index 402d8e20d0..1de779bb1d 100644 --- a/app/helpers/program_stream_helper.rb +++ b/app/helpers/program_stream_helper.rb @@ -109,7 +109,7 @@ def group_selection_field_types(program_stream, program_stream_step) group_value_field_types = [] case program_stream_step when 'trackings' - program_stream.client_enrollments.each do |client_enrollment| + program_stream.client_enrollments.includes(:client_enrollment_trackings).each do |client_enrollment| client_enrollment.client_enrollment_trackings.each do |client_enrollment_tracking| choosen_option_form_tracking = client_enrollment_tracking.properties if client_enrollment_tracking.properties.present? group_value_field_types << choosen_option_form_tracking @@ -123,7 +123,7 @@ def group_selection_field_types(program_stream, program_stream_step) end group_value_field_types when 'exit_program' - program_stream.client_enrollments.each do |client_enrollment| + program_stream.client_enrollments.includes(:leave_program).each do |client_enrollment| choosen_option_form_exit_program = client_enrollment.leave_program.properties if client_enrollment.leave_program&.properties.present? group_value_field_types << choosen_option_form_exit_program end diff --git a/app/views/client_enrolled_program_trackings/_form.haml b/app/views/client_enrolled_program_trackings/_form.haml index ab38009291..6ea7f5040b 100644 --- a/app/views/client_enrolled_program_trackings/_form.haml +++ b/app/views/client_enrolled_program_trackings/_form.haml @@ -8,7 +8,7 @@ / = render "/shared/fields/#{field['type'].underscore}", f: prop_f, field_props: field, errors: @client_enrollment_tracking.errors.messages - field['label'] = convert_bracket(field['label'], @client_enrollment_tracking.properties) - unless field['type'] == 'file' - = render "/shared/fields/#{field['type'].underscore}", f: prop_f, field_props: field, errors: @client_enrollment_tracking.errors.messages, entity: f + = render "shared/fields/#{field['type'].underscore}", f: prop_f, field_props: field, errors: @client_enrollment_tracking.errors.messages, entity: f - else = f.simple_fields_for :form_builder_attachments, @attachments.file_by_name(field['label']) || @attachments.build do |attachment| = render 'shared/form_builder/attachment', f: attachment, property: field, entity: f, resource: @client_enrollment_tracking diff --git a/app/views/program_streams/_all_ngos_program_streams_tab.haml b/app/views/program_streams/_all_ngos_program_streams_tab.haml index fe12a4e2bc..aa2a22be7e 100644 --- a/app/views/program_streams/_all_ngos_program_streams_tab.haml +++ b/app/views/program_streams/_all_ngos_program_streams_tab.haml @@ -26,7 +26,7 @@ %tr %td.name= program_stream.name %td.domain= program_stream.domains.pluck(:identity).join(', ') - %td= program_stream.services.distinct.map(&:name).join(', ') + %td= program_stream.services.uniq.map(&:name).join(', ') %td.status %label{ class: "#{program_stream.completed_label_class}"} = program_stream.completed_status diff --git a/app/views/program_streams/_tracking.haml b/app/views/program_streams/_tracking.haml index df569e2090..9dc1e83b12 100644 --- a/app/views/program_streams/_tracking.haml +++ b/app/views/program_streams/_tracking.haml @@ -1,7 +1,7 @@ %h4= t('.tracking_form') %section #trackings{ data: { complete_tracking: t('.complete_tracking') } } - = f.simple_fields_for :trackings, @tracking do |ff| + = f.simple_fields_for :trackings, f.object.trackings.any? && f.object.trackings.includes(:client_enrollment_trackings) || @tracking do |ff| = render 'tracking_fields', f: ff .links = link_to_add_association t('.add_tracking'), f, :trackings, class: 'btn btn-primary pull-right' @@ -11,4 +11,4 @@ - form_builder_selection_options(@program_stream, 'trackings') %div.hidden.tracking_select_option{"data-tracking-select-option" => "#{@select_field}"} %div.hidden.tracking_checkbox_option{"data-tracking_checkbox_option" => "#{@checkbox_field}"} -%div.hidden.tracking_radio_option{"data-tracking_radio_option" => "#{@radio_field}"} \ No newline at end of file +%div.hidden.tracking_radio_option{"data-tracking_radio_option" => "#{@radio_field}"} diff --git a/app/views/shared/fields/_select.haml b/app/views/shared/fields/_select.haml index df1c691f16..abe60e0c87 100644 --- a/app/views/shared/fields/_select.haml +++ b/app/views/shared/fields/_select.haml @@ -1,10 +1,24 @@ - label = field_label(field_props) -%div{ class: "form-group #{field_with(label,errors)}" } +%div{ class: "form-group #{field_with(label, errors)}" } %label.control-label{ class: required?(field_props['required'] || false) } %abbr{ title: 'required' }= '*' if field_props['required'] || false = label.gsub('&#91;', '[').gsub('&#93;', ']').html_safe - - = f.input remove_field_prop_unicode(field_props), collection: field_props['values'].map { |f| [format_placeholder(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), input_html: { id: field_props['label'].gsub(' ', '_').gsub("'", ""), class: "#{field_props['ClassName']} #{I18n.locale.to_s != I18n.default_locale.to_s ? 'd-none' : ''}", multiple: field_props['multiple'].present?, name: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}][]", placeholder: format_placeholder(field_props['placeholder']), 'data-label': field_props["local_label"].gsub(' ', '_').gsub("'", "") }, label: false - = f.input "Local_label #{remove_local_field_prop_unicode(field_props)}", collection: field_props['values'].map { |f| [format_placeholder(f['local_label']), f['local_label'], id: "custom_field_property_properties_#{field_props["local_label"].gsub('"', '&qoute;').html_safe}_#{f['local_label'].html_safe}", 'data-value': f['label']] }, required: (field_props['required'] || false), input_html: { id: field_props["local_label"].gsub(' ', '_').gsub("'", ""), class: "#{field_props['ClassName']} #{I18n.locale.to_s == I18n.default_locale.to_s ? 'd-none1' : ''}", multiple: field_props['multiple'].present?, name: "#{f.object_name}[Local_label #{remove_local_field_prop_unicode(field_props)}][]", placeholder: format_placeholder(field_props['placeholder']), 'data-label': field_props["label"].gsub(' ', '_').gsub("'", "") }, label: false + - collection = field_props['values'].map { |f| [format_placeholder(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']] } + - if I18n.locale == :en || params[:locale] == 'en' + = f.input remove_field_prop_unicode(field_props), | + collection: collection, | + required: (field_props['required'] || false), input_html: { id: field_props['label'].gsub(' ', '_').gsub("'", ""), | + class: "#{field_props['ClassName']} #{I18n.locale.to_s != I18n.default_locale.to_s ? 'd-none' : ''}", multiple: field_props['multiple'].present?, | + name: "#{f.object_name}[#{remove_field_prop_unicode(field_props)}][]", placeholder: format_placeholder(field_props['placeholder']), | + 'data-label': field_props["label"].gsub(' ', '_').gsub("'", "") }, label: false + - else + = f.input "Local_label #{remove_local_field_prop_unicode(field_props)}", | + collection: field_props['values'].map { |f| [format_placeholder(f['local_label']), f['local_label'], id: "custom_field_property_properties_#{field_props["local_label"].gsub('"', '&qoute;').html_safe}_#{f['local_label'].html_safe}", 'data-value': f['label']] }, | + required: (field_props['required'] || false), | + input_html: { id: field_props["local_label"].gsub(' ', '_').gsub("'", ""), + class: "#{field_props['ClassName']} #{I18n.locale.to_s == I18n.default_locale.to_s ? 'd-none1' : ''}", + multiple: field_props['multiple'].present?, name: "#{f.object_name}[Local_label #{remove_local_field_prop_unicode(field_props)}][]", + placeholder: format_placeholder(field_props['placeholder']), + 'data-label': field_props["local_label"].gsub(' ', '_').gsub("'", "") }, label: false %span.help-block = field_message(label, errors) diff --git a/lib/tasks/custom_form_field.rake b/lib/tasks/custom_form_field.rake new file mode 100644 index 0000000000..2e3edc66a4 --- /dev/null +++ b/lib/tasks/custom_form_field.rake @@ -0,0 +1,47 @@ +namespace :custom_form_field do + desc "Migrate local label and values for custom field/form" + task migrate: :environment do + Organization.without_shared.pluck(:short_name).each do |short_name| + next unless short_name == 'cif' + Apartment::Tenant.switch short_name + + ProgramStream.paper_trail.disable + ProgramStream.all.each do |program_stream| + enrollment = populate_local_fields(program_stream.enrollment) + exit_program = populate_local_fields(program_stream.exit_program) + program_stream.enrollment = enrollment + program_stream.exit_program = exit_program + program_stream.save + end + ProgramStream.paper_trail.enable + + Tracking.paper_trail.disable + Tracking.all.each do |tracking| + fields = populate_local_fields(tracking.fields) + tracking.fields = fields + tracking.save + end + Tracking.paper_trail.enable + + CustomField.paper_trail.disable + CustomField.all.each do |custom_field| + fields = populate_local_fields(custom_field.fields) + custom_field.fields = fields + custom_field.save + end + CustomField.paper_trail.enable + end + end +end + +def populate_local_fields(fields) + fields.map do |field_hash| + if field_hash.has_key?('local_label') + field_hash + elsif field_hash.has_key?('values') + field_hash.merge('local_label' => field_hash['label'], 'values' => field_hash['values'].map{|values| values.merge('local_value' => values['value'], 'local_label' => values['label']) }) + else + field_hash.merge('local_label' => field_hash['label']) + end + end +end