Forum: Ruby on Rails Rendering objects directly via <%= render @some_object %>

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3dcb0c9fdc4e2b501c8177d91a5869a2?d=identicon&s=25 Heino H. Gehlsen (Guest)
on 2006-02-07 15:16
(Received via mailing list)
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 ;-)

-----------------
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 }}
end

attr_writer :partial
attr_writer :foo
attr_writer :baa

end

@some_object = SomeClass
@some_object.partial = 'Partial'
@some_object.foo = '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
http://www.heino.gehlsen.dk/software/ruby/rails/pl...

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
This topic is locked and can not be replied to.