<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Making #composed_of more useful Comments</title>
  <id>tag:opensoul.org,2008:/2006/11/16/making-code-composed_of-code-more-useful/comments</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
  <updated>2007-12-14T14:56:26Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Ole Begemann</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:4602</id>
    <published>2007-12-14T14:56:26Z</published>
    <updated>2007-12-14T14:56:26Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Ole Begemann</title>
<content type="html">&lt;p&gt;Congratulations on the accepted patch, Brandon. I&#8217;ve just begun using it in my project together with the Money gem, too.&lt;/p&gt;


&lt;pre&gt;
class Project &amp;lt; ActiveRecord::Base
  composed_of :budget, :class_name =&amp;gt; &quot;Money&quot;, :mapping =&amp;gt; %w(budget cents), :allow_nil =&amp;gt; true do |amount|
    amount.to_money
  end
end
&lt;/pre&gt;

	&lt;p&gt;The backend part works great but I seem to have one problem with displaying the form field in an edit form:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;%= f.text_field &quot;budget&quot; %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;This displays the cents value in the input field (i.e. &#8220;24099&#8221;) while I obviously would like it to display it in &lt;span class=&quot;caps&quot;&gt;EUR&lt;/span&gt; instead (&#8220;240.99&#8221;). When I then submit the form without changing anything, the &#8220;24099&#8221; gets converted to a Money object representing 24099 &lt;span class=&quot;caps&quot;&gt;EUR&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Looking at Rails&#8217;s form_helper.rb, the reason seems to be that Rails calls @project.budget_before_type_cast to construct the display value for the input field and sure enough, this yields the cents value.&lt;/p&gt;


	&lt;p&gt;Have you experienced this problem? Or does it work differently for you? Overwriting budget_before_type_cast should do the trick, but I&#8217;m curious why nobody else seems to have this problem. I&#8217;m on Rails 2.0.1.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:4028</id>
    <published>2007-10-24T04:05:51Z</published>
    <updated>2007-10-24T04:05:51Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Brandon</title>
<content type="html">&lt;p&gt;The patch &lt;a href=&quot;http://dev.rubyonrails.org/changeset/8003&quot;&gt;finally got accepted&lt;/a&gt;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Chris Cruft</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:3920</id>
    <published>2007-10-09T13:00:07Z</published>
    <updated>2007-10-09T13:00:07Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Chris Cruft</title>
<content type="html">&lt;p&gt;Brandon,
I&#8217;ve been using your patch for some time and nursing it along with an update lately (update available on dev.rubyonrails.org).  I&#8217;m tired of this being a patch since it fixes an egregious problem with nil values as well.&lt;/p&gt;


	&lt;p&gt;Any chance you can shake the trees by posting to the Rails core Google Group?  I&#8217;ll pile on and back you up, and I know Ian White will as well (he and I worked on an earlier patch that fixed that nil problem).&lt;/p&gt;


	&lt;p&gt;-Chris&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Dave Bryand</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:3139</id>
    <published>2007-06-26T18:21:53Z</published>
    <updated>2007-06-26T18:21:53Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Dave Bryand</title>
<content type="html">&lt;p&gt;Sweet&#8212;I don&#8217;t think that will be an issue in my case.&lt;/p&gt;


	&lt;p&gt;Thanks!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:3116</id>
    <published>2007-06-22T06:42:05Z</published>
    <updated>2007-06-22T06:42:05Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Brandon</title>
<content type="html">&lt;p&gt;Dave,&lt;/p&gt;


	&lt;p&gt;The only issue I can think of is if you want the composed_of object to use the same name as an attribute. For example, if you have a field called balance that you want to be replaced by the composed_of.&lt;/p&gt;


	&lt;p&gt;Other than that, I think it&#8217;s a good idea.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Dave Bryand</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:3109</id>
    <published>2007-06-21T16:31:29Z</published>
    <updated>2007-06-21T16:31:29Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Dave Bryand</title>
<content type="html">&lt;p&gt;Thanks for this&#8212;it was a big help.&lt;/p&gt;


	&lt;p&gt;What I&#8217;d like to be able to do is to pass the value from an instance method that I create into the mapping of &#8220;composed_of&#8221;.&lt;/p&gt;


	&lt;p&gt;In both the Rails source and your plugin, the value that gets passed from the model containing the composed_of call into the other model has to be in @attributes.&lt;/p&gt;


	&lt;p&gt;I made it work by simply changing the &lt;strong&gt;read_method&lt;/strong&gt; method slightly. Instead of reading the attribute, I just call send and pass it the first element of the the mapping.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;read_attribute(pair.first)&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;to&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;send(pair.first)&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;in&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
        def reader_method(name, class_name, mapping, allow_nil)
          module_eval do
            define_method(name) do |*args|
              force_reload = args.first || false
              if (instance_variable_get(&quot;@#{name}&quot;).nil? || force_reload) &#38;&#38; (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
                # instance_variable_set(&quot;@#{name}&quot;, class_name.constantize.new(*mapping.collect {|pair| read_attribute(pair.first)}))
                instance_variable_set(&quot;@#{name}&quot;, class_name.constantize.new( *mapping.collect do |pair| send(pair.first) end ))
              end
              return instance_variable_get(&quot;@#{name}&quot;)
            end
          end
        end        
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Can you think of any negative effects that this might have? Is there a better way to do it?&lt;/p&gt;


	&lt;p&gt;Thanks!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>jerry rirchardson</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:723</id>
    <published>2007-01-12T15:54:08Z</published>
    <updated>2007-01-12T15:54:08Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by jerry rirchardson</title>
<content type="html">&lt;p&gt;i had been banging my head on my desk for an hour or so, but you came to my rescue. Hail the liberator! Hail Brandon! I hope your patch gets into rails.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:697</id>
    <published>2006-12-08T19:35:32Z</published>
    <updated>2006-12-08T19:35:32Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by Brandon</title>
<content type="html">&lt;p&gt;jake:&lt;/p&gt;


	&lt;p&gt;I&#8217;m not sure that I get what you mean, could you give an example?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>jake</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:672:690</id>
    <published>2006-12-04T19:39:19Z</published>
    <updated>2006-12-04T19:39:19Z</updated>
    <category term="work"/>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Comment on 'Making #composed_of more useful' by jake</title>
<content type="html">&lt;p&gt;Thanks for this article. I am currently facing a puzzle: is it possible to create ActiveRecord aggregations for objects whose attributes include other objects? If I don&#8217;t need to be able to access the attributes of the sub-object, can I just declare the object column as type :binary and be done with it? The Rails docs are mum on this so any hints would be greatly appreciated!&lt;/p&gt;</content>  </entry>
</feed>
