Forum: Ruby on Rails more questions: human_name

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.
Yannick M. (Guest)
on 2006-06-01 17:28
(Received via mailing list)
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
Pete Y. (Guest)
on 2006-06-02 05:02
(Received via mailing list)
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/
Michael D. (Guest)
on 2006-06-02 07:05
(Received via mailing list)
> 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/...
module-alias_method_chain


--
Michael D.
http://www.mdaines.com
Pete Y. (Guest)
on 2006-06-02 17:21
(Received via mailing list)
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. :)

>
>   http://weblog.rubyonrails.org/articles/2006/04/26/...
> module-alias_method_chain

Nice. I hadn't come across that before.

Cheers,

Pete Y.
http://9cays.com
This topic is locked and can not be replied to.