Passing Arrays in recursive functions

ok, I’m still new to ruby and new to rails. what I have is a task
database that acts_as_tree a task can have a children an those
children are full tasks so they can have children of their own, Tasks
can only be completed if they have no children, a task with children
is only completed if all its children are done. this much I have
working, I’m trying to add some Ajax support for toggling the done/
not_done flag on the view but I’m hitting a problem and I’m not sure
how to get around it.
[model]
def completed?
self.siblings.all? { |x| x.done } if self.parent
end

[controller]
def task_done
@task = Task.find(params[:id])
is_complete = !@task.done
ids_changed = task_complete!(@task, is_complete)
words = is_complete ? " " : " Not "
flash[:notice] = “Task is#{words}finished”
if request.xhr? then
render :update do |page|
ids_changed.each {|x|
page.toggle “td_#{x}”
page.toggle “menu_#{x}_done”
}
end
else
redirect_to_index
end
end

def task_complete!(taskid, is_complete=true, ids_changed = [])
@task = Task.find(taskid)
val = is_complete ? 1 : 0
@task.update_attribute(:done, val)
ids_changed.insert(-1,@task.id)
if @task.completed?
task_complete!(@task.parent, is_complete, ids_changed)
end
end

what I have thought about is changing task_complete! to pass an array
back an forth but am unsure how to get each call to insert a new
element into the array and since this happens in a place I don’t know
how to hack away at I’m at a bit of a loss. any suggestions/advice is
appreciated.

found the solution on my own, I uh forgot to return the array after
the call

def task_done
@task = Task.find(params[:id])
is_complete = !@task.done
ids_changed = task_complete!(@task, is_complete)
words = is_complete ? " " : " Not "
flash[:notice] = “Task is#{words}finished”
if request.xhr? then
render :update do |page|
ids_changed.each {|x|
page.toggle “td_#{x}”
}
page.toggle “menu_#{params[:id]}_done”
end
else
redirect_to_index
end
end

def task_complete!(taskid, is_complete=true, ids_changed = [])
@task = Task.find(taskid)
val = is_complete ? 1 : 0
@task.update_attribute(:done, val)
ids_changed.insert(-1,@task.id)
if @task.completed?
task_complete!(@task.parent, is_complete, ids_changed)
end
ids_changed
end

On Feb 13, 12:34 pm, “[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs