Skip to content

Commit 553626f

Browse files
authored
feat: db:annotate can now handle sub directories (#16)
1 parent a6dc541 commit 553626f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/cli/commands/new_app/files/db_rake.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ def self.content(app_name, db_name)
168168
169169
models_dir = #{app_name}.root
170170
171-
Dir.glob("app/models/*.rb").each do |model_file|
171+
Dir.glob("app/models/**/*.rb").each do |model_file|
172172
next if !model_file_name.nil? && model_file == model_file_name
173173
174174
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)
177178
table_name = model_klass.table_name
178179
schema = db.schema(table_name)
179180
@@ -184,8 +185,10 @@ def self.content(app_name, db_name)
184185
# Remove existing schema info comments if present
185186
updated_contents = file_contents.sub(/# == Schema Info\\n(.*?)(\\n#\\n)?\\n(?=\\s*class)/m, "")
186187
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")
189192
190193
File.write(model_path, modified_contents)
191194
end

0 commit comments

Comments
 (0)