Best way to develop a self-contained widget?


I am developing a small widget composed of several form elements, which
are connected with javascript code (i.e. what the user does on an
element has an impact on what another element shows).

At first I put the code in a function in my application helper, but as
the widget was evolving, I wanted to put it in a dedicated class, as
each instance of the widget needs some business logic. Indeed, sometimes
the widget has to be placed several times on a page, and the UI items
are not always beside each other (they could be on several rows of a
table), so each widget should have several attributes (such as the DOM
id of each of its elements) and several methods (e.g. to display each
part of the widget as needed).

So I made a class in the /lib directory, containing all the code of the
widget. This is elegant, but then I noticed that I cannot use ActionView
helper functions (such as select_tag or javascript_tag) from within that
class. I know that I could include all the needed ActionView::Helpers
modules in the class, but is it really the best way to do that ? How
would you develop such a reusable widget, composed of html elements,
javascript and some ruby logic ?

Thanks in advance,