当前位置 : 主页 > 编程语言 > ruby >

ruby – 为ActiveRecord模型添加可读字段描述

来源:互联网 收集:自由互联 发布时间:2021-06-23
我想在ActiveRecord模型字段中添加说明,作为每个字段的基本说明/示例.基本上是模型元数据.然后我可以在UI中显示这些内容(在表单上的字段旁边等) 我计划这样做的方法就是在模型中创建
我想在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) %>
如果要修补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
网友评论