diff --git a/app/controllers/admin/cancerpredicts_controller.rb b/app/controllers/admin/cancerpredicts_controller.rb index a6169eb..c459150 100644 --- a/app/controllers/admin/cancerpredicts_controller.rb +++ b/app/controllers/admin/cancerpredicts_controller.rb @@ -413,7 +413,7 @@ class Admin::CancerpredictsController < OrbitAdminController @field_property["comment_text"] = property["comment_text"][locale.to_s] break end - if @field_property["comment_text"] != "" + if @field_property["comment_text"].present? @table_str_right += '' @table_str_right += '' else diff --git a/app/controllers/cancerpredicts_controller.rb b/app/controllers/cancerpredicts_controller.rb index 1999908..de3ff70 100644 --- a/app/controllers/cancerpredicts_controller.rb +++ b/app/controllers/cancerpredicts_controller.rb @@ -1,5 +1,6 @@ # encoding: utf-8 require 'rubyXL' +require "json" class CancerpredictsController < ApplicationController def initialize super @@ -32,7 +33,7 @@ class CancerpredictsController < ApplicationController result['page_title'] = @form_to_show.title_texts[params[:locale]] elsif params['get_mapping_data_from_csv'].to_i == 1 result = {} - result['mapping_data_from_csv'] = YAML.load(@form_to_show.mapping_data_from_csv) rescue {} + result['mapping_data_from_csv'] = JSON.parse(@form_to_show.mapping_data_from_csv) rescue {} else @record = Cancerpredictrecord.new @record.title = @app_title @@ -59,7 +60,7 @@ class CancerpredictsController < ApplicationController # else # result['cal_value'] = 1 # end - mapping_data_from_csv = YAML.load(@form_to_show.mapping_data_from_csv) rescue {} + mapping_data_from_csv = JSON.parse(@form_to_show.mapping_data_from_csv) rescue {} @form_to_show.form_show.each do |num,property| @variable = property[:variable] if @variable.present? @@ -102,13 +103,17 @@ class CancerpredictsController < ApplicationController end end #result['lpv'] = -0.51427548* (result['sex_value']- 0.508312) + 0.05764604* (result['Age_value'] - 61.894501) + 0.49138819*(result['cal_value'] - 0.334399) rescue 'error' - formula = @form_to_show.tmp_lpv_ruby_code - formula_variables = @form_to_show.tmp_lpv_variables_ruby_code - eval(formula) - result['lpv'] = eval(formula_variables.last) + formula_variables = @form_to_show.tmp_lpv_variables + begin + eval_formula(result) + rescue => e + @form_to_show.generate_eval_formula + eval_formula(result) + end + result['lpv'] = instance_variable_get(formula_variables.last) result['lpv_variable'] = {} formula_variables.each do |variable_name| - eval( "result['lpv_variable']['#{variable_name}'] = #{variable_name}" ) + result['lpv_variable']["#{variable_name[1..-1]}"] = instance_variable_get(variable_name) end @years = @form_to_show.years result['table'] = '

'+@form_to_show.table_above_texts[locale].to_s+'

' @@ -141,7 +146,7 @@ class CancerpredictsController < ApplicationController year = year.to_f end year_index = @years.index(year) - @servive_ratio = eval(@form_to_show.tmp_years_settings[year_index]) + @servive_ratio = eval(@form_to_show.tmp_years_settings_for_ruby[year_index]) @servive_ratio = ((1 - @servive_ratio) * 100).round(2) @texts = @form_to_show.text_above_texts[locale].to_s.gsub('
','
').gsub('{{Surgery_only}}',''+@servive_ratio.round(2).to_s+'') @texts = @texts.split('{{years}}') diff --git a/app/models/cancerpredictfields.rb b/app/models/cancerpredictfields.rb index 09e0e37..0a3e8a2 100644 --- a/app/models/cancerpredictfields.rb +++ b/app/models/cancerpredictfields.rb @@ -95,9 +95,10 @@ class Cancerpredictfields +0.2002546*(O9-0.06176471)" field :years_settings , type: Array , default: ["0.8095037^( exp(A) )","0.729158^( exp(A) )","0.6717211^( exp(A) )","0.6056773^( exp(A) )"] field :tmp_years_settings , type: Array , default: [] + field :tmp_years_settings_for_ruby , type: Array , default: [] field :lpv_calc, type: Hash, default: {} #for js code field :tmp_lpv_ruby_code, type: String, default: "" - field :tmp_lpv_variables_ruby_code, type: Array, default: [] + field :tmp_lpv_variables, type: Array, default: [] field :mapping_data_from_csv , type: String ,default: "" scope :can_display, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}).order_by([:is_top, :desc],[:postdate, :desc])} scope :is_approved, ->{where(:approved => true)} @@ -121,15 +122,33 @@ class Cancerpredictfields end formula = self.prediction_formula.gsub("\r\n"," ").gsub("^","**") result_keys.each do |k| - formula = formula.gsub(/#{k}?(-|\+|\*|\s|\=)/){ "result[\"#{k}\"]#{$1}" } + formula = formula.gsub(/#{k}?(-|\+|\*|\/|\s|\=)/){ "result[\"#{k}\"]#{$1}" } end + formula = formula.split(/^([^=]+)=([^=])/).select{|s| s.present?}.each_slice(2).map do |a,b| + if b + ("@"+ a + "=" + b) + else + "@"+ a + end + end.join("\n") self.tmp_lpv_ruby_code = formula - formula_variables = formula.enum_for(:scan,/([^\=]*)?=/).map { ::Regexp.last_match[1] }.map{|s| s.strip.split(/(\s*|;\r\n)/).last} - self.tmp_lpv_variables_ruby_code = formula_variables + formula_variables = formula.enum_for(:scan,/([^\=\(\)]+)?=[^=]/).map {|x| x[-1] }.compact.map{|s| s.strip} + self.tmp_lpv_variables = formula_variables self.tmp_years_settings = self.years_settings.map do |s| s.gsub('^','**').gsub('exp','Math.exp') end + self.tmp_years_settings_for_ruby = self.tmp_years_settings.clone + formula_variables.each do |variable_name| + self.tmp_years_settings_for_ruby = self.tmp_years_settings_for_ruby.map do |y| + y.gsub(variable_name[1..-1],variable_name) + end + end self.lpv_calc = get_years_settings_dict + self.generate_eval_formula + end + def generate_eval_formula + eval_formula = "def eval_formula(result); #{self.tmp_lpv_ruby_code}; end" + CancerpredictsController.module_eval(eval_formula) end def update_user User.find(update_user_id) rescue nil