0

私の task.rb モデルで定義された validate_completion メソッドがあります。疑似ロジックは

if no hazards exist
  date_completed = Date.today
elsif any hazard exists with a risk_total > 1000
  date_completed = nil
else any hazard that exists has a risk_total =< 1000 
  date_complete = Date.today
end

コードは次のようになります。

    def validate_completion
if self.date_completed.nil?
  if self.Biohaz_exist == "No"
    if self.Hazmat_exist == "No"
      if self.Fire_exist == "No"
        if self.Hazengy_exist == "No"
          if self.Tool_exist == "No"
            if self.Rad_exist == "No"
              if self.Laz_exist == "No"
                if self.Mag_exist == "No"
                  if self.Ergo_exist == "No"
                    if self.Mechand_exist == "No"
                      if self.Road_exist == "No"
                        if self.Fall_exist == "No"
                          if self.Hazatm_exist == "No"
                            if self.Noise_exist == "No"
                              if self.Ovrhead_exist == "No"
                                if self.Cut_exist == "No"
                                  if self.Temp_exist == "No"
                                    if self.Access_exist == "No"
                                      if self.Cowrkr_exist == "No"
                                        if self.Lonewrkr_exist == "No"
                                          self.date_completed = Date.today
                                        elsif self.Biohaz_exist == "Yes" and self.Biohaz.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Hazmat_exist == "Yes" and self.Hazmat.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Fire_exist == "Yes" and self.Fire.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Hazengy_exist == "Yes" and self.Hazengy.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Tool_exist == "Yes" and self.Tool.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Rad_exist == "Yes" and self.Rad.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Laz_exist == "Yes" and self.Laz.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Mag_exist == "Yes" and self.Mag.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Ergo_exist == "Yes" and self.Ergo.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Mechand_exist == "Yes" and self.Ergo.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Road_exist == "Yes" and self.Ergo.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Fall_exist == "Yes" and self.Fall.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Hazatm_exist == "Yes" and self.Hazatm.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Noise_exist == "Yes" and self.Noise.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Ovrhead_exist == "Yes" and self.Ovrhead.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Cut_exist == "Yes" and self.Cut.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Temp_exist == "Yes" and self.Temp.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Access_exist == "Yes" and self.Access.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Cowrkr_exist == "Yes" and self.Cowrk.risk_total > 1000
                                          self.date_completed = nil
                                        elsif self.Lonewrkr_exist == "Yes" and self.Lonewrkr.risk_total > 1000
                                          self.date_completed = nil                                                                                  
                                        else
                                          self.date_completed = Date.today  
                                        end
                                      end
                                    end
                                  end
                                end
                              end
                            end
                          end
                        end
                      end
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end

終わり

現在と同様に、危険がなければ、日付を今日に設定することに成功しています。そして、ハザードを追加すると、日付が事実上 nil に戻ります。ただし、1 つのハザードが存在し、その risk_total が 1000 未満の場合、日付は nil のままです。

4

1 に答える 1

1

「まあ、ハザードが静的に定義されており、データベースにないことを考えると、次のようなことができます。

#definition of all the hazard
hazards = [:Fire, :Road, :Fall]
def validate_completion
  # find all the hazards that exist
  exists = hazards.select { |hazard| self.send("#{hazard.to_s}_exist") } 
  # find if at least one high risk hazard exists
  high_risk_total = exists.detect {|hazard| self.send("#{hazard.to_s}_risk_total") > 1000 }
  # find if at least one low rick hazard exists
  low_risk_total = exists.detect {|hazard| self.send("#{hazard.to_s}_risk_total") <= 1000 }

  # validation logic here
end

このロジックでうまくいくことができますが、ハザードをデータベーステーブルにし、ActiveRecordの関連付けとクエリを使用してこれを単純化する方がはるかに簡単な場合があります。

于 2012-12-04T18:03:20.477 に答える