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

结尾

就像现在一样,如果不存在危险,它会成功地将日期设置为今天。如果我再添加一个危险,它会有效地将日期切换回零。但是,如果存在一种危害并且它的 risk_total 低于 1000,则日期保持为零。

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 回答