This project is archived and is in readonly mode.

#212 ✓wontfix
Chris Hapgood

Add alternate constructor support to aggregations

Reported by Chris Hapgood | May 16th, 2008 @ 10:15 PM

The composed_of macro assumes that the value object can be instantiated with new. But for some aggregate classes, new is not the appropriate constructor and instead something like parse is required.

This patch adds support for a :constructor option to the composed_of method. It accepts a symbol identifying a constructor method on the aggregate class. All aggregate object are then instantiated using this constructor method.

A typical example of this new functionality:

require 'uuid'

class Widget < ActiveRecord::Base
  composed_of :uuid, :constructor => :parse
end

Of course, the default value for the constructor is :new.

[This is identical in function to the enhancement documented on Trac ticket 11122. It has been updated to work on Edge and includes less brittle tests and a git-compatible patch.]

Comments and changes to this ticket

  • Pratik

    Pratik July 1st, 2008 @ 11:44 PM

    • State changed from “new” to “wontfix”
    • Tag set to activerecord, edge, enhancement, patch, tests

    You can just call parse from #initialize itself. Not convinced about the provided use case for this.

    Thanks.

  • Chris Hapgood

    Chris Hapgood July 2nd, 2008 @ 03:24 PM

    Agreed, you can call parse from MyARModel#initialize. But that seems like a reason for scrapping aggregations entirely because you could also instantiate the composed_of objects in #initialize as well.

    I would be happy to see aggregations disappear entirely -they seem like a very thin veneer on top of existing Ruby/AR constructors. But if they are worth keeping, why not make them useful for the large population of classes that use constructors like parse and from_x? The patch I propose is pretty trivial...

  • Pratik

    Pratik July 2nd, 2008 @ 03:41 PM

    If we have enough people asking for this, we can surely roll it in.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages