Forum: Ruby on Rails how would you implement this normalizer?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-02-19 21:07
(Received via mailing list)
Names of models in my application have accented characters that imply
unexpected orderings. For instance Ávila should go before Madrid, but
String#<=> puts it the other way aroud.

I am only sorting for views, so it would be OK to do some
normalization on-the-fly on Ruby land, and since the same criteria
has to be used throughout the application I was thinking on some sort
of generator whose usage would be:

   class User < ActiveRecord::Base
     normalize_for_sorting :surname
   end

The intention is not to modify surname, we need it in the views,
normalize_for_sroting would generate a method
User#surname_for_sorting and configure it as a creation/finding
filter that applies the necessary tr///. The aim is:

   * We normalize the strings once and store them in
     attributes instead of applying the normalizer
     in all sort blocks

   * The tr///, the configuration of filters, etc. is
     written in one place, following DRY.

I think that can be done, but I am not fluent enough yet in Rails to
determine whether it is a clean solution, and how to implement it in
an idiomatic way. Any advices?

-- fxn
7b8adabc68bcdcf2df5ea4a8441cecc4?d=identicon&s=25 Colin (Guest)
on 2006-02-19 22:53
How about modifying your data before putting it in the database?? That's
how i do the "trick".

===
class User < ActiveRecord::Base

  def before_save
    self.surname_for_sorting = normalize_for_sorting :surname
  end
end
===

or.. if you don't want extra data in the db, and format it for every
request.

===
class User < ActiveRecord::Base

  def surname_for_sorting
    @sfs = normalize_for_sorting(:surname) if !@sfs
    @sfs
  end
end
===

If you desperately want more DRY, make it a mixin.
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-02-19 23:11
(Received via mailing list)
On Feb 19, 2006, at 22:53, Colin wrote:

> end
> ===
>
> If you desperately want more DRY, make it a mixin.

What I have in mind is: a class method for models that generates
_for_sorting accessors on-the-fly based on parameters, and that
creates callbacks after_(save|find) that initialize those attributes.

I guess class_eval is all what I need, but which is the idiomatic way
to define normalize_for_sorting so that is visible in all models? Do
I open ActiveRecord::Base and add the method? If that's the way,
where do I do that? In which file?

-- fxn
This topic is locked and can not be replied to.