I would like to define a method available in both my views and my models
Let's say I have a view helper:
def foo(s) "hello #{s}" end
The view may use an assistant:
<div class="data"><%= foo(@user.name) %></div>
However, this <div> will be updated by calling ajax again. I use the to_json call in the controller, which returns the data as follows:
render :text => @item.to_json(:only => [...], :methods => [:foo])
This means that I must have foo defined in my Item model:
class Item def foo "hello
It would be nice if I had a DRY method that could be used both in my representations and in my models.
Usage may look like this:
Assistant
def say_hello(s) "hello #{s}" end
User.rb Model
def foo say_hello(name) end
Model Item.rb
def foo say_hello(label) end
View
<div class="data"><%= item.foo %></div>
controller
def observe @items = item.find(...) render :text => @items.to_json(:only=>[...], :methods=>[:foo]) end
I donβt know how best to deal with this, but I donβt want to go completely against the best practices here.
If you can come up with a better way, I really want to find out!