Heino H. Gehlsen
2006-Feb-07 14:14 UTC
[Rails] 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 ;-) ----------------- 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/plugins/renjects/ 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 Heino H. Gehlsen wrote:> So, Ruby is OO all the way, but in Rails we can?t render an object? > > Having retyped way too many calls to ActionViews render method with > various options, I finally figured, why not make it possible to > actually render customized objects, which could encapsulate the often > messy code, that I often ended up writing partly in the controller and > partly in the view - using instance variables, and typing ?if? way too > often in the template. > > What I ended up with were ?Ren(d)jects?, objects capable of being > rendered. > > I have already made a functional plugin using only 66 lines of actual > code (well, actually only 28, but I thought a few basic helpers would > be nice ;-), and I would really like some feedback? > > Source and documentation is avalible at > http://www.heino.gehlsen.dk/software/ruby/rails/plugins/renjects/. The > documentation includes a brief introduction to the concept of Rendjets > - and of cause a few small examples, which show how to use them. To > some, the examples will perhaps at first sight seem at bit too > complex, but think code reuse and easier debugging! > > Having tried using Ren(d)jects when generating sidebars based on > multiple independent blocks, I already find encapsulating partial > rendering pretty neat ? from now on such blocks have to be very > complex before I?ll end up using components? > > Kind regards, > Heino H. Gehlsen > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails