From 604b6e499a9a385bcc5fef6ae15073dfeeb2584d Mon Sep 17 00:00:00 2001 From: kirykr Date: Wed, 2 Aug 2023 11:52:19 +0700 Subject: [PATCH] Update API V1 - added fildname to CustomFieldPropertyUploader serializable_hash - Fixed delete custom referrals --- .../api/v1/custom_field_properties_controller.rb | 14 +++++++++----- .../concerns/form_builder_attachments.rb | 14 +++++++------- .../custom_field_properties_controller.rb | 4 ++-- app/models/attachment.rb | 8 ++++++++ app/uploaders/custom_field_property_uploader.rb | 12 ++++++++++++ app/uploaders/form_builder_attachment_uploader.rb | 12 ++++++++++++ 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/v1/custom_field_properties_controller.rb b/app/controllers/api/v1/custom_field_properties_controller.rb index 0f1f2d0679..7230c755d4 100644 --- a/app/controllers/api/v1/custom_field_properties_controller.rb +++ b/app/controllers/api/v1/custom_field_properties_controller.rb @@ -33,12 +33,16 @@ def destroy name = params[:file_name] index = params[:file_index].to_i if name.present? && index.present? - delete_form_builder_attachment(@custom_field_property, name, index) - render json: { error: "Failed deleting attachment" } unless @custom_field_property.save + if delete_form_builder_attachment(@custom_field_property, name, index) + head 204 if @custom_field_property.save + else + render json: { error: "Failed deleting attachment" } + end + elsif @custom_field_property.destroy + head 204 else - 33054 + render json: { error: "Failed deleting custom field property" } end - head 204 end private @@ -61,7 +65,7 @@ def custom_field_property_params end end end - properties_params.values.map{ |v| v.delete('') if (v.is_a?Array) && v.size > 1 } if properties_params.present? + properties_params.values.map { |v| v.delete('') if (v.is_a? Array) && v.size > 1 } if properties_params.present? default_params = params.require(:custom_field_property).permit({}).merge(custom_field_id: params[:custom_field_id]) default_params = default_params.merge(properties: properties_params) if properties_params.present? default_params = default_params.merge(form_builder_attachments_attributes: attachment_params) if action_name == 'create' && attachment_params.present? diff --git a/app/controllers/concerns/form_builder_attachments.rb b/app/controllers/concerns/form_builder_attachments.rb index 559839b67c..ae35bd2ae0 100644 --- a/app/controllers/concerns/form_builder_attachments.rb +++ b/app/controllers/concerns/form_builder_attachments.rb @@ -29,15 +29,15 @@ def delete_form_builder_attachment(resource, name, index) end def attachment_params - if ['client_enrollments','client_enrolled_programs'].include?(controller_name) + if ['client_enrollments', 'client_enrolled_programs'].include?(controller_name) params[:client_enrollment][:form_builder_attachments_attributes] elsif ['client_enrollment_trackings', 'client_enrolled_program_trackings', 'client_trackings'].include?(controller_name) params[:client_enrollment_tracking][:form_builder_attachments_attributes] - elsif ['leave_programs','leave_enrolled_programs'].include?(controller_name) + elsif ['leave_programs', 'leave_enrolled_programs'].include?(controller_name) params[:leave_program][:form_builder_attachments_attributes] elsif ['custom_field_properties', 'client_custom_fields'].include?(controller_name) params[:custom_field_property][:form_builder_attachments_attributes] - elsif ['enrollments','enrolled_programs'].include?(controller_name) + elsif ['enrollments', 'enrolled_programs'].include?(controller_name) params[:enrollment][:form_builder_attachments_attributes] elsif ['enrollment_trackings', 'enrolled_program_trackings', 'trackings'].include?(controller_name) params[:enrollment_tracking][:form_builder_attachments_attributes] @@ -45,11 +45,11 @@ def attachment_params end def properties_params - if ['client_enrollments','client_enrolled_programs'].include?(controller_name) + if ['client_enrollments', 'client_enrolled_programs'].include?(controller_name) params[:client_enrollment][:properties] elsif ['client_enrollment_trackings', 'client_enrolled_program_trackings', 'client_trackings'].include?(controller_name) params[:client_enrollment_tracking][:properties] - elsif ['leave_programs','leave_enrolled_programs'].include?(controller_name) + elsif ['leave_programs', 'leave_enrolled_programs'].include?(controller_name) params[:leave_program][:properties] elsif ['custom_field_properties', 'client_custom_fields'].include?(controller_name) params[:custom_field_property][:properties] @@ -57,9 +57,9 @@ def properties_params end def entity_properties_params - if ['enrollments','enrolled_programs'].include?(controller_name) + if ['enrollments', 'enrolled_programs'].include?(controller_name) params[:enrollment][:properties] - elsif ['enrollment_trackings','enrolled_program_trackings', 'trackings'].include?(controller_name) + elsif ['enrollment_trackings', 'enrolled_program_trackings', 'trackings'].include?(controller_name) params[:enrollment_tracking][:properties] end end diff --git a/app/controllers/custom_field_properties_controller.rb b/app/controllers/custom_field_properties_controller.rb index 698d780743..65154b9352 100644 --- a/app/controllers/custom_field_properties_controller.rb +++ b/app/controllers/custom_field_properties_controller.rb @@ -65,8 +65,8 @@ def custom_field_property_params properties_params.each do |k, v| mappings[k] = k.gsub('&', '&').gsub('<', '<').gsub('>', '>').gsub('%22', '"') end - formatted_params = properties_params.map {|k, v| [mappings[k], v] }.to_h - formatted_params.values.map{ |v| v.delete('') if (v.is_a?Array) && v.size > 1 } + formatted_params = properties_params.map { |k, v| [mappings[k], v] }.to_h + formatted_params.values.map { |v| v.delete('') if (v.is_a? Array) && v.size > 1 } end default_params = params.require(:custom_field_property).permit({}).merge(custom_field_id: params[:custom_field_id]) default_params = default_params.merge(properties: formatted_params) if formatted_params.present? diff --git a/app/models/attachment.rb b/app/models/attachment.rb index c42617da91..9df9d44041 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -8,6 +8,14 @@ class Attachment < ActiveRecord::Base validate :image_size_validation + protected + + def _filename + file_name = File.basename(path).split('.').first.titleize + extention = File.basename(path).split('.').last + "#{file_name}.#{extention}" + end + private def image_size_validation diff --git a/app/uploaders/custom_field_property_uploader.rb b/app/uploaders/custom_field_property_uploader.rb index 5236c1032c..e30790fc62 100644 --- a/app/uploaders/custom_field_property_uploader.rb +++ b/app/uploaders/custom_field_property_uploader.rb @@ -17,6 +17,12 @@ def extension_white_list %w(jpg jpeg png doc docx xls xlsx pdf) end + def serializable_hash + super.merge( + filename: _filename + ) + end + protected def image?(new_file) @@ -24,4 +30,10 @@ def image?(new_file) new_file.content_type.start_with? 'image' end end + + def _filename + file_name = File.basename(path).split('.').first.titleize + extention = File.basename(path).split('.').last + "#{file_name}.#{extention}" + end end diff --git a/app/uploaders/form_builder_attachment_uploader.rb b/app/uploaders/form_builder_attachment_uploader.rb index 3953a4b88e..75dfe3bb07 100644 --- a/app/uploaders/form_builder_attachment_uploader.rb +++ b/app/uploaders/form_builder_attachment_uploader.rb @@ -17,6 +17,12 @@ def extension_white_list %w(jpg jpeg png doc docx xls xlsx pdf) end + def serializable_hash + super.merge( + filename: _filename + ) + end + protected def image?(new_file) @@ -24,4 +30,10 @@ def image?(new_file) new_file.content_type.start_with? 'image' end end + + def _filename + file_name = File.basename(path).split('.').first.titleize + extention = File.basename(path).split('.').last + "#{file_name}.#{extention}" + end end