@@ -168,12 +168,13 @@ def self.content(app_name, db_name)
168
168
169
169
models_dir = #{ app_name } .root
170
170
171
- Dir.glob("app/models/*.rb").each do |model_file|
171
+ Dir.glob("app/models/**/* .rb").each do |model_file|
172
172
next if !model_file_name.nil? && model_file == model_file_name
173
173
174
174
model_path = File.expand_path(model_file, models_dir)
175
- model_name = Zeitwerk::Inflector.new.camelize(File.basename(model_file, ".rb"), model_path)
176
- model_klass = Object.const_get(model_name)
175
+ modules = model_file.gsub("app/models/", "").gsub(".rb", "").split("/").map { |mod| Zeitwerk::Inflector.new.camelize(mod, model_path) }
176
+ const_name = modules.join("::")
177
+ model_klass = Object.const_get(const_name)
177
178
table_name = model_klass.table_name
178
179
schema = db.schema(table_name)
179
180
@@ -184,8 +185,10 @@ def self.content(app_name, db_name)
184
185
# Remove existing schema info comments if present
185
186
updated_contents = file_contents.sub(/# == Schema Info\\ n(.*?)(\\ n#\\n)?\\n(?=\\s*class)/m, "")
186
187
187
- # Insert the new schema comments before the class definition
188
- modified_contents = updated_contents.sub(/(\A |\\ n)(class \# {model_name})/m, "\\ \\ 1\# {schema_comments}\\ n\\ n\\ \\ 2")
188
+ # Insert the new schema comments before the module/class definition
189
+ first_const = modules.first
190
+ first_module_or_class = modules.count == 1 ? "class #{ first_const } " : "module #{ first_const } "
191
+ modified_contents = updated_contents.sub(/(A|\n )(#{ first_module_or_class } )/m, "\\ 1#{ schema_comments } \n \n \\ 2")
189
192
190
193
File.write(model_path, modified_contents)
191
194
end
0 commit comments