Using acts_as_tree i created a hierarchical structure called
category.
class Category < ActiveRecord::Base
fields do
name :string
parent_id :integer
timestamps
end
acts_as_tree :order => :full_name
belongs_to :parent, :class_name => "Category"
has_many :children, :class_name => "Category", :foreign_key =>
"parent_id", :dependent => :destroy
has_many :tasks
children :tasks
# created to show the full name
def full_name
if parent.nil?
self[:name]
else
old_name = self[:name]
parent.to_s + "::" + old_name.to_s
end
end
....
end
To provide the appropriate selection drop down box I used the
following
<extend tag="form" for="Task">
<old-form merge>
<field-list: fields="name,categories">
<category-view:>
<category-select-tree cat="&this"/>
</category-view:>
</field-list:>
</old-form>
</extend>
the category-select-tree tag is defined as follows (i am reusing some
code that was included in the acts_as_tree receipie)
<def tag="category-select-tree" attrs="cat">
<%
def tree_select(acts_as_tree_set, init=true, &block)
if acts_as_tree_set.size > 0
acts_as_tree_set.collect do |item|
next if item.parent_id && init
yield item
tree_select(item.children, false, &block) if
item.children.size > 0
end
end
end
def emit_link(my_item,cat)
if !cat.nil? && cat.id==my_item.id then
%><option selected="yes" value="#{my_item.id}>"><%=
my_item.full_name %></option> <%
else
%><option value="#{my_item.id}>"><%= my_item.full_name
%></option> <%
end
end
%>
<select name="task[category_id]">
<%
@categories = Category.find(:all)
tree_select(@categories, true) {|item| emit_link(item,cat) } %>
</select>
</def>
Couple of Questions,
1. It works when task has only one category but doesn't work when
tasks have more than one categories. Would be really helpful to get
any guidance on how to make it work when tasks could have more than
one categories.
2. Is there a better way of redfining the select drop down box.
Currently, I have hardcoded the usage --- task[category_id] is
configured in, any way to dynamically make it so whether i use this
for tasks or for anything else it would be generic.
3. Any other better ways of solving this problem?
Thanks Again
Krish
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.