Last active
October 2, 2020 08:18
-
-
Save julianrubisch/57445c9866ddc2b20913045f3830eb2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
namespace :solargraph do | |
task update_model_definitions: :environment do | |
if Rails.env.development? | |
Rails.application.eager_load! unless Rails.application.config.cache_classes | |
Zeitwerk::Loader.eager_load_all if defined?(Zeitwerk) | |
ApplicationRecord.descendants.each do |model| | |
def_file = Rails.root.join('config', 'model_definitions', "#{model.name.underscore}.rb") | |
FileUtils.mkdir_p File.dirname(def_file) | |
File.open(def_file, 'w') do |file| | |
file << "class #{model.name}\n" | |
klass = model.name.constantize | |
klass.attribute_names.each do |name| | |
file << "# @!attribute [rw] #{name}\n" | |
file << "# @return [#{ruby_type[klass.type_for_attribute(name).type]}]\n" | |
end | |
file << "end\n" | |
end | |
end | |
end | |
end | |
end | |
def ruby_type | |
{ | |
array: 'Array', | |
boolean: 'Boolean', | |
date: 'Date', | |
datetime: 'DateTime', | |
decimal: 'Decimal', | |
float: 'Float', | |
integer: 'Integer', | |
string: 'String', | |
text: 'String', | |
time: 'Time', | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an amended version of https://gist.github.com/iftheshoefritz/f13bd3b0f23f3d72a32121d0e1cab4e8 🙌 @iftheshoefritz