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