lance.sanchez-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-13 19:34 UTC
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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
lance.sanchez-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-23 01:39 UTC
Re: Passing Arrays in recursive functions.
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,
"lance.sanc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<lance.sanc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---