# Refactoring commonly used aggregations

**URL:** <https://www.ruby-forum.com/t/refactoring-commonly-used-aggregations/99468>\
**Category:** Rails\
**Created:** [July 23, 2006, 12:54am UTC](https://www.ruby-forum.com/t/refactoring-commonly-used-aggregations/99468 "2006-07-23T00:54:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 23, 2006, 12:54am UTC](https://www.ruby-forum.com/t/refactoring-commonly-used-aggregations/99468/1 "2006-07-23T00:54:34Z")

</div>

I have a number of models that use an aggregation to combine First Name,  
Middle Initial and Last Name and they have been working fine. I am  
looking to clean this up, DRY it if possible.

I have things like this in application.rb  
class Clwholename  
attr\_reader :first\_name, :middle\_initial, :last\_name  
def initialize(first\_name, middle\_initial, last\_name)  
@first\_name = first\_name  
@middle\_initial = middle\_initial  
@last\_name = last\_name  
end  
def to\_s  
[@first\_name, @middle\_initial, @last\_name].compact.join(" ")  
end  
end

and then in whichever model I need to use this, I have…  
composed\_of :clwholename,  
:class\_name =\> Clwholename,  
:mapping =\>  
[[:first\_name, :first\_name],  
[:middle\_initial, :middle\_initial],  
[:last\_name, :last\_name]  
]

which is repeated in a number of models. I ran into a snag when trying  
to use RAV (which is very cool but gags on my Clwholename class if I  
include models) and the suggestion was to put the stuff in  
lib/clwholename.rb and ‘require’ it in my model.

I seem to be working fine by putting the class definition for  
Clwholename in lib/clwholename.rb and requiring it in my model but the  
section of “composed of :clwholename” simply doesn’t work if I put it  
into helpers (I assume because that is for views and not controllers) or  
in the lib/clwholename (unknown method composed\_of) and I am not getting  
any DRYer.

What’s the best way to handle this?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 23, 2006, 1:18am UTC](https://www.ruby-forum.com/t/refactoring-commonly-used-aggregations/99468/2 "2006-07-23T01:18:59Z")

</div>

If it’s universal controller logic, put it in application\_controller.rb.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 23, 2006, 1:28am UTC](https://www.ruby-forum.com/t/refactoring-commonly-used-aggregations/99468/3 "2006-07-23T01:28:06Z")

</div>

I would set up an acts\_as\_name for the model part of it.

Craig W. wrote:

> I have a number of models that use an aggregation to combine First Name,  
> Middle Initial and Last Name and they have been working fine. I am  
> looking to clean this up, DRY it if possible.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 23, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/refactoring-commonly-used-aggregations/99468/4 "2006-07-23T16:01:23Z")

</div>

On Sun, 2006-07-23 at 01:28 +0200, Yehuda K. wrote:

> I would set up an acts\_as\_name for the model part of it.
> 
> Craig W. wrote:
> 
> > I have a number of models that use an aggregation to combine First Name,  
> > Middle Initial and Last Name and they have been working fine. I am  
> > looking to clean this up, DRY it if possible.

* * *

that makes sense but I wouldn’t know where to start. This sounds like a  
module. Any point to something I could read that would give me an idea  
on how to accomplish this?

Craig
