Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Last active October 2, 2020 08:18
Show Gist options
  • Save julianrubisch/57445c9866ddc2b20913045f3830eb2f to your computer and use it in GitHub Desktop.
Save julianrubisch/57445c9866ddc2b20913045f3830eb2f to your computer and use it in GitHub Desktop.
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
@julianrubisch
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment