How can I refactor out needing so many for-loops in rails? -


I need re-refactoring help to this multi-loop thing here What do I have here:

The campaign has many contacts - there are several models in the campaign that are templates: (email, call, and letter).

Because I'm looking for overdue, on each one, I have created an array called an event that I would like to loop in ['email', 'call', 'letter'].

I need a list of all emails, calls and letters that are "overdue" for each contact that is related to a campaign. Overdue is determined by one from the set method, which appears on the date which was entered into that system and the number of days that would have to be passed for any event. From today () today the number of days indicates that the event for a given contact should be done.

What I have done here is working for all the emails in one campaign in all the contacts I was going to try to make every one loop to change the class names.

It was not sure where to begin: name_scope, some things a method, etc, or - to change the names of the classes dynamically, repeating the code twice at least twice Beat three times in different programs:

  & lt;% @ campaigns.each do | Campaign | & Gt%; & Lt; H2 & gt; & Lt;% = link_to Campaign.name, Campaign% & gt; & Lt; / H2 & gt; & Lt;% @ events.each do Event | & Gt%; & Lt;% = event% & gt; For email in & lt;% campaign Miles% & gt; & Lt; H4 & gt; & Lt;% = link_to Emailtitle, Email% & gt; & Lt;% = email.days% & gt; Of the day & lt; / H4 & gt; In & lt;% campaign.contacts.find (all :: order = & gt; "date_edited ASC")% & gt; & Lt;% if (from_dade (contact, email.) & Lt; 0)% & gt; & Lt;% if show_status (contact, email) == 'no status'% & gt; & Lt; P & gt; & Lt;% = full_name (contact)% & gt; Is & lt;% = to_today (contact, email das) .abs% & gt; Day overdue: & lt;% = do_event (contact, email)% & gt; & Lt; / P & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% end% & gt; To add Patrick's answer, I also use it: 
Render for a little easier, for example, to present each contact, there is a partial _contact.html.erb:
  & lt;% if (from_dade (contact) , Email.) & Lt; 0)%> & Lt;% if show_status (contact, email) == 'no status'% & gt; & Lt; P & gt; & Lt;% = full_name (contact)% & gt; Is & lt;% = to_today (contact, email das) .abs% & gt; Day overdue: & lt;% = do_event (contact, email)% & gt; & Lt; / P & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% end% & gt;  

and then archive with contact

  = render: partial = & gt; "Contact" ,: archive = & gt; @ Contact  

I do not even search in the view, instead I will set up all the variables of the controller, and possibly transfer all the conditional code into one assistant. It is better to argue as much as possible in the ideas.


Comments