5

Yard でドキュメントを生成していますが、Ruby on Rails プロジェクトのサンプル ドキュメントが見つかりません。短い入門チュートリアルとruby​​doc.infoのいくつかの github プロジェクトしか見つかりませんでしたが、それらはまったく文書化されていません。Railsプロジェクトのコントローラー(アクション付き)、モデル、ルートを適切に文書化する方法を教えてください。

たとえば、私はそのようなコントローラを持っています:

class ArticlesController < ApplicationController
  before_filter :authenticate_user!, except: [:show]
  before_filter :restrict_user, only: [:edit, :update]

  def index
    @articles = current_user.articles.sort_by_rating.
        paginate(:page => params[:page],
                 per_page: 5)
  end

  def new
    @user = User.find(params[:user_id])
    @article = @user.articles.build
  end

  def create
    @user = User.find(params[:user_id])
    @article = @user.articles.build(params[:article])

    if @article.save
      redirect_to  @article, notice: 'Article was successfully created.'
    else
      render action: "new"
    end
  end
end

そしてユーザーモデル:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable :recoverable
  devise :database_authenticatable, :registerable,
         :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body

  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false

  has_many :articles, dependent: :destroy

  letsrate_rater
end
4

2 に答える 2

0

私はこの質問が古いことを知っていますがattr_accessible、からデータ型を取得するプラグインを作成しましたdb/schema.rb。このプラグインは、次のものに直接基づいていますYARD::Handlers::Ruby::AttributeHandler

require 'active_support/inflector'
class AttrAccessibleHandler < YARD::Handlers::Ruby::AttributeHandler
  include ActiveSupport::Inflector
  handles method_call(:attr_accessible)
  namespace_only


  process do
    return if statement.type == :var_ref || statement.type == :vcall
    read, write = true, true
    params = statement.parameters(false).dup
    hashify_object_types(underscore(namespace.name).pluralize)
    # Add all attributes
    validated_attribute_names(params).each do |name|
      namespace.attributes[scope][name] ||= SymbolHash[:read => nil, :write => nil] #Comment this out to see getter and setter methods

      # Show their methods as well
      {:read => name, :write => "#{name}="}.each do |type, meth|
          object_type = attribute_type(name)
          o = MethodObject.new(namespace, meth, scope)
          if type == :write
            o.parameters = [['value', nil]]
            src = "def #{meth}(value)"
            full_src = "#{src}\n  @#{name} = value\nend"
            doc = "Sets the attribute #{name}\n
                   @param [#{object_type}] value the value to set the attribute #{name} to."
          else
            src = "def #{meth}"
            full_src = "#{src}\n  @#{name}\nend"
            doc = "@return [#{object_type}] #{name}\nReturns the value of attribute #{name}"
          end

          o.source ||= full_src
          o.signature ||= src
          o.docstring = doc if o.docstring.blank?(false)
          register(o)

          # Regsiter the object explicitly
          namespace.attributes[scope][name][type] = o #Comment this out to see getter and setter methods
      end
    end
  end
  def hashify_object_types(table)
    table_section = ""
    inside = false
    File.open("db/schema.rb").each do |line|
      if line =~ /create_table "#{table}"/ || inside == true
        inside = true
        table_section << line.chomp
        break if line.chomp.strip == "end"
      end
    end
    @hash_table = convert_keys(Hash[table_section.scan(/t\.(\w+)\s+"(\w+)"/).map(&:reverse)])
  end
  def attribute_type(name)
    @hash_table[name] || "Object"
  end
  def convert_keys(h)
    h.each_with_object({}) do |(k,v),obj|
      obj[k] = v == "datetime" ? "DateTime" : v.capitalize
    end
  end
end

これは、プラグインがルート ディレクトリにあり、ファイルがあることを前提としていdb/schema.rbます。また、テーブル名が下線付きの複数形の文字列ではないクラスも処理しません。BookAuthor #=> book_authors

于 2014-12-10T21:17:19.553 に答える