#364 composed_of: Access record information from converter #1436

Closed
wants to merge 4 commits into
from

Projects

None yet

6 participants

@franckverrot

AR#composed_of can now access the associated record

    class User < ActiveRecord::Base
      composed_of :some_aggregation, ...,  :converter => Proc.new { |record, values| ... }
    end

Tested against REE and 1.9.2-p180.

@tenderlove tenderlove was assigned Jun 1, 2011
@ilyakatz

hey there, this was a great addition, however, i noticed something strange - looks like it make a difference in what order the attributes were specified (i guess the one that is being specified in the composed_of, in this case value)

In the examples below, currency field is not populated

Rate.new(:start_at=>"2012-12-5", :end_at=>"2012-12-8",:value=>43,:currency=>"EUR")
/Users/ilyakatz/NetBeansProjects/xxxx/app/models/rate.rb:17
:converter => Proc.new { |record, value, currency| debugger; Money.new(value || 0, currency) }
(rdb:1) pp record
#

Rate.new(:start_at=>"2012-12-5", :end_at=>"2012-12-8",:currency=>"EUR",:value=>43)
/Users/ilyakatz/NetBeansProjects/xxxx/app/models/rate.rb:17
:converter => Proc.new { |record, value, currency| debugger; Money.new(value || 0, currency) }
(rdb:1) pp record
#

@lichtamberg

Coool!!!

Please apply this patch to the master branch!

@isaacsanders

@cesario Is this still an issue?

@steveklabnik
Ruby on Rails member

@cesario This pull request can't be cleanly merged any more. While I don't have the authority to merge it in anyway, clean patches are always faster to get merged than ones that aren't. :)

@steveklabnik
Ruby on Rails member

If #6743 gets merged, this feature won't be useful.

@pnegri pnegri referenced this pull request Jun 16, 2012
Merged

Removing composed_of #6743

@steveklabnik steveklabnik added a commit that closed this pull request Jun 18, 2012
@steveklabnik steveklabnik Removing composed_of from ActiveRecord.
This feature adds a lot of complication to ActiveRecord for dubious
value. Let's talk about what it does currently:

class Customer < ActiveRecord::Base
  composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
end

Instead, you can do something like this:

    def balance
      @balance ||= Money.new(value, currency)
    end

    def balance=(balance)
      self[:value] = balance.value
      self[:currency] = balance.currency
      @balance = balance
    end

Since that's fairly easy code to write, and doesn't need anything
extra from the framework, if you use composed_of today, you'll
have to add accessors/mutators like that.

Closes #1436
Closes #2084
Closes #3807
14fc8b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment