简体   繁体   English

在rails中添加模型范围的文档

[英]Adding documentation for model scopes in rails

I'm not sure if this is actually possible or not but I'm trying to make the documentation of our rails app more complete by adding documentation for the scopes in our app/models files. 我不确定这是否真的可行,但我正在尝试通过在app/models文件中添加范围文档来使rails应用程序的文档更加完整。 What I'm looking to try and do is: 我想要尝试做的是:

# This is a description of what the scope does and the action that it performs
scope :newest_records, order("created_at desc").limit(50)

And then when I run rdoc over the app I want to see newest_records listed as a public class method along with the more traditional methods which will get documented like: 然后,当我在应用程序上运行rdoc时,我希望newest_records列为公共类方法以及更为传统的方法,这些方法将记录如下:

# some more documentation about this method
def self.a_class method
  ....
end

EDIT 编辑

I realise this question may be a bit ambiguous. 我意识到这个问题可能有点含糊不清。 So here's an attempt to clarify: At present when I try to add a comment line above a scope declaration I get NO documentation generated by RDoc for the scope. 所以这里是一个澄清的尝试:目前,当我尝试在scope声明上方添加注释行时,我得不到RDoc为范围生成的文档。 I know RDoc can pick up meta methods/attributes otherwise it wouldn't be displaying attributes in the docs which are declared with attr_accessor . 我知道RDoc可以获取元方法/属性,否则它不会在使用attr_accessor声明的文档中显示属性。 So my question is how do I add comments to my file so that : 所以我的问题是如何在我的文件中添加注释,以便:

  • The method appears in my RDoc generated documentation 该方法出现在我的RDoc生成的文档中
  • It appears as a public class method (as opposed to public instance method etc) 它看起来像一个公共类方法(而不是公共实例方法等)

After some digging around in the RDoc Docs I think I've managed to answer my own question. 在RDoc Docs中进行了一些挖掘后,我想我已经设法回答了我自己的问题。

You can document a scope as follows: 您可以按如下方式记录范围:

##
# :singleton-method:
# Documentation for the scope to explain what it does
scope :newest_records, order("created_at desc").limit(50)

The double hash is used to pick up meta-programmed methods, and if you're creating an instance method then that's all you need. 双哈希用于获取元编程方法,如果您正在创建实例方法,那么这就是您所需要的。 However as scope create a class method you also need to use the :singleton-method: line to indicate that. 但是,当作用域创建一个类方法时,您还需要使用:singleton-method:行来表示。 Documentation the continues as normal on the following lines. 在以下行中记录正常继续。

You can see the full syntax for documenting meta-methods etc in the RDoc Documentation 您可以在RDoc文档中查看用于记录元方法等的完整语法

Scopes are class methods, so Rdoc is doing it right. 范围是类方法,因此Rdoc正确地执行它。 I would say that's working as far as Rdoc knows. 我会说,就像Rdoc所知道的那样。

You might have better control using something more extendable like YARD. 您可以更好地控制使用像YARD这样更具扩展性的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM