From 0fdbf08f9e69228a2956eb3a8081bb9942613a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Fri, 23 Aug 2024 21:17:27 +0800 Subject: [PATCH] Refine constant variables. --- .../admin/cancerpredicts_controller.rb | 60 ++++++++----------- app/controllers/cancerpredicts_controller.rb | 12 ++-- app/models/cancerpredictfields.rb | 9 ++- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/app/controllers/admin/cancerpredicts_controller.rb b/app/controllers/admin/cancerpredicts_controller.rb index ab697e9..f694f2c 100644 --- a/app/controllers/admin/cancerpredicts_controller.rb +++ b/app/controllers/admin/cancerpredicts_controller.rb @@ -324,29 +324,23 @@ class Admin::CancerpredictsController < OrbitAdminController @form_to_show.auto_write_predict_js end end - @index = 0 - Dir.chdir("public") do - while File.exist?("cancerfield_back" + @index.to_s + ".txt") - @index += 1 - end + @site_locales = Site.last.in_use_locales.each do |locale| + I18n.with_locale(locale) do + File.open(Cancerpredictfields::ToolTableMap[I18n.locale], "w") do |f| + tmp_table_texts = create_table(locale) + f.write(tmp_table_texts) + end + end + end + idx = 0 + while File.exist?("public/cancerfield_back#{idx}.txt") + idx += 1 end - Dir.chdir("public") do - @site_locales = Site.last.in_use_locales.each do |locale| - I18n.with_locale(locale) do - @file_tmp = File.new("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt", "w") - tmp_table_texts = create_table(locale) - @file_tmp.write(tmp_table_texts) - @file_tmp.close - end - end + File.open("public/cancerfield_back#{idx}.txt", "w") do |f| + f.write(@form_to_show.attributes) end - Dir.chdir("public") do - @file_back = File.open("cancerfield_back" + @index.to_s + ".txt", "w") - @file_back.write(@form_to_show.attributes) - @file_back.close - @file_org = File.open("cancerfield_org" + @index.to_s + ".txt", "w") - @file_org.write(Cancerpredictfields.where("title" => @app_title + "_back").first.attributes) - @file_org.close + File.open("public/cancerfield_org#{idx}.txt", "w") do |f| + f.write(Cancerpredictfields.where("title" => @app_title + "_back").first.attributes) end redirect_to admin_cancerpredicts_path end @@ -637,12 +631,10 @@ class Admin::CancerpredictsController < OrbitAdminController #FileUtils.rm_r(dir_path, :force => true) if Dir.exist?(dir_path) FileUtils.mkdir dir_path if !Dir.exist?(dir_path) #create dir for storing tmp_file if dir doesn't exist @filename = ("#{Time.now.year}_%02s_%02s_export_cancer_tool_record.xlsx" % [Time.now.month, Time.now.day]).gsub(" ", "0") - Dir.chdir(dir_path) do - File.open(@filename, "w") do |f| - f.write render_to_string(:handlers => [:axlsx], :formats => [:xlsx], :partial => "export_cancer_tool_record.xlsx", :locals => { :results => cancer_records }) - end + File.open("#{dir_path}#{@filename}", "w") do |f| + f.write render_to_string(:handlers => [:axlsx], :formats => [:xlsx], :partial => "export_cancer_tool_record.xlsx", :locals => { :results => cancer_records }) end - tmp_filename_data = File.read(dir_path + @filename) + tmp_filename_data = File.read("#{dir_path}#{@filename}") send_data(tmp_filename_data, type: "application/xlsx", disposition: "attachment", filename: @filename) end @@ -658,16 +650,12 @@ class Admin::CancerpredictsController < OrbitAdminController @form_to_show.title = @app_title @form_to_show.save @form_to_show = Cancerpredictfields.where("title" => @app_title).first - Dir.chdir("public") do - @site_locales = Site.last.in_use_locales.each do |locale| - I18n.with_locale(locale) do - @file_tmp = File.new("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt", "w") - tmp_table_texts = create_table(locale) - @file_tmp.write(tmp_table_texts) - @file_tmp.close - end - end - end + @site_locales = Site.last.in_use_locales.each do |locale| + File.open(Cancerpredictfields::ToolTableMap[I18n.locale], "w") do |f| + tmp_table_texts = create_table(locale) + f.write(tmp_table_texts) + end + end else @form_to_show = Cancerpredictfields.where("title" => @app_title).first end diff --git a/app/controllers/cancerpredicts_controller.rb b/app/controllers/cancerpredicts_controller.rb index c443187..20eda75 100644 --- a/app/controllers/cancerpredicts_controller.rb +++ b/app/controllers/cancerpredicts_controller.rb @@ -150,11 +150,11 @@ class CancerpredictsController < ApplicationController uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] - @table_str = File.read("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt") - preidct_js_url = "/assets/cancer_predict.js" + @table_str = File.read(Cancerpredictfields::ToolTableMap[I18n.locale]) + preidct_js_url = "/assets/#{Cancerpredictfields::JS}" if File.exist?(Cancerpredictfields::JSFileName) js_filename = File.read(Cancerpredictfields::JSFileName) - if js_filename.include?("cancer_predict.js") + if js_filename.include?(Cancerpredictfields::JS) asset = Rails.application.assets[js_filename] preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}" else @@ -171,11 +171,11 @@ class CancerpredictsController < ApplicationController uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] - @table_str = File.read("#{Cancerpredictfields::ToolTablePrefix}#{I18n.locale}.txt") - preidct_js_url = "/assets/cancer_predict.js" + @table_str = File.read(Cancerpredictfields::ToolTableMap[I18n.locale]) + preidct_js_url = "/assets/#{Cancerpredictfields::JS}" if File.exist?(Cancerpredictfields::JSFileName) js_filename = File.read(Cancerpredictfields::JSFileName) - if js_filename.include?("cancer_predict.js") + if js_filename.include?(Cancerpredictfields::JS) asset = Rails.application.assets[js_filename] preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}" else diff --git a/app/models/cancerpredictfields.rb b/app/models/cancerpredictfields.rb index 915962c..c9e109b 100644 --- a/app/models/cancerpredictfields.rb +++ b/app/models/cancerpredictfields.rb @@ -10,7 +10,11 @@ class Cancerpredictfields TherapyFields = ["variable","name","hint","comment_text","choice_fields","lpv_impact","active_choice","disable_condition"] TherapyOnly = ["lpv_impact","active_choice","disable_condition"] JSFileName = "public/cancer_tool_js_filename.txt".freeze - ToolTablePrefix = "public/cancer_tool_table_tmp_".freeze + ModuleAppPath = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s.freeze + JS = "cancer_predict.js" + ToolTableMap = I18n.available_locales.map do |locale| + [locale, "public/cancer_tool_table_tmp_#{locale}.txt".freeze] + end.to_h field :title ,type:String ,default:"" field :advance_mode, type: Boolean, default: false field :form_show , :type=> Hash ,default:{ @@ -419,8 +423,7 @@ class Cancerpredictfields end def auto_write_predict_js(force_reload=true) js_codes = generate_jscode - module_app_path = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s - save_path = module_app_path + '/app/assets/javascripts/cancer_predict.js' + save_path = "#{ModuleAppPath}/app/assets/javascripts/#{JS}" file_texts = File.read(save_path) need_write = false str1 = "/* auto add start */"