Why isn't this working?


#1

This is a very straightforward problem…

-I included a mixin in lib/ (see below)
-In environment.rb, I included the require statement (require
‘acts_as_tree_additions’)
-Defined a method in the model:

def category_list
format_as_list #method in acts_as_tree_additions
end

-Called that method in the view…

I know the method is in the correct use, because it prints out a string
if I replace ‘format_as_list’ with a string in category_list. However,
it’s not working as expected for the mixin.

Another thing is that this is a shared server, and does not need a
restart of the server (which couldn’t be done either way with their port
blocking). ‘require’ code works for file_column without a restart, but
not this… Am I doing something wrong?

–Thanks in advance,
flamesrock

(by the way… for some reason, I login successfully, but my session
doesn’t work… so I’m posting as a guest)

acts_as_tree_additions.rb

##credited to argv[] in #rubyonrails

module ActiveRecord::Acts::Tree::ClassMethods
@@TreeListView_tree = nil

def Object.format_as_list(options = {}, pid = nil, level = 0, &block)
@@TreeListView_tree ||= self.find(:all, options)
t = Array.new
return t if @@TreeListView_tree.nil?
list = @@TreeListView_tree.find_all { |e| e.parent_id == pid }.sort!
list.each do |e|
array_of_e = (block.nil? ? e : yield(e, level))
t.size == 0 ? t = [array_of_e] : t << array_of_e
t += format_as_list(options, e.id, level + 1, &block)
end
t
end

def Object.traverse_tree(options = {}, pid = nil, level = 0, &block)
@@TreeListView_tree ||= self.find(:all, options)
list = @@TreeListView_tree.find_all { |e| e.parent_id == pid }.sort!
list.each do |e|
yield(e, level)
traverse_tree(options, e.id, level + 1, &block)
end
end

def Object.format_as_select_options(display_field = :name, value_field
= :id)
format_as_list do |e, level|
["-" * level + " " + (e[display_field] || “”), e[value_field]]
end
end

end