Topic: What's the best way to refactor a big chunk of code like this?

Helper
[code:ruby]
  def current_projects(logistics)
    Project.find(:all, :conditions => {:assigned_to => logistics, :completed => "no" })
  end
[/code]

View
[code:ruby]
<h4>name</h4>
<% for project in current_projects('email') %>
  <%= link_to(truncate(project.name, 24), {:controller => 'projects', :action => 'show', :id => project.id}) -%><br />
<%end%><br />
<h4>name</h4>
<% for project in current_projects('email') %>
  <%= link_to(truncate(project.name, 24), {:controller => 'projects', :action => 'show', :id => project.id}) -%><br />
<%end%><br />
<h4>name</h4>
<% for project in current_projects('email') %>
<%= link_to(truncate(project.name, 24), {:controller => 'projects', :action => 'show', :id => project.id}) -%><br />
<%end%><br />
[/code]

As you can see, this is pretty ugly in the view, but I'm having problems running for or .each loops in controllers or helper methods.  Is there anyway to clean this up?

I know I can take the <%= link_to(truncate(project.name, 24), {:controller => 'projects', :action => 'show', :id => project.id}) -%><br /> and make that a custom helper, but is there anyway to set the loops in a helper when you have multiple finds like this?

Last edited by malkomalko (2007-07-30 03:54:36)

Re: What's the best way to refactor a big chunk of code like this?

What is name and what is email and where is it coming from? If you're iterating through, say, users, then you can have an iterating block inside of another iterating block. It seems to me you just want to loop over a certain number of people.

Re: What's the best way to refactor a big chunk of code like this?

I just took out the real names and email's of the people.  What I'm doing is creating a sidebar to show all users tasks that are assigned to them.

Re: What's the best way to refactor a big chunk of code like this?

I don't get why the code is repeated 3 times? Is that just a typo?

Alex

Re: What's the best way to refactor a big chunk of code like this?

Alex, I took out the real names and email addresses of the users.  But say I had 6 users, I want to show all Projects that are assigned to everyone.

Re: What's the best way to refactor a big chunk of code like this?

Having a feeling this is retarded and should try the group_by

Re: What's the best way to refactor a big chunk of code like this?

WHy cant you just do this?

<% for project in current_projects('email') %>
<h4>name</h4>
<%= link_to truncate(project.name, 24), {
  :controller => 'projects',
  :action => 'show',
  :id => project} -%><br /><br />
<%end%>

however if you are passing different arguments into your current_projects method, then you can just do a loop within a loop

<% for arg in args%>
   <% for project in current_projects(arg) %>
      <h4>name</h4>
      <%= link_to truncate(project.name, 24), {
        :controller => 'projects',
        :action => 'show',
        :id => project} -%><br /><br />
    <%end%>
<%end%>

Of course, if you do that, then you need to set up args as an array of options..

args = [email,name,whatever,etc]

Preferably this should live within the database or model. It's hard to make the call without knowing exactly what you are trying to do.

Last edited by pimpmaster (2007-07-30 04:31:30)

Re: What's the best way to refactor a big chunk of code like this?

I was in fact looking for group_by.  I was trying to group projects by people who are assigned.  Sorry I didn't explain that very well.

In my sidebar partial I put:
[code:ruby]
<% Project.find_all_by_completed("no").group_by {|t| t.assigned_to}.each do |user, projects| %>
... Loop through the projects array to display.
[/code]