More questions: human_name


#1

One more question:

Is there some way to set the human_name of a column? e.g.: human_name
for column address1 shouldn’t be Address1 but “Address, line 1”.

If not, should I make a hash with my custom names?

Best regards,

Yannick M. http://www.inma.ucl.ac.be/~majoros
Informaticien UCL/INMA-MEMA
4, avenue G. Lemaître
B-1348 Louvain-la-Neuve
Belgium
Tel: +32-10-47.80.10
Fax: +32-10-47.21.80
Si vous avez des problèmes pour afficher ce message (accents qui ne
passent pas, signature électronique, …) votre système de mail n’est
pas conforme aux standards modernes, voir
http://www.inma.ucl.ac.be/~majoros/email.html
#JAPH : http://www.inma.ucl.ac.be/~majoros/japh.txt


#2

ActiveRecord uses the humanize method from the Inflector module in
ActiveSupport to convert column names to human readable form. (See
http://rails.outertrack.com/module/Inflector/humanize) You can
potentially override this method something like this:

module Inflector
def humanize_with_fixes(name)
case name
when ‘address1’
‘Address line 1’
else
humanize_without_fixes(name)
end
end

 alias_method :humanize_without_fixes, :humanize
 alias_method :humanize, :humanize_with_fixes

end

I’m not sure if this is the recommended approach, but it should work.
It might be easier to just rename your column to address_line_1!

(Note that ActiveRecord also has a human_attribute_name method you
could override for the same purpose, but source code comments
indicate that this method is deprecated and that ActiveRecord may
call the Inflector directly.)

Cheers,

Pete Y.
http://9cays.com/


#3

You can potentially override this method something like this

That seems slightly icky, (where is that code going to go?) but I
can’t think of anything better at present. How are these custom names
going to be used?

But also! You can change this

alias_method :humanize_without_fixes, :humanize
alias_method :humanize, :humanize_with_fixes

to this

alias_method_chain :humanize, :fixes

as shown here

http://weblog.rubyonrails.org/articles/2006/04/26/new-in-rails-
module-alias_method_chain


Michael D.
http://www.mdaines.com


#4

On 02/06/2006, at 1:03 PM, Michael D. wrote:

You can potentially override this method something like this

That seems slightly icky, (where is that code going to go?) but I
can’t think of anything better at present.

I never said it was a nice solution. :slight_smile:

http://weblog.rubyonrails.org/articles/2006/04/26/new-in-rails-
module-alias_method_chain

Nice. I hadn’t come across that before.

Cheers,

Pete Y.
http://9cays.com