Francois Beausoleil
2006-Mar-06 03:11 UTC
Why is FormHelper using value_before_type_cast ?
Hi ! Just wondering here, because if I define reader methods, then these aren't used in the forms. For example, I have this: class Customer def tax_rate (read_attribute(:tax_rate) * 100.0).to_i end def tax_rate=(rate) write_attribute(:tax_rate, rate / 100.0) end end <%= text_field :customer, :tax_rate %> For 7.5, the value shown on screen is 750. Of course, I could do: <%= text_field :customer, :tax_rate, :value => @customer.tax_rate %> but that gets pretty boring real quick... So, what is the rationale behind this decision ? Are the date/time helpers the ones that need the value_before_type_cast ? Thanks ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Michael Koziarski
2006-Mar-06 03:35 UTC
Re: Why is FormHelper using value_before_type_cast ?
On 3/6/06, Francois Beausoleil <francois.beausoleil@gmail.com> wrote:> Hi ! > > Just wondering here, because if I define reader methods, then these > aren''t used in the forms. > > For example, I have this: > class Customer > def tax_rate > (read_attribute(:tax_rate) * 100.0).to_i > end > > def tax_rate=(rate) > write_attribute(:tax_rate, rate / 100.0) > end > end > > <%= text_field :customer, :tax_rate %> > > For 7.5, the value shown on screen is 750. Of course, I could do: > <%= text_field :customer, :tax_rate, :value => @customer.tax_rate %> > > but that gets pretty boring real quick... > > So, what is the rationale behind this decision ? Are the date/time > helpers the ones that need the value_before_type_cast ?Otherwise the user''s data will be ''randomly'' changed by the system. i.e. irb(main):001:0> "asdifjasdliofj".to_i => 0 irb(main):002:0> "123,40".to_i => 123 So if it didn''t use _before_typecast, and the user typed either of those values, then when the page was re-rendered, the values won''t have been remembered correctly. The previous behaviour was worse and led to many bug reports about 0 appearing where it wasn''t expected. -- Cheers Koz
Francois Beausoleil
2006-Mar-06 03:38 UTC
Re: Why is FormHelper using value_before_type_cast ?
2006/3/5, Michael Koziarski <michael@koziarski.com>:> On 3/6/06, Francois Beausoleil <francois.beausoleil@gmail.com> wrote: > > class Customer > > def tax_rate > > (read_attribute(:tax_rate) * 100.0).to_i > > end > > > > def tax_rate=(rate) > > write_attribute(:tax_rate, rate / 100.0) > > end > > end > > Otherwise the user's data will be 'randomly' changed by the system. i.e. > > irb(main):001:0> "asdifjasdliofj".to_i > => 0 > irb(main):002:0> "123,40".to_i > => 123 > > So if it didn't use _before_typecast, and the user typed either of > those values, then when the page was re-rendered, the values won't > have been remembered correctly.How can custom reader/writer methods be accomodated then ? Thanks ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Michael Koziarski
2006-Mar-06 03:43 UTC
Re: Why is FormHelper using value_before_type_cast ?
> How can custom reader/writer methods be accomodated then ? >See http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/base.rb#L1561 if you implement your own _before_type_cast methods you should be fine. Why not just define a separate accessor though, i.e. one used during calculations but not during user input? -- Cheers Koz
Francois Beausoleil
2006-Mar-06 03:53 UTC
Re: Why is FormHelper using value_before_type_cast ?
2006/3/5, Michael Koziarski <michael@koziarski.com>:> See http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/base.rb#L1561 > > if you implement your own _before_type_cast methods you should be > fine. Why not just define a separate accessor though, i.e. one > used during calculations but not during user input?I WANT the value to be displayed as 7.50, not 750 ! #***_before_type_cast returns the latter, which is incorrect from the user's point of view. He really typed 7.5, but the value is stored as 750, to ensure floating point values are not modified along the way. So, if I override tax_rate_before_type_cast, I should be all right ? I'll test this out. Thanks for the info, Michael. Bye ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core