DRY, repeating partials

It’s in HAML, which I hope is readable by most people here… It’s
concise though, which is nice.

So here’s what I have in my beautiful first partial. Explanation of this
obviously poor code and logic is after the code.

= render :partial => ‘reports/lists’, :locals => {:type => :cr,
:reports => reports.find_all { |i| i.modality == “CR” } }

= render :partial => ‘reports/lists’, :locals => {:type => :non_cr,
:reports => reports.find_all { |i| ![‘MG’, ‘CR’].include? i.modality } }

= render :partial => ‘reports/lists’, :locals => {:type => :mg,
:reports => reports.find_all { |i| i.modality == “MG” } }

And here is the beautiful second partial : ‘reports/lists’

.hideextra { white-space: nowrap; overflow: hidden; }

- if type == :cr
- elsif type == :mg
- else
= reports.size

%th Name
%th Date
%th Age
%th Procedure
- reports.each do |report|
%tr{ :class => cycle(“even”, “odd”) }
%div{ :style=>“width:215px”}= “#{report.last},
%div{ :style=>“width:80px”}= report.date
- if !report.old?
%div{ :style=>“width:55px”}= report.age
- else
%div{:style=>“width:55px”}= report.age
%td= report.proc

The first partial is called by the controller action, and in turn, it
splits the data a little more. There’s clearly some logic in there which
needs to be whisked away to some other location.
I am wondering if I should store the split into a hash, in a kind of
‘settings’ table, and then recall that.
Can I loop over a partial and call it over and over, with slightly
varying data sent to it (like the symbol, in here)… And if so, how do I
handle the fact that I want to display slightly different data in each
partial depending on what I’m displaying?

I am open to complete renovation, all the way straight down to the
fundamentals, so if I need to break the logic down completely and
rebuild it, I’m cool with that. I just know my way of thinking does not
scale, and I’m wondering how seasoned pros would do it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs