0
MODEL_DIR = "app/models"
0
- FIXTURE_DIR
= "test/fixtures"0
+ FIXTURE_DIR
S = ["test/fixtures","spec/fixtures"]0
PREFIX = "== Schema Information"
0
# Simple quoting for the default column value
0
@@ -49,7 +49,7 @@ module AnnotateModels
0
# a schema info block (a comment starting
0
# with "Schema as of ..."), remove it first.
0
- def annotate_one_file(file_name, info_block
)
0
+ def annotate_one_file(file_name, info_block
, options={})
0
if File.exist?(file_name)
0
content = File.read(file_name)
0
@@ -57,7 +57,18 @@ module AnnotateModels
0
content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
0
- File.open(file_name, "w") { |f| f.puts info_block + content }
0
+ new_content = options[:position] == "before" ? (info_block + content) : (content + info_block)
0
+ File.open(file_name, "w") { |f| f.puts new_content }
0
+ def remove_annotation_of_file(file_name)
0
+ if File.exist?(file_name)
0
+ content = File.read(file_name)
0
+ content.sub!(/^# #{PREFIX}.*?\n(#.*\n)*\n/, '')
0
+ File.open(file_name, "w") { |f| f.puts content }
0
@@ -66,14 +77,16 @@ 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 annotate(klass, file, header
)
0
+ def annotate(klass, file, header
,options={})
0
info = get_schema_info(klass, header)
0
model_file_name = File.join(MODEL_DIR, file)
0
- annotate_one_file(model_file_name, info
)
0
+ annotate_one_file(model_file_name, info
, options.merge(:position=>(options[:position_in_class] || options[:position])))
0
- fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
- annotate_one_file(fixture_file_name, info)
0
+ FIXTURE_DIRS.each do |dir|
0
+ fixture_file_name = File.join(dir,klass.table_name + ".yml")
0
+ annotate_one_file(fixture_file_name, info, options.merge(:position=>(options[:position_in_fixture] || options[:position]))) if File.exist?(fixture_file_name)
0
# Return a list of the model files to annotate. If we have
0
@@ -84,7 +97,7 @@ module AnnotateModels
0
+
models.reject!{|m| m.starts_with?("position=")}0
Dir.chdir(MODEL_DIR) do
0
models = Dir["**/*.rb"]
0
@@ -110,7 +123,7 @@ module AnnotateModels
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 do_annotations
(options={})0
version = ActiveRecord::Migrator.current_version rescue 0
0
@@ -123,7 +136,7 @@ module AnnotateModels
0
klass = get_model_class(file)
0
if klass < ActiveRecord::Base && !klass.abstract_class?
0
- annotate(klass, file, header
)
0
+ annotate(klass, file, header
,options)
0
puts "Unable to annotate #{file}: #{e.message}"
0
@@ -131,5 +144,28 @@ module AnnotateModels
0
puts "Annotated #{annotated.join(', ')}"
0
+ def remove_annotations
0
+ get_model_files.each do |file|
0
+ klass = get_model_class(file)
0
+ if klass < ActiveRecord::Base && !klass.abstract_class?
0
+ model_file_name = File.join(MODEL_DIR, file)
0
+ remove_annotation_of_file(model_file_name)
0
+ FIXTURE_DIRS.each do |dir|
0
+ fixture_file_name = File.join(dir,klass.table_name + ".yml")
0
+ remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
0
+ puts "Unable to annotate #{file}: #{e.message}"
0
+ puts "Removed annotaion from: #{deannotated.join(', ')}"
0
\ No newline at end of file
Comments
No one has commented yet.