-
Notifications
You must be signed in to change notification settings - Fork 0
Relevant Code
Randy Russell edited this page Apr 17, 2018
·
1 revision
I added a jobs model to a current website, the jobs scaffold is all done for this code challenge. app/controllers/jobs_controller.rb
## code added to the scaffolded controller.
def index
@jobs = Job.all
@jobs_with_dependancies = find_jobs_with_dependancies(@jobs)
end
private
def find_jobs_with_dependancies(jobs)
collection = []
jobs.each do |job|
if job.dependant.present?
collection.push(job)
end
end
return collection
end
Results window calls sort_jobs_by_dependancies helper method with @jobs and @jobs_with_dependancies from the controller. views/jobs/index.html.erb
<h1>Result</h2>
<h3><%= sort_jobs_by_dependancies(@jobs, @jobs_with_dependancies) %></h3>
app/helpers/job_helper.rb See comments.
#
# This handles an empty database, if there is only one object and, if there are several
# with no dependancies, then calls dep helper to handle sorting a collection
# with dependancies, and prints that result and removes duplicates with
# .uniq().
##
def sort_jobs_by_dependancies(jobs, jobs_with_dependancies)
if jobs.empty?
'empty'
elsif jobs.count == 1
jobs[0].name
elsif jobs_with_dependancies.empty?
jobs.map(&:name).join ', '
elsif jobs_with_dependancies.present?
dep_helper(jobs, jobs_with_dependancies, result = [])
result.map(&:name).uniq.join ', '
end
end
#
# Loops jobs with dependencies gets the dependancy if its dep isn't blank,
# pushes it and then, adds the whole array again.
##
def dep_helper(jobs, jobs_with_dependancies, result)
jobs_with_dependancies.each do |job|
dep = job.dependant
find_dep = Job.find(dep)
if find_dep.dependant.blank?
result.push find_dep
result.push job
end
end
jobs.each do |non_dependant_job|
result << non_dependant_job
end
end
app/models/job.rb
class Job < ApplicationRecord
validate :can_not_dep_on_self, on: :update
validate :jobs_cant_have_circular_dependencies, on: :update
#
# Custom validations handle issue returns,
##
def can_not_dep_on_self
return unless id == dependant
errors.add(:dependant, "Jobs can’t depend on themselves")
end
#
# Returns unless a dependant is present.
# Starts the collection array with an array of the id and dependant id.
# Sends the collection to the dependant_factory_method.
# Takes the returned collection and checks to see if there is a circle happ.
##
def jobs_cant_have_circular_dependencies
return unless dependant
coll = [] << [id, dependant]
dependant_factory(coll)
if coll[0][0] == coll.last[0]
return errors.add(:dependant, "No circular dependancies
#{coll} #{coll[0][0]} self.dependant =
#{self.dependant} self.id=#{self.id}")
end
end
#
# Returns unless there is a value in the last position of the last array. (dep
# id). Put's its object in a variable and adds an array of with id and dep id.
# Then calls itself where it will check to see if a nil has arrived yet.
##
def dependant_factory(coll)
return unless coll.last[1]
dep = Job.find(coll.last[1])
coll << [dep.id, dep.dependant]
dependant_factory(coll)
end
end