Topic: Why are components bad?

Yesterday I was playing around with some old code just for fun. After a while I realized that this model I was playing around with would be nice to rewrite as a partial. But then I'll get some problems if I invoke the partial from another controller, then I won't have access to the first model's controller actions where I generate some variables.

I don't won't to duplicate the code in all controllers where I use the partial. I could move some code into the model so I don't have to define my variables in the controller, but that doesn't feel right at all.

So then I thought about components, they seem to suite the task. But after a quick search at google I found out that people generally don't like components, and that they are slow and so on.

So just out of curiosity, why are components bad? And "how" slow are they? Are there any situations when components actually is the best option? Have anyone used them in a real application?


Re: Why are components bad?

From what I have heard, components are about 3-5 times slower than using a normal partial. I would say this is acceptable for most applications if they don't go overboard. I have also heard success stories with using components and believe there are times when they are the best option.

However, I have never run into the need for a component and I recommend you explore all other options first. Often you can remove this duplicate code into one of these:

1. before filter
2. helper method
3. model

Which one depends on the situation. If you post your code I can give you a more detailed answer.

Railscasts - Free Ruby on Rails Screencasts

Re: Why are components bad?

Thanks for the advice! A combination of the three suggestions would solve the problem in this case. And in most other cases too I guess.

But what if I want to make an application that have a few different components (Not Ruby components). For example my own startpage that display the weather forcast, some sports results and todays TV shows. I could put all the logic behind this in the same controller, but it doesn't feel right, they have nothing in common except that they are displayed on the same page.

It would feel much better to separate theese three into different parts, so that the startpage would just call all the different parts it want to render. Easier to maintain and overview if I want to add or remove a "component".

Do I solve this with helper methods and filters as well?


Re: Why are components bad?

Have anyone tried this?

Looks promising at first glance but haven't had the time to look into it.

Re: Why are components bad?

Peter wrote:

Do I solve this with helper methods and filters as well?

I think a helper method is a good solution. It basically allows you to mix logic with a partial:

# in helper file
def weather_forecast
  # get the wether forecast details here
  # ...
  render :partial => 'shared/forecast' # pass local variables if you need to ...

I still don't like putting too much logic in the helper method, but you can refactor this into its own class later if you need to.

Railscasts - Free Ruby on Rails Screencasts