[rspec-users] OT: Implementation Question

Andrew Premdas apremdas at gmail.com
Fri Dec 19 11:56:03 EST 2008

You can use read_attribute and write_attribute in models to do this sort of
thing - though I'm not really clear exactly what it is you're trying to do.
I don't think normalising is quite the right term here.

Anyhow I think you're saying that you have an attribute whose storage format
is different from its presenation format. To deal with this you can override
the attribute accessor in the model e.g. you present product_names in a
capitlalized form even if they are stored lower case

  # Always titleize product names when showing
  def name

Another example, you're seriously old school and believe only capital
letters should be in databases

    def name=(name)
      write_attribute(:name, name.upcase)

Another technique would be to create a virtual attribute, so you can have
two representations of the same value. For example a price and a
currency_price where both refer to the same attribute but one deals with an
integer and the other deals with nicely formatted prices.


2008/12/19 James Byrne <lists at ruby-forum.com>

> I have asked about this issue on several different mailing lists and
> have had no response. If anyone here can offer some advice then I would
> be much obliged.
> I have a feature that essentially says:
>  Given I have a user "myuser"
>  When I fill in "User Name" with "MyUser"
>    And I press "Login"
>  Then I should find a user "myuser"
>    And I should be logged in
> The steps definitions for this are all quite conventional and at the
> moment "Then I should find the user "myuser"" is failing, as expected.
> My question is: What is best practice with respect to normalizing form
> input data?  My feeling is that this sort of thing belongs in the model
> but I cannot see a way to get this to work.  In short, how to implement
> this feature?
> I have extended String with a method called hll_keycase which preforms
> the desired normalization.  The setter for Users.username is overridden
> in the model and the stored values in the database are all properly
> normalized.  But, it seems to me at the moment that the only place to
> normalize form input data for the SELECT required to determine if the
> username exists or not is either in the form or in the controller.  Is
> that the case?
> This has to be a recurring issue for data input forms and yet I can find
> very few resources on the web that address this issue at all, and those
> are often terse to the point of unintelligible.
> Not exactly a test issue but I really could use some guidance with this
> as whatever technique I finally adopt will pervade the entire project.
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081219/5a19bbcd/attachment.html>

More information about the rspec-users mailing list