向ActiveRecord模型添加可读字段描述

时间:2009-09-29 16:11:33

标签: ruby activerecord hashtable field

我想在ActiveRecord模型字段中添加说明,作为每个字段的基本说明/示例。基本上是模型元数据。然后我可以在UI中显示这些内容(在表单等字段旁边)。

我计划这样做的方法就是在模型中创建一个静态哈希表,其中字段名称为键,描述为值。即。

FIELD_DESCRIPTIONS = {
  'category' => 'Select the category it should appear within.',
  'title' => 'The title should be a short but descriptive summary.',
  'description' => 'Please enter a full description.'
}

然后我会创建一个基本的表单助手,将这些解释包含在一个span中(最初隐藏并通过jQuery显示),这样它们就可以通过f.field_description(:title)或沿着这些行的东西进行实例化。

任何人都有更好的想法?我想将这个字段元数据保存在模型中,因为许多视图可以使用相同的信息,我还认为当你回过头来查看代码时,在模型中有描述是很好的(比如DataMapper可以如何在模型中使用,以指定字段)。

为了更详细地介绍一下我已经完成的工作(并且工作正常),这里是代码。我认为必须有更漂亮的方式在模型中表达这些描述,所以如果您有任何想法,请告诉我。

模特:

FIELD_DESCRIPTIONS = {
  'category' => 'Select the category it should appear within.',
  'title' => 'The title should be a short but descriptive summary.',
  'description' => 'Please enter a full description.'
}

def self.describe_field(field)
  FIELD_DESCRIPTIONS[field]
end

在application_helper.rb

def field_helper(form, field)
  "<span class='field_helper'>#{form.object.class.describe_field(field)}</span>"
end

在视图中:

<%= field_helper(f, 'title') %>

这将产生所需的输出:

<span class='field_helper'>The title should be a short but descriptive summary.</span>

更新

好的,这是我根据接受的答案使用的最终代码。

文件:/config/initializers/describe_attr.rb

if defined?(ActiveRecord)

  # let's us add attribute descriptions to each AR model
  class ActiveRecord::Base
    def self.describe_attr(*params)
      attrs = params.shift
      unless attrs.nil?
        case attrs
          when Hash
            @@attr_descriptions = attrs
          when Symbol
            return @@attr_descriptions[attrs]
        end
      end 
      @@attr_descriptions ||= {}
    end
  end

end

文件:/app/models/project.rb

describe_attr(
    :category => 'Select the category the project should appear within.',
    :title => 'The title should be a short but descriptive summary of the project.',
    :description => 'Describe the project in detail.',
    :image => 'Upload an image for the project.'
)

文件:/app/helpers/application_helper.rb

# assumes you have a style defined for attr_description
def describe_attr(form, attribute)
    "<span class='attr_description'>#{form.object.class.describe_attr(attribute)}</span>"
end

文件:/app/views/projects/_form.html.erb

<%= describe_attr(f, :title) %>

4 个答案:

答案 0 :(得分:4)

哈希是一个合理的简单解决方案,但是如果你在rails 2.2或更高版本上,你可能想尝试国际化api来做到这一点。如果您想添加翻译,这也会让您处于一个好位置。

查看the i18n guide了解详细信息,但基本上你会创建一个包含你的列名的config / locales / en.yml:

en:
  labels:
    category: Select the category it should appear within.

然后在你看来:

<%= t('labels.category') %>

命名空间当然是你的呼唤。另请参阅4.1.4节,了解基于当前视图模板分离翻译的简洁方法。

答案 1 :(得分:1)

您可以将解决方案与label帮助器本身混合使用:

f.label :title, f.describe(:title)

在你的模特中:

FIELD_DESCRIPTIONS = {
  :category => 'Select the category it should appear within.',
  :title => 'The title should be a short but descriptive summary.',
  :description => 'Please enter a full description.'
}

def describe(:field)
  self.class::FIELD_DESCRIPTIONS[field]
end

答案 2 :(得分:1)

如果要修补ActiveRecord,则可以执行以下操作:

# Add this at the bottom of enviroment.rb
class ActiveRecord::Base
  def self.field_description(*params)
    attrs = params.shift
    unless attrs.nil?
      case attrs
        when Hash
          @@field_description = attrs
        when Symbol
          return @@field_description[attrs]
        end
      end 
      @@field_description ||= {}
    end
end

在模型中,你可以像宏一样添加这一行:

class Product < ActiveRecord::Base

  field_description  :category => 'Select the category it should appear within.',:title => 'The title should be a short but descriptive summary.',:description => 'Please enter a full description.'

end

获取值

Product.field_description : title

答案 3 :(得分:1)

请务必查看formtastic,其中包括根据matschaffer的答案支持国际化(或不支持)字段标签。

http://github.com/justinfrench/formtastic/