diff --git a/app/controllers/admin/coloncancerpredict1s_controller.rb b/app/controllers/admin/coloncancerpredict1s_controller.rb index 9c8d3e2..9284a69 100644 --- a/app/controllers/admin/coloncancerpredict1s_controller.rb +++ b/app/controllers/admin/coloncancerpredict1s_controller.rb @@ -230,10 +230,6 @@ class Admin::Coloncancerpredict1sController < OrbitAdminController end @size = ['small','medium','large'] @size.each{|size| @form_to_show[size] = params["coloncancerpredictfields1s"][size].to_h} - @file_path = Rails.root.to_s + '/app/assets/images/predict_tool' - if !Dir.exist? @file_path - FileUtils.mkdir_p @file_path - end @images = params["coloncancerpredictfields1s"]['head_images'] @head_images_id = @form_to_show.head_images_id @delete_index = [] @@ -321,29 +317,23 @@ class Admin::Coloncancerpredict1sController < OrbitAdminController @form_to_show.auto_write_predict_js end end - @index = 0 - Dir.chdir("public") do - while File.exist?('colon_cancerfield_back'+@index.to_s+'1.txt') - @index += 1 - end - end - Dir.chdir("public") do - @site_locales = Site.last.in_use_locales.each do |locale| - I18n.with_locale(locale) do - @file_tmp = File.new('colon_cancer_tool_table_tmp_'+locale.to_s+'1.txt', 'w') + @site_locales = Site.last.in_use_locales.each do |locale| + I18n.with_locale(locale) do + File.open(Coloncancerpredictfields1s::ToolTableMap[I18n.locale], 'w') do |f| tmp_table_texts = create_table(locale) - @file_tmp.write(tmp_table_texts) - @file_tmp.close + f.write(tmp_table_texts) end end end - Dir.chdir("public") do - @file_back = File.open('colon_cancerfield_back'+@index.to_s+'1.txt', 'w') - @file_back.write(@form_to_show.attributes) - @file_back.close - @file_org = File.open('colon_cancerfield_org'+@index.to_s+'1.txt', 'w') - @file_org.write(Coloncancerpredictfields1s.where("title"=>@app_title+'_back').first.attributes) - @file_org.close + idx = 0 + while File.exist?("public/colon_cancerfield1_back#{idx}.txt") + idx += 1 + end + File.open("public/colon_cancerfield1_back#{idx}.txt", 'w') do |f| + f.write(@form_to_show.attributes) + end + File.open("public/colon_cancerfield1_org#{idx}.txt", 'w') do |f| + f.write(Coloncancerpredictfields1s.where("title"=>@app_title+'_back').first.attributes) end redirect_to admin_coloncancerpredict1s_path end @@ -631,12 +621,10 @@ class Admin::Coloncancerpredict1sController < 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 def create_first_field @@ -651,13 +639,11 @@ class Admin::Coloncancerpredict1sController < OrbitAdminController @form_to_show.title=@app_title @form_to_show.save @form_to_show = Coloncancerpredictfields1s.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('colon_cancer_tool_table_tmp_'+locale.to_s+'1.txt', 'w') + @site_locales = Site.last.in_use_locales.each do |locale| + I18n.with_locale(locale) do + File.open(Coloncancerpredictfields1s::ToolTableMap[I18n.locale], 'w') do |f| tmp_table_texts = create_table(locale) - @file_tmp.write(tmp_table_texts) - @file_tmp.close + f.write(tmp_table_texts) end end end diff --git a/app/controllers/coloncancerpredict1s_controller.rb b/app/controllers/coloncancerpredict1s_controller.rb index 332fb32..f77ce9b 100644 --- a/app/controllers/coloncancerpredict1s_controller.rb +++ b/app/controllers/coloncancerpredict1s_controller.rb @@ -147,20 +147,40 @@ class Coloncancerpredict1sController < ApplicationController uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] - @table_str = File.read('public/colon_cancer_tool_table_tmp_'+I18n.locale.to_s+'1.txt') + @table_str = File.read(Coloncancerpredictfields1s::ToolTableMap[I18n.locale]) + preidct_js_url = "/assets/#{Coloncancerpredictfields1s::JS}" + if File.exist?(Coloncancerpredictfields1s::JSFileName) + js_filename = File.read(Coloncancerpredictfields1s::JSFileName) + if js_filename.include?(Coloncancerpredictfields1s::JS) + asset = Rails.application.assets[js_filename] + preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}" + else + preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}" + end + end { "coloncancerpredict1" => [], - "extras"=>{"table"=> @table_str} + "extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url} } end def widget uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] - @table_str = File.read('public/colon_cancer_tool_table_tmp_'+I18n.locale.to_s+'1.txt') + @table_str = File.read(Coloncancerpredictfields1s::ToolTableMap[I18n.locale]) + preidct_js_url = "/assets/#{Coloncancerpredictfields1s::JS}" + if File.exist?(Coloncancerpredictfields1s::JSFileName) + js_filename = File.read(Coloncancerpredictfields1s::JSFileName) + if js_filename.include?(Coloncancerpredictfields1s::JS) + asset = Rails.application.assets[js_filename] + preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}" + else + preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}" + end + end { "coloncancerpredict1" => [], - "extras"=>{"table"=> @table_str} + "extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url} } end def create_first_field diff --git a/app/models/coloncancerpredictfields1s.rb b/app/models/coloncancerpredictfields1s.rb index d0dc267..72fc648 100644 --- a/app/models/coloncancerpredictfields1s.rb +++ b/app/models/coloncancerpredictfields1s.rb @@ -9,6 +9,12 @@ class Coloncancerpredictfields1s AdvanceFields = ["revert_value","map_values","coloncancer_predict_mapping_file1"] TherapyFields = ["variable","name","hint","comment_text","choice_fields","lpv_impact","active_choice","disable_condition"] TherapyOnly = ["lpv_impact","active_choice","disable_condition"] + ModuleAppPath = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s.freeze + JS = "colon_cancer_predict1.js" + JSFileName = "public/cc1_tool_js_filename.txt" + ToolTableMap = I18n.available_locales.map do |locale| + [locale, "public/colon_cancer_tool_table_tmp_#{locale}1.txt".freeze] + end.to_h field :title ,type:String ,default:"" field :advance_mode, type: Boolean, default: false field :form_show , :type=> Hash ,default: { @@ -214,6 +220,50 @@ class Coloncancerpredictfields1s self.lpv_calc = get_years_settings_dict self.generate_eval_formula end + def reload_any_asset(path, type=nil, force_reload=false) + env = Rails.application.assets + new_env = Sprockets::Environment.new(Rails.root.to_s) do |env| + env.version = ::Rails.env + tmp_path = "#{Rails.application.config.root}/tmp/cache/assets/#{::Rails.env}" + env.cache = ::Sprockets::Cache::FileStore.new(tmp_path) + env.context_class.class_eval do + include ::Sprockets::Rails::Helper + end + end + new_env.config = env.config + stats = env.cached.instance_variable_get(:@stats) + new_path = path.sub(/\.erb$/,'') + if force_reload || (stats && stats[path]) #Need reload asset + if type.nil? + ext = File.extname(new_path) + env.mime_types.each do |t, h| + if h[:extensions].include?(ext) + type = t + break + end + end + end + if type + full_path = 'file://'+path+'?type='+type + uris = env.cached.instance_variable_get(:@uris) + keys = uris.keys.select{|k| k.include?(full_path)} + asset = new_env.load(full_path) + if keys.count != 0 + keys.each do |k| + uris[k] = asset + end + end + new_path = new_path.sub(/\.[^.]+$/){|ext| '-' + asset.digest + ext} + File.binwrite(new_path, asset.to_s) + yield new_path if block_given? + end + end + end + def reload_js_asset(path, force_reload=false) + reload_any_asset(path, 'application/javascript', force_reload) do |new_path| + File.open(JSFileName, 'w+'){|f| f.write(new_path)} + end + end def generate_eval_formula eval_hidden_variables = "def eval_hidden_variables(result); #{self.tmp_hidden_variables_for_ruby}; end" Coloncancerpredict1sController.module_eval(eval_hidden_variables) @@ -339,10 +389,9 @@ class Coloncancerpredictfields1s org_str.insert(st, replace_str) org_str end - def auto_write_predict_js + 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/colon_cancer_predict1.js' + save_path = "#{ModuleAppPath}/app/assets/javascripts/#{JS}" file_texts = File.read(save_path) need_write = false str1 = "/* auto add start */" @@ -405,6 +454,9 @@ class Coloncancerpredictfields1s end if need_write File.write(save_path,file_texts) + reload_js_asset(save_path, force_reload) + elsif !(File.exist?(JSFileName)) + File.open(JSFileName, 'w+'){|f| f.write(save_path)} end end def get_years_settings_dict diff --git a/modules/coloncancerpredict1/_colon_cancer_predict1_widget.html.erb b/modules/coloncancerpredict1/_colon_cancer_predict1_widget.html.erb index 330f77f..c7e5653 100644 --- a/modules/coloncancerpredict1/_colon_cancer_predict1_widget.html.erb +++ b/modules/coloncancerpredict1/_colon_cancer_predict1_widget.html.erb @@ -1,6 +1,6 @@ - + {{table}} <%= stylesheet_link_tag "colon_cancer_predict1", media: "screen" %> <%= stylesheet_link_tag "bootstrap.min.print", media: "print" %> diff --git a/modules/coloncancerpredict1/colon_cancer_predict1_index.html.erb b/modules/coloncancerpredict1/colon_cancer_predict1_index.html.erb index 330f77f..c7e5653 100644 --- a/modules/coloncancerpredict1/colon_cancer_predict1_index.html.erb +++ b/modules/coloncancerpredict1/colon_cancer_predict1_index.html.erb @@ -1,6 +1,6 @@ - + {{table}} <%= stylesheet_link_tag "colon_cancer_predict1", media: "screen" %> <%= stylesheet_link_tag "bootstrap.min.print", media: "print" %>