Created
June 16, 2020 14:24
-
-
Save iftheshoefritz/4bf1980a686123f47e99fa5a21b190ab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
namespace :solargraph do | |
task update_model_definitions: :environment do | |
if Rails.env.development? | |
Rails.application.eager_load! | |
def_file = Rails.root.join('config', 'model_definitions.rb') | |
File.open(def_file, 'w') do |file| | |
ApplicationRecord.descendants.each do |model| | |
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
I had to do
Zeitwerk::Loader.eager_load_all if Zeitwerk
instead ofRails.application.eager_load!
to get it working on Rails 6 app