Gem 给大家推荐一个好用的 项目代码分析、模型视图化 gem

18810625123 · June 08, 2017 · Last by zhou1_zhen2 replied at April 19, 2018 · 2187 hits

介绍:

  • 可以分析一个 Rails 项目的代码
  • 每个 model 实例,会有一个print方法可以调用,这样可以查看这个模型拥有哪些关联关系、路由、字段、对应的控制器、对应的视图 等信息

安装:

1、添加 Gemfile

gem 'ld'

2、运行 bundle install

bundle install

配置

  • 在 config/application.rb 的最后添加如下代码,这样可以让每个 model 实例都具有查看自己信息的能力,使用方法任意模型实例.print :接参数
require "ld"
class ActiveRecord::Base
  # 为所有模型添加一个实例方法 print
  def print type = :relations
    @@project ||= Ld::Project.new
    @@project.print self.class.to_s.underscore, type
    self
  end
end

用法:

  • 使用Ld::Project.new可以分析当前 Rails 项目
project = Ld::Project.new(Rails.root.to_s)
project.print :user, :relations
project.print :user, :fields
project.print :user, :views
project.print :user, :controllers
project.print :user, :routes
  • 如果在 ActiveRecord::Base 中添加了实例方法 print,将可以这样更方便的使用
# 效果同上
user =  User.first
user.print :relations
user.print :fields
user.print :views
user.print :controllers
user.print :routes
  • 效果展示

rubygems.org 链接

https://rubygems.org/gems/ld

github 项目源码 与文档

https://github.com/18810625123/ld

联系方式,技术支持

  • QQ 370182106
  • 微信 18810625123
  • star 我的 github 项目可在更新 gem 时会收到通知。觉得好用的朋友们别忘了分享给小伙伴们哦 😄

今天重构了一下

User.columns_hash 是能获取表结构化信息,至于 view 应该是遍历了吧

You need to Sign in before reply, if you don't have an account, please Sign up first.