Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

# 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. 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. So my question is how do I add comments to my file so that :

  • The method appears in my RDoc generated documentation
  • It appears as a public class method (as opposed to public instance method etc)
like image 312
Chris Bailey Avatar asked Sep 01 '11 17:09

Chris Bailey


2 Answers

After some digging around in the RDoc Docs I think I've managed to answer my own question.

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. Documentation the continues as normal on the following lines.

You can see the full syntax for documenting meta-methods etc in the RDoc Documentation

like image 176
Chris Bailey Avatar answered Oct 16 '22 15:10

Chris Bailey


Scopes are class methods, so Rdoc is doing it right. I would say that's working as far as Rdoc knows.

You might have better control using something more extendable like YARD.

like image 31
Robert Rouse Avatar answered Oct 16 '22 14:10

Robert Rouse