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

Dav*_*pin 4 ruby activerecord field hashtable

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

我计划的方法是简单地在模型内创建一个静态哈希表,以字段名称作为键,以描述作为值。IE

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.'
}
Run Code Online (Sandbox Code Playgroud)

ETC。

然后我将创建一个基本表单助手,将这些解释包装在一个跨度内(最初通过 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
Run Code Online (Sandbox Code Playgroud)

在 application_helper.rb 中

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

视图中:

<%= field_helper(f, 'title') %>
Run Code Online (Sandbox Code Playgroud)

这将产生所需的输出:

<span class='field_helper'>The title should be a short but descriptive summary.</span>
Run Code Online (Sandbox Code Playgroud)

更新:

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

文件:/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
Run Code Online (Sandbox Code Playgroud)

文件:/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.'
)
Run Code Online (Sandbox Code Playgroud)

文件:/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
Run Code Online (Sandbox Code Playgroud)

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

<%= describe_attr(f, :title) %>
Run Code Online (Sandbox Code Playgroud)

Mat*_*fer 5

哈希是一个合理且简单的解决方案,但如果您使用的是 Rails 2.2 或更高版本,您可能需要尝试国际化 api 来执行此操作。如果您想添加翻译,这也会让您处于一个有利的位置。

查看i18n 指南了解详细信息,但基本上您将创建一个 config/locales/en.yml,其中包含您的列名称,例如:

en:
  labels:
    category: Select the category it should appear within.
Run Code Online (Sandbox Code Playgroud)

那么在你看来:

<%= t('labels.category') %>
Run Code Online (Sandbox Code Playgroud)

当然,命名空间是您的决定。另请查看第 4.1.4 节,了解根据当前视图模板分离翻译的巧妙方法。

  • 除非由于某种原因它不能满足您的需求(我无法想象为什么不这样做),否则这绝对是正确的选择。需要使用这些描述的插件和 gem 将在此处查看。 (2认同)