[Boulder-Denver Ruby Group] re-inventing components

ara.t.howard ara.t.howard at gmail.com
Sun May 20 09:19:21 EDT 2007


On May 18, 2007, at 7:24 PM, David Clements wrote:

> Not sure if I am totally following the issue here. I totally agree
> with the premise that the support for widgetizing our apps is really
> ugly. And I use components regularly to widgetize various portions of
> our apps.
>
> Are we talking about syntax or functionality?

both.

>
> We could re-write your example today using render_component_as_string:
>
> class FooController < ApplicationController
>    def bar
>      # get a handle on, and parameterize, a bar controller
>      bar_controller = component_for(:controller => 'bar') do
>        @foo = 42
>        @bar = 'forty-two'
>      end
>
>      # call two actions on the bar controller, potentially reusing
> the entire model+view+controller stack
>      foo = bar_controller.content_for :action => 'foo'
>      bar = bar_controller.content_for :action => 'bar'
>
>      render :text => [foo, bar].inspect #=> [42, "forty-two"]
>    end
> end
>
>
> With this:
>
> class FooController < ApplicationController
>    def bar
>      params_hash = {:foo => 42, :bar => 'forty_two'}
>
>      # call two actions on the bar controller, potentially reusing
> the entire model+view+controller stack
>      foo = render_component_as_string( params_hash.merge(:controller
> => "bar", :action => "foo" )
>
>      bar = render_component_as_string( params_hash.merge(:controller
> => "bar", :action => "bar" )
>
>      render :text => [foo, bar].inspect #=> [42, "forty-two"]
>    end
> end
>


the main difference in the above is that, using 'component_for' is  
constructing an object *once*.  using 'render_component_as_string' is  
making two objects.  in the case of components these can be very  
expensive objects to create

class SpreadSheetController < ApplicationController
   def initialize
     @rows = select_100_100_rows
   end

   def foo
     something_with_100_000_rows
   end

   def bar
     something_with_100_000_rows
   end
end

of course there's nothing on 'component_for' which adds some measure  
of turning completeness to ruby/rails - nothing truely new is gained  
- it's simply the difference between creating objects which can be  
manipulated and combined vs. setting up a bunch of parameters and  
calling a bunch of methods.  i personally just happen to map most  
problems to objects in my mind and find it a useful abstraction to be  
able to do this with rails' views - it's certainly not a panacea  
though! ;-)

anyhow, i'm happy to hear you are using components - people claim  
that they are much slower, but my tests haven't managed to show  
this.  can you comment on that?

hope you are having fun at the jupiter!

cheers.


-a
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama





More information about the Bdrg-members mailing list