0
@@ -4,7 +4,7 @@ module AnnotateModels
0
PREFIX = "== Schema Information"
0
# Simple quoting for the default column value
0
when NilClass then "NULL"
0
when TrueClass then "TRUE"
0
@@ -21,7 +21,7 @@ module AnnotateModels
0
# to create a comment block containing a line for
0
# each column. The line contains the column name,
0
# the type (and length), and any optional attributes
0
- def
self.get_schema_info(klass, header)
0
+ def
get_schema_info(klass, header)
0
info = "# #{header}\n#\n"
0
info << "# Table name: #{klass.table_name}\n#\n"
0
@@ -48,7 +48,7 @@ module AnnotateModels
0
# a schema info block (a comment starting
0
# with "Schema as of ..."), remove it first.
0
- def
self.annotate_one_file(file_name, info_block)
0
+ def
annotate_one_file(file_name, info_block)
0
if File.exist?(file_name)
0
content = File.read(file_name)
0
@@ -65,10 +65,10 @@ module AnnotateModels
0
# on the columns and their types) and put it at the front
0
# of the model and fixture source files.
0
- def
self.annotate(klass, header)
0
+ def
annotate(klass, file, header)
0
info = get_schema_info(klass, header)
0
- model_file_name = File.join(MODEL_DIR,
klass.name.underscore + ".rb")
0
+ model_file_name = File.join(MODEL_DIR,
file)
0
annotate_one_file(model_file_name, info)
0
fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
@@ -80,7 +80,7 @@ module AnnotateModels
0
# the underscore or CamelCase versions of model names.
0
# Otherwise we take all the model files in the
0
# app/models directory.
0
- def
self.get_model_names
0
@@ -91,13 +91,25 @@ module AnnotateModels
0
+ # Retrieve the classes belonging to the model names we're asked to process
0
+ # Check for namespaced models in subdirectories as well as models
0
+ # in subdirectories without namespacing.
0
+ def get_model_class(file)
0
+ model = file.gsub(/\.rb$/, '').camelize
0
+ parts = model.split('::')
0
+ parts.inject(Object) {|klass, part| klass.const_get(part) }
0
+ Object.const_get(parts.last)
0
# We're passed a name of things that might be
0
# ActiveRecord models. If we can find the class, and
0
# if its a subclass of ActiveRecord::Base,
0
# then pas it to the associated block
0
- def self.do_annotations
0
version = ActiveRecord::Migrator.current_version rescue 0
0
@@ -105,18 +117,16 @@ module AnnotateModels
0
- self.get_model_names.each do |m|
0
- class_name = m.sub(/\.rb$/,'').camelize
0
+ get_model_files.each do |file|
0
- klass =
class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }0
+ klass =
get_model_class(file)0
if klass < ActiveRecord::Base && !klass.abstract_class?
0
- annotated << class_name
0
- self.annotate(klass, header)
0
+ annotate(klass, file, header)
0
- puts "Unable to annotate #{
class_name}: #{e.message}"
0
+ puts "Unable to annotate #{
file}: #{e.message}"
0
puts "Annotated #{annotated.join(', ')}"
Comments
No one has commented yet.