Skip to content

Commit

Permalink
Migrate local fields to custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kirykr committed Nov 4, 2022
1 parent 4f43039 commit 2f17d00
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
9 changes: 6 additions & 3 deletions app/controllers/program_streams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 = '')
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/client_enrollment_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/helpers/custom_form_builder_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/helpers/program_stream_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/client_enrolled_program_trackings/_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/views/program_streams/_tracking.haml
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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}"}
%div.hidden.tracking_radio_option{"data-tracking_radio_option" => "#{@radio_field}"}
22 changes: 18 additions & 4 deletions app/views/shared/fields/_select.haml
Original file line number Diff line number Diff line change
@@ -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('&amp;#91;', '&#91;').gsub('&amp;#93;', '&#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)
47 changes: 47 additions & 0 deletions lib/tasks/custom_form_field.rake
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2f17d00

Please sign in to comment.