In the example I was needing this for: We need to allow people to enter
prices in dutch format, i.e.:
12,25 (notice the comma). Before storing this into the database we would
like to transform this value to: 12.25 (notice the dot).
When displaying the value in the view we need to do the opposite, retrieve
the value from the database and replace dots by comma's and display the
value to the end user.

We solved this easily using a virtual attribute, however, it would be nice
if it were just possible to override getters and setters in AR. It just
seems awkward that this is not possible for helper methods.

Why is this so hard? Why not save the original value typed in by the end
user, and if validation goes wrong just display the original entered value,
else just use the overridden setter?

I might be missing something though....



On 5/25/07, Nik Wakelin <[EMAIL PROTECTED]> wrote:
>
> I think the only way you want to format it is for display to the end
> user, store it in the database in a different format.
>
> On 5/25/07, Josh Susser <[EMAIL PROTECTED]> wrote:
> >
> > That takes care of the transformation in one direction, but not the
> > other.  I suppose you can just handle that the usual way with a
> > before_validation callback.  Sorry if that's silly, I haven't been
> > following this thread in detail.
> >
> > --josh
> >
> > On May 24, 2007, at 10:22 PM, Nik Wakelin wrote:
> >
> > > How about this for a solution.
> > >
> > > The real problem seems to be that people want to format attributes
> > > before they display them (credit card, phone number). So, how about we
> > > allow that. Could be as simple as:
> > >
> > >  <%= text_field :person, :name, :formatter => :random -%>
> > >
> > >  class Person < ActiveRecord::Base
> > >
> > >    def random(value)
> > >       value.upcase * 4
> > >    end
> > >
> > >  end
> > >
> > > Thoughts?
> > >
> > > On 5/23/07, Mislav Marohnić <[EMAIL PROTECTED]> wrote:
> > >> On 5/23/07, Nik Wakelin <[EMAIL PROTECTED]> wrote:
> > >>>
> > >>>   1) If an overridden accessor is there, (i.e you've defined
> > >>> Product#price) then that gets called
> > >>>    2) Otherwise, we call price_before_type_cast
> > >>
> > >> That's still no good. You want to be able to display to the user
> > >> exactly
> > >> what he entered, unconditionally. Doing what you suggested would
> > >> make the
> > >> behavior even more inconsistent.
> > >>
> > >>
> > >>>
> > >>
> > >
> > >
> > > --
> > > Nik Wakelin
> > > (027) 424 5433
> > > [EMAIL PROTECTED]
> > >
> > > >
> >
> > --
> > Josh Susser
> > http://blog.hasmanythrough.com
> >
> >
> >
> > >
> >
>
>
> --
> Nik Wakelin
> (027) 424 5433
> [EMAIL PROTECTED]
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to