I've made a mistake. To fix this, I think we should add one more
method to application.rb and change the code for accessing current
model. See notes below:
On Jun 27, 2007, at 1:48 PM, Jordan A. Fowler wrote:
Hi Leon,
So, you're trying to consolidate code from two helpers into a
single helper that will work with any model you specify. Here's
what I would do:
In app/controller/application.rb make something similar to this:
protected
def implements_tree_for(klass)
Change line below to:
(@tree_classes ||= {})[EMAIL PROTECTED] = klass
(@tree_classes ||= {})[self.class] = klass
end
Add another method here:
def tree_model
@[EMAIL PROTECTED]
end
Now, place at the top of your controller (MarketplaceController,
etc.):
implements_tree_for Marketplace
Then, just change the hardcoded values in your helper to the
following:
The line below will now simply be:
tree_model
@tree_classes[self.class] # this would yield Marketplace for the
previous example
For example, in your helper where you say:
def display_node
Marketplace.root()
end
change this to:
def display_node
Change the line below to:
tree_model.root()
@tree_classes[self.class].root()
end
Hope this helps!
-Jordan
On Jun 27, 2007, at 11:32 AM, Leon Yeh wrote:
Hi,
I am trying to refactoring two helper files into one single file.
The helper module is to display act_as_tree model type of object.
I am
currently have two helper files that have the same exact code, so I
decided to combined them into one single file. Then have the
controller
to include the correct helper module.
My problem is at the tree helper module. I need to have access to the
model object, instead of just hardcoding the name of model object. I
tried to look around at the documentation, but got nowhere. Any
idea on how to get to the corresponding model object ?
Thanks in advance,
Leon
------------------
class MarketplaceController < ApplicationController
helper :tree
end
class ListingController < ApplicationController
helper :tree
end
module TreeHelper
def display_node
Marketplace.root() #???
end
end
Leon Yeh
New Avenue Systems Inc.
Imagination Delivered. www.newavenue.net
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby
--------------------------------------------------------------
Jordan A. Fowler
1830 Market St. #4
San Diego, CA 92102
E-mail: [EMAIL PROTECTED]
AIM: virtuosojordan
Phone: 406.546.8055
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby
--------------------------------------------------------------
Jordan A. Fowler
1830 Market St. #4
San Diego, CA 92102
E-mail: [EMAIL PROTECTED]
AIM: virtuosojordan
Phone: 406.546.8055
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby