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 == AnnotateModels
Add a comment summarizing the current schema to the bottom of each Add a comment summarizing the current schema to the bottom of each
......
...@@ -3,7 +3,7 @@ module AnnotateModels ...@@ -3,7 +3,7 @@ module AnnotateModels
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = "== Schema Info"
PREFIX = "== Schema Information" PREFIX = "== Schema Information"
FIXTURE_DIRS = ["test/fixtures","spec/fixtures"] FIXTURE_DIRS = ["test/fixtures","spec/fixtures"]
# File.join for windows reverse bar compat? # File.join for windows reverse bar compat?
# I dont use windows, can`t test # I dont use windows, can`t test
...@@ -11,11 +11,11 @@ module AnnotateModels ...@@ -11,11 +11,11 @@ module AnnotateModels
SPEC_MODEL_DIR = File.join("spec", "models") SPEC_MODEL_DIR = File.join("spec", "models")
# Object Daddy http://github.com/flogic/object_daddy/tree/master # Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_DIR = File.join("spec", "exemplars") EXEMPLARS_DIR = File.join("spec", "exemplars")
def model_dir def model_dir
@model_dir || "app/models" @model_dir || "app/models"
end end
def model_dir=(dir) def model_dir=(dir)
@model_dir = dir @model_dir = dir
end end
...@@ -55,13 +55,13 @@ module AnnotateModels ...@@ -55,13 +55,13 @@ module AnnotateModels
else else
col_type << "(#{col.limit})" if col.limit col_type << "(#{col.limit})" if col.limit
end end
# Check out if we got a geometric column # Check out if we got a geometric column
# and print the type and SRID # and print the type and SRID
if col.respond_to?(:geometry_type) if col.respond_to?(:geometry_type)
attrs << "#{col.geometry_type}, #{col.srid}" 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" info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end end
...@@ -92,7 +92,7 @@ module AnnotateModels ...@@ -92,7 +92,7 @@ module AnnotateModels
# Returns true or false depending on whether the file was modified. # Returns true or false depending on whether the file was modified.
# #
# === Options (opts) # === 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". # "before" or "after". Default is "before".
# :position_in_class<Symbol>:: where to place the annotated section in model file # :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 # :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
...@@ -105,7 +105,8 @@ module AnnotateModels ...@@ -105,7 +105,8 @@ module AnnotateModels
header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/) header = Regexp.new(/(^# Table name:.*?\n(#.*\n)*\n)/)
old_header = old_content.match(header).to_s old_header = old_content.match(header).to_s
new_header = info_block.match(header).to_s new_header = info_block.match(header).to_s
old_header = ""
if old_header == new_header if old_header == new_header
false false
else else
...@@ -120,13 +121,13 @@ module AnnotateModels ...@@ -120,13 +121,13 @@ module AnnotateModels
end end
end end
end end
def remove_annotation_of_file(file_name) def remove_annotation_of_file(file_name)
if File.exist?(file_name) if File.exist?(file_name)
content = File.read(file_name) content = File.read(file_name)
content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
File.open(file_name, "wb") { |f| f.puts content } File.open(file_name, "wb") { |f| f.puts content }
end end
end end
...@@ -137,35 +138,43 @@ module AnnotateModels ...@@ -137,35 +138,43 @@ module AnnotateModels
# of the model and fixture source files. # of the model and fixture source files.
# Returns true or false depending on whether the source # Returns true or false depending on whether the source
# files were modified. # files were modified.
def annotate(klass, file, header,options={}) def annotate(klass, file, header,options={})
info = get_schema_info(klass, header, options) info = get_schema_info(klass, header, options)
annotated = false annotated = false
model_name = klass.name.underscore model_name = klass.name.underscore
model_file_name = File.join(model_dir, file) 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 annotated = true
end 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'] unless ENV['exclude_fixtures']
annotate_fixtures(info, klass, options) unless ENV['exclude_fixtures'] file = File.join(EXEMPLARS_DIR, "#{model_name}_exemplar.rb") # Object Daddy
annotated annotate_one_file(file, info, options_with_position(:position_in_fixture))
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
def annotate_fixtures(info, klass, options) FIXTURE_DIRS.each do |dir|
FIXTURE_DIRS.each do |dir| fixture_file_name = File.join(dir,klass.table_name + ".yml")
fixture_file_name = File.join(dir, klass.table_name + ".yml") if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options.merge(:position=>(options[:position_in_fixture] || options[:position]))) if File.exist?(fixture_file_name) annotate_one_file(fixture_file_name, info, options_with_position(:position_in_fixture))
end
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 end
# Return a list of the model files to annotate. If we have # Return a list of the model files to annotate. If we have
...@@ -191,7 +200,7 @@ module AnnotateModels ...@@ -191,7 +200,7 @@ module AnnotateModels
end end
models models
end end
# Retrieve the classes belonging to the model names we're asked to process # Retrieve the classes belonging to the model names we're asked to process
# Check for namespaced models in subdirectories as well as models # Check for namespaced models in subdirectories as well as models
# in subdirectories without namespacing. # in subdirectories without namespacing.
...@@ -223,9 +232,9 @@ module AnnotateModels ...@@ -223,9 +232,9 @@ module AnnotateModels
version = ActiveRecord::Migrator.current_version rescue 0 version = ActiveRecord::Migrator.current_version rescue 0
if version > 0 if version > 0
header << "\n# Schema version: #{version}" header << "\n# Schema version: #{version}"
end end
end end
if options[:model_dir] if options[:model_dir]
self.model_dir = options[:model_dir] self.model_dir = options[:model_dir]
end end
...@@ -235,7 +244,7 @@ module AnnotateModels ...@@ -235,7 +244,7 @@ module AnnotateModels
begin begin
klass = get_model_class(file) klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
if annotate(klass, file, header,options) if annotate(klass, file, header, options)
annotated << klass annotated << klass
end end
end end
...@@ -250,7 +259,7 @@ module AnnotateModels ...@@ -250,7 +259,7 @@ module AnnotateModels
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}" puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end end
end end
def remove_annotations(options={}) def remove_annotations(options={})
if options[:model_dir] if options[:model_dir]
puts "removing" puts "removing"
...@@ -262,7 +271,7 @@ module AnnotateModels ...@@ -262,7 +271,7 @@ module AnnotateModels
klass = get_model_class(file) klass = get_model_class(file)
if klass < ActiveRecord::Base && !klass.abstract_class? if klass < ActiveRecord::Base && !klass.abstract_class?
deannotated << klass deannotated << klass
model_file_name = File.join(model_dir, file) model_file_name = File.join(model_dir, file)
remove_annotation_of_file(model_file_name) 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