Reputation: 163
I have a validate_completion method defined in my task.rb model. The Pseudo logic should be
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
The code looks like as follows.
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
end
As is right now if no hazards exist, it successfully sets the date to today. And if I then add a hazard it effectively switches the date back to nil. However, If one hazard exists and it has a risk_total below 1000 the date remains nil.
Upvotes: 0
Views: 100
Reputation: 8257
"Well, given that your hazards are statically defined and not in the database, you could do something like this:
#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
You can get by with this logic, but it might be significantly easier to make your hazards a database table and use ActiveRecord associations and queries to simplify this.
Upvotes: 1