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

标签 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.'
}

等等

然后我会创建一个基本的表单助手,将这些解释包装在一个范围内(最初通过 jQuery 隐藏和显示),这样它们就可以通过 f.field_description(:title) 或类似的东西来实例化。

有人有更好的想法吗?我想将这个字段元数据保留在模型中,因为许多 View 可以使用相同的信息,而且我还认为在您回头查看代码时在模型中进行描述是很好的(比如 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

在 View 中:

<%= 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) %>

最佳答案

散列是一个相当简单的解决方案,但如果您使用的是 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 节,了解根据当前 View 模板分离翻译的简洁方法。

关于ruby - 向 ActiveRecord 模型添加可读的字段描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1493546/

相关文章:

ruby-on-rails - 带模型的 Ruby 聚合函数

mysql - 如何在数据库级别获取primary_key的列名?

ruby-on-rails - Rails 数据类型之间有什么区别?

ruby-on-rails - belongs_to 关联的 Rails 作用域

arrays - 将对象添加到哈希表中的数组

c# - foreach 哈希表中的项目

ruby-on-rails - 如何在 Ruby on Rails 中集成 Paypal 集成

windows - 在 Windows 上启动 Sinatra 应用程序时,shotgun gem 会爆炸

ruby - tar 格式替代方案

java - 哈希多映射空间节省问题