Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rails documentation generator
Rails.application.eager_load!
models_to_document = ApplicationRecord.descendants.map(&:to_s).uniq.select { |m|
ActiveRecord::Base.connection.table_exists? m.underscore.pluralize
}.map(&:constantize)
model_descriptions ||= {}
models_to_document.each do |model|
model_referenced_by = models_to_document
.select { |m| m.column_names.include?("#{ model.table_name.singularize }_id") }
.map(&:to_s)
.join(', ')
model_references_to = model
.column_names
.select { |cn| cn =~ /_id/ }
.map { |r| r.gsub(/_id/, '') }
.map(&:camelize)
.join(', ')
model_ancestors = model.ancestors.select { |k| k < ApplicationRecord }
model_descendants = model.descendants
model_hierarchy = (model_ancestors + model_descendants).map(&:to_s)
model_column_names = model.column_names.reject { |cn| cn =~ /\Aid|created_at|updated_at|uuid/ }
model_description = model_descriptions[model.to_s]
puts "### #{ model.to_s }"
puts
print "```"
"#{ model_hierarchy.each { |model_name| puts model_name } }" if model_hierarchy.length > 1
puts
puts "#{ model.to_s } <- #{ model_referenced_by }" if model_referenced_by.present?
puts "#{ model.to_s } -> #{ model_references_to }" if model_references_to.present?
puts
model_column_names.each { |cn| puts " |#{ cn }" }
puts "```"
puts
puts "#{ model_description }"
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment