Commit 49e5fce3 by Alex Chaffee

Merge remote branch 'jacqui/master'

* jacqui/master: Pass options to annotate_one_file for specs/tests/exemplars as well Refactoring & making work with Neal's 'exclude' option too.
parents 0f978c57 597c862d
== Fork of AnnotateModels
I forked AnnotateModels to fix what I'm guessing is a bug - options (including position) were not being passed when annotating spec, test, or exemplar files. This resulted in my models having annotations at the bottom of my models but at the top of my specs, which was frustrating.
This version of AnnotateModels will pass all options to the method annotate_one_file when annotating models and specs (or tests, or exemplars, whatever you use). Otherwise this version is exactly the same as the original!
== AnnotateModels
Add a comment summarizing the current schema to the bottom of each
......
......@@ -3,7 +3,7 @@ module AnnotateModels
# Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information"
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
# File.join for windows reverse bar compat?
# I dont use windows, can`t test
......@@ -11,11 +11,11 @@ module AnnotateModels
SPEC_MODEL_DIR = File.join("spec", "models")
# Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_DIR = File.join("spec", "exemplars")
def model_dir
@model_dir || "app/models"
end
def model_dir=(dir)
@model_dir = dir
end
......@@ -55,13 +55,13 @@ module AnnotateModels
else
col_type << "(#{col.limit})" if col.limit
end
# Check out if we got a geometric column
# and print the type and SRID
if col.respond_to?(:geometry_type)
attrs << "#{col.geometry_type}, #{col.srid}"
end
end
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end
......@@ -92,7 +92,7 @@ module AnnotateModels
# Returns true or false depending on whether the file was modified.
#
# === Options (opts)
# :position<Symbol>:: where to place the annotated section in fixture or model file,
# :position<Symbol>:: where to place the annotated section in fixture or model file,
# "before" or "after". Default is "before".
# :position_in_class<Symbol>:: where to place the annotated section in model file
# :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
......@@ -105,7 +105,8 @@ module AnnotateModels
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
old_header = old_content.match(header).to_s
new_header = info_block.match(header).to_s
old_header = ""
if old_header == new_header
false
else
......@@ -120,13 +121,13 @@ module AnnotateModels
end
end
end
def remove_annotation_of_file(file_name)
if File.exist?(file_name)
content = File.read(file_name)
content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
File.open(file_name, "wb") { |f| f.puts content }
end
end
......@@ -137,35 +138,43 @@ module AnnotateModels
# of the model and fixture source files.
# Returns true or false depending on whether the source
# files were modified.
def annotate(klass, file, header,options={})
info = get_schema_info(klass, header, options)
annotated = false
model_name = klass.name.underscore
model_file_name = File.join(model_dir, file)
if annotate_one_file(model_file_name, info, options.merge(
:position=>(options[:position_in_class] || options[:position])))
if annotate_one_file(model_file_name, info, options_with_position(:position_in_class))
annotated = true
end
unless ENV['exclude_tests']
[
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
].each do |file|
annotate_one_file(file, info, options_with_position(:position_in_fixture))
end
end
annotate_tests(info, model_name) unless ENV['exclude_tests']
annotate_fixtures(info, klass, options) unless ENV['exclude_fixtures']
annotated
end
def annotate_tests(info, model_name)
[
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
].each { |file| annotate_one_file(file, info) }
end
unless ENV['exclude_fixtures']
file = File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb") # Object Daddy
annotate_one_file(file, info, options_with_position(:position_in_fixture))
def annotate_fixtures(info, klass, options)
FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir, klass.table_name + ".yml")
annotate_one_file(fixture_file_name, info, options.merge(:position=>(options[:position_in_fixture] || options[:position]))) if File.exist?(fixture_file_name)
FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,klass.table_name + ".yml")
if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options_with_position(:position_in_fixture))
end
end
end
annotated
end
# position = :position_in_fixture or :position_in_class
def options_with_position(position_in)
options.merge(:position=>(options[position_in] || options[:position]))
end
# Return a list of the model files to annotate. If we have
......@@ -191,7 +200,7 @@ module AnnotateModels
end
models
end
# Retrieve the classes belonging to the model names we're asked to process
# Check for namespaced models in subdirectories as well as models
# in subdirectories without namespacing.
......@@ -223,9 +232,9 @@ module AnnotateModels
version = ActiveRecord::Migrator.current_version rescue 0
if version > 0
header << "\n# Schema version: #{version}"
end
end
end
if options[:model_dir]
self.model_dir = options[:model_dir]
end
......@@ -235,7 +244,7 @@ module AnnotateModels
begin
klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class?
if annotate(klass, file, header,options)
if annotate(klass, file, header, options)
annotated << klass
end
end
......@@ -250,7 +259,7 @@ module AnnotateModels
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end
end
def remove_annotations(options={})
if options[:model_dir]
puts "removing"
......@@ -262,7 +271,7 @@ module AnnotateModels
klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class?
deannotated << klass
model_file_name = File.join(model_dir, file)
remove_annotation_of_file(model_file_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment