# DRY, repeating partials

**URL:** <https://www.ruby-forum.com/t/dry-repeating-partials/174909>\
**Category:** Rails\
**Created:** [September 3, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/dry-repeating-partials/174909 "2009-09-03T21:25:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [September 3, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/dry-repeating-partials/174909/1 "2009-09-03T21:25:58Z")

</div>

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.

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

#non\_cr  
= render :partial =\> ‘reports/lists’, :locals =\> {:type =\> :non\_cr,  
:reports =\> reports.find\_all { |i| ![‘MG’, ‘CR’].include? i.modality } }

#mammo  
= render :partial =\> ‘reports/lists’, :locals =\> {:type =\> :mg,  
:reports =\> reports.find\_all { |i| i.modality == “MG” } }

* * *

* * *

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

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

%center  
%h2  
- if type == :cr  
X-RAYS:  
- elsif type == :mg  
MAMMOGRAMS:  
- else  
MODALITIES OTHER THAN MAMMO AND XRAY:  
= reports.size

%table  
%thead  
%tr  
%th Name  
%th Date  
%th Age  
%th Procedure  
%tbody  
- reports.each do |report|  
%tr{ :class =\> cycle(“even”, “odd”) }  
%td  
%div{ :style=\>“width:215px”}= “#{report.last},  
#{report.first}”  
%td  
%div{ :style=\>“width:80px”}= report.date  
- if !report.old?  
%td  
%div{ :style=\>“width:55px”}= report.age  
- else  
%td  
%b  
%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.
