Rendering objects directly via <%= render @some_object %>


My original post might have been overlooked by most, but I’ve made a
plugin (link below) which allows rendering of objects directly via the
render method:

<%= render @some_object %> which would actuall render the @some_object
object (the object is even able to handle the options hash.

Rendering would actually be handeled by the object itself, by leaving it
up to the object to inteact with the template and parse the wished for
options hash back to the ActionView render method. This way the object
could descide to render nothing depending on internal state. The plugin
even allow the object being used as mere container of commonly used
:locals hashes set by the controller (as showed in the documentation).

An object could be made parseable anywhere, but the controller would be
an obvious place to start :wink:

class SomeClass

Include renject functionality to allow object to render itself

include ActionView::Renjects::BaseRenject

Generate hash to be parsed back to ActionView render method

The options parameter, which contains the original options hash,

could optionally be uesd to when generating the hash.

def renject_render_options( options = {} )

Generate the wished for hash

return { :partial => @partial, :locals = {:foo => @foo, :baa => @baa }}

attr_writer :partial
attr_writer :foo
attr_writer :baa


@some_object = SomeClass
@some_object.partial = ‘Partial’ = ‘Foo’
@some_object.baa = ‘Baa’

Rendering @some_object via

<%= render @some_object %>

corresponds to

<%= render :partial => @partial, :locals=> {:foo => @foo, :baa => @baa}

Now imagine having to repeat this multiple times - and then trying to
find a typo…

The source and documentation is still avalible at

NB. Since my initial post, I have changed the classes into modules, so
that any module could be rendered by extending these modules.

Kind regards
Heino H. Gehlsen