diff --git a/app/controllers/cancerpredicts_controller.rb b/app/controllers/cancerpredicts_controller.rb index d4fdac0..8b53cba 100644 --- a/app/controllers/cancerpredicts_controller.rb +++ b/app/controllers/cancerpredicts_controller.rb @@ -148,9 +148,19 @@ class CancerpredictsController < ApplicationController tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] @table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt') + preidct_js_url = '/assets/cancer_predict.js' + if File.exist?('public/cancer_tool_js_filename.txt') + js_filename = File.read('public/cancer_tool_js_filename.txt') + if js_filename.match(/cancer_predict\.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 { "cancerpredict" => [], - "extras"=>{"table"=> @table_str} + "extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url} } end def widget @@ -158,9 +168,19 @@ class CancerpredictsController < ApplicationController tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] @table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt') + preidct_js_url = '/assets/cancer_predict.js' + if File.exist?('public/cancer_tool_js_filename.txt') + js_filename = File.read('public/cancer_tool_js_filename.txt') + if js_filename.match(/cancer_predict\.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 { "cancerpredict" => [], - "extras"=>{"table"=> @table_str} + "extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url} } end def create_first_field diff --git a/app/models/cancerpredictfields.rb b/app/models/cancerpredictfields.rb index 8d6e75b..683f75e 100644 --- a/app/models/cancerpredictfields.rb +++ b/app/models/cancerpredictfields.rb @@ -269,6 +269,50 @@ class Cancerpredictfields 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("#{Rails.root}/public/cancer_tool_js_filename.txt",'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" CancerpredictsController.module_eval(eval_hidden_variables) @@ -394,7 +438,7 @@ class Cancerpredictfields 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/cancer_predict.js' @@ -460,6 +504,9 @@ class Cancerpredictfields end if need_write File.write(save_path,file_texts) + reload_js_asset(save_path, force_reload) + elsif !(File.exist?("#{Rails.root}/public/cancer_tool_js_filename.txt")) + File.open("#{Rails.root}/public/cancer_tool_js_filename.txt",'w+'){|f| f.write(save_path)} end end def get_years_settings_dict diff --git a/modules/cancerpredict/_cancer_predict_widget.html.erb b/modules/cancerpredict/_cancer_predict_widget.html.erb index 5fd837b..65d9be2 100644 --- a/modules/cancerpredict/_cancer_predict_widget.html.erb +++ b/modules/cancerpredict/_cancer_predict_widget.html.erb @@ -1,6 +1,6 @@ - + {{table}} <%= stylesheet_link_tag "cancer_predict", media: "screen" %> <%= stylesheet_link_tag "bootstrap.min.print", media: "print" %> diff --git a/modules/cancerpredict/cancer_predict_index.html.erb b/modules/cancerpredict/cancer_predict_index.html.erb index 5fd837b..65d9be2 100644 --- a/modules/cancerpredict/cancer_predict_index.html.erb +++ b/modules/cancerpredict/cancer_predict_index.html.erb @@ -1,6 +1,6 @@ - + {{table}} <%= stylesheet_link_tag "cancer_predict", media: "screen" %> <%= stylesheet_link_tag "bootstrap.min.print", media: "print" %>