加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 01:23:18 所属栏目:百科 来源:网络整理
导读:我想在ActiveRecord模型字段中添加说明,作为每个字段的基本说明/示例.基本上是模型元数据.然后我可以在UI中显示这些内容(在表单上的字段旁边等) 我计划这样做的方法就是在模型中创建一个静态哈希表,其中字段名称为键,描述为值.即 FIELD_DESCRIPTIONS = { 'ca
我想在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.','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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读