Agile Web Development

Build it. Launch it. Love it.

Human Attribute Override

HumanAttributeOverride

================

Read a tutorial for this plugin at http://weblog.redlinesoftware.com/2008/1/2/human-attribute-override-plugin-tutorial


Updates

Nov. 14, 2007

Fixed a bug where column.human_name didn’t use the new attribute name.

This should now produce the correct results:

<% for column in Company.content_columns %>

  <%= column.human_name %>

<% end %>

Aug. 18, 2007

Attributes can now be specified with symbols or strings (only strings were accepted before) ex. attr_human_name :num_employees => ‘Number of employees’, ‘unit_num’ => ‘Unit#’

Attributes can also be accessed with symbols or strings ex. Company.human_attribute_name(‘num_employees’) == ‘Number of employees’ Company.human_attribute_name(:num_employees) == ‘Number of employees’


After Rails 1.2, the method human_attribute_name will be removed, so this must be implemented as a plugin to keep the method and it’s existing uses of the method intact, while adding additional functionality described below.

This plugin allows humanized versions of attributes to be overridden with custom strings to provide a better conversion than humanize may provide.

This is useful in error reporting and possibly legacy databases where more "cryptic" field names may be used in database tables and a humanized version is not very human.

Rails uses these humanized conversions in error reporting with the error_messages_for method and in schema definitions for column names with the human_name method.

For example, a table (Company) has a field called num_employees which when humanized becomes ‘Num employees’ using the human_attribute_name method (which currently equates to a humanize method call). Ex.

Company.human_attribute_name(‘num_employees’) == ‘Num employees’

With this patch you can override this conversion…

class Company

  attr_human_name 'num_employees' => 'Number of employees'
  ...

end

Now the following is true…

Company.human_attribute_name(‘num_employees’) == ‘Number of employees’

Vitals

Home http://www.redlinesoftware.com
Repository http://github.com/redlinesoftware/human_attribute_override/tree/master
License Rails' (MIT)
Tags Tag_red
Rating (25 votes)
Owner Redline Software Inc.
Created 16 November 2006

Comments

  • Avatar
    19 March 2007

    Nice!

    Needs update in order to work well under Rails 1.2, it crashes in Errors::full_messages because now error messages are arrays and not strings, workaround is only two lines of code :-)

  • Avatar
    Andrew
    29 March 2007

    I wrote this plugin for use mainly with rails 1.2+. Can you show me what you changed? I've never experienced any issues so far with the plugin with any of the recent rails versions. The plugin version of full_messages is basically the same as that found in edge rails. I'll update the code asap with a little more info. Thanks.

  • 24 August 2007

    Could somebody please fix the issue demimismo pointed out. I am having the same problem here.

  • Avatar
    Andrew
    4 October 2007

    Please check that you're not using another plugin that is breaking this one. I've been using this plugin in rails 1.2+ since it came out and have never had an issue and I've used it in multiple apps.

    Please post more information on the error so I can look into any issues further.

  • RainChen
    20 October 2007

    seems not working with : <% for column in Company.content_columns %> <p> <b><%= column.human_name %></b> </p> <% end %>

    I think the issue is the code:

    class Column
      def human_name
        Base.human_attribute_name(@name)
      end
    end
    
  • Avatar
    Andrew
    15 November 2007

    RainChen... I have now fixed this. Thanks for reporting the issue. I didn't see your comment until today, which is why this wasn't updated earlier.

  • Hique
    15 January 2008

    Does the plugin work with label helper?

  • Avatar
    Andrew
    23 January 2008

    If another plugin uses the human_name method then yes, this plugin should work with it. If another plugin uses the humanize method, then no, this plugin will not do anything since humanize is a string method.

  • 3 May 2008

    I did a little hack on label helper in FormBuilder. If you type

    f.label :name

    It will first search for human_name for that attribute. You can find it here:

    http://hiquepedia.hique.org/2008/4/10/expanding-attr_human_name

Add a comment