Is Helpers code forced to be module only?

9 views
Skip to first unread message

BobTodd

unread,
Jan 4, 2011, 12:15:37 PM1/4/11
to StaticMatic
Hi

Can anyone tell me what's wrong with this? I am trying to call the
url_helper method "link" from my own helper.

The HelperCentral code is full of pure module code (no classes). This
maybe my lack of understanding of modules/classes/mixins, but the
following code throws an error:

%body
=Big.new("An icecream").render

......

(big_helper.rb)

module BigHelper

end

class Big
include UrlHelper

attr_reader :title
def initialize(title)
@title = title
end

public

def render
link @title
end
end

And the error:

SystemStackError

stack level too deep

Stack Trace

/usr/lib/ruby/gems/1.8/gems/staticmatic-0.11.1/bin/../lib/staticmatic/
helpers/current_path_helper.rb:7:in `current_page'
/usr/lib/ruby/gems/1.8/gems/staticmatic-0.11.1/bin/../lib/staticmatic/
helpers/current_path_helper.rb:7:in `current_page'
/usr/lib/ruby/gems/1.8/gems/staticmatic-0.11.1/bin/../lib/staticmatic/
helpers/current_path_helper.rb:14:in `current_page_relative_path'
/usr/lib/ruby/gems/1.8/gems/staticmatic-0.11.1/lib/staticmatic/helpers/
url_helper.rb:30:in `link'
./agileapps/src/helpers/big_helper.rb:16:in `render'
(haml):12:in `render'

....

If I move the code into the helper and call that its no problem.

Outputting the problem variable "current_page" directly in the class
def shows empty, whilst in the module it has a value.

Also, any advice how you might go about debugging this code (stepping
through it)?

Thanks

Stephen Bartholomew

unread,
Jan 4, 2011, 5:52:21 PM1/4/11
to StaticMatic
Hey,

If you've used Rails, helpers in staticmatic work in the same way.
You don't need to create classes in this manner - any module code is
included into the methods available in the view.

If you have a large helper that requires a lot of code and passing
around data, include it as a class in lib/ and provide a module-based
helper to interface with it:

lib/big_class.rb:

class BigClass
...
def render(current_page)
...
end
end

helpers/big_helper.rb

module BigHelper
def print_link(title)
BigClass.new(title).render(current_page)
end
end

To be honest though, the simplest thing to do is break helpers down as
much as you can and separate them out into separate files based on
context and/or concern.

Hope that helps!

Steve

BobTodd

unread,
Jan 5, 2011, 7:39:50 AM1/5/11
to StaticMatic
Having read my OP I wasnt very clear...

The problem line is "link @title " < note the "link" method actually
belongs to the built-in staticmatic UrlHelper

When called from a module, the variable "current_page" which belongs
to the CurrentPageHelper (also built-in) and gets called by UrlHelper,
has a value.

When it is called from a class, it has no value and the page blows up.

I guess my Q is why. It seems the context is lost.

Stephen Bartholomew

unread,
Jan 5, 2011, 3:05:07 PM1/5/11
to StaticMatic
Hey,

It wont ever work properly if you use a class instead of a module.
The module is mixed into the class that renders the template. Because
it's mixed in, any helpers used in staticmatic will be available to
the helper module you create.

You shouldn't actually need to use a class so perhaps if you outline
what it is you're trying to do, I could give you a guide on how to
approach it to fit in with the helper pattern used?

Cheers,

Steve

BobTodd

unread,
Jan 6, 2011, 5:09:35 AM1/6/11
to StaticMatic
That's fine if its not possible - I have a workaround.

I won't be giving up my classes though - I just won't try and use the
staticmatic helpers from within the classes (must be a way to mix
those in to my classes too but I couldnt find it)

FYI I am creating a (very simple) reusable dhtml menu, and i want
classes to represent a menu and a menu item.
Reply all
Reply to author
Forward
0 new messages