From 8cf9eef25a00b9077625935dfebe5014ea18b49d Mon Sep 17 00:00:00 2001 From: Ryan Taylor Long Date: Wed, 14 Sep 2011 03:51:12 -0500 Subject: [PATCH 1/2] Implement FactoryGirl (#47) --- lib/annotate/annotate_models.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 2365b6829..200c8213a 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -9,11 +9,15 @@ class << self # I dont use windows, can`t test UNIT_TEST_DIR = File.join("test", "unit" ) 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 EXEMPLARS_TEST_DIR = File.join("test", "exemplars") EXEMPLARS_SPEC_DIR = File.join("spec", "exemplars") # Machinist http://github.com/notahat/machinist BLUEPRINTS_DIR = File.join("test", "blueprints") + # FactoryGirl http://github.com/thoughtbot/factory_girl + FACTORIES_TEST_DIR = File.join("test", "factories") + FACTORIES_SPEC_DIR = File.join("spec", "factories") + def model_dir @model_dir || "app/models" @@ -119,12 +123,13 @@ def annotate_one_file(file_name, info_block, options={}) old_content = File.read(file_name) # Ignore the Schema version line because it changes with each migration - header = Regexp.new(/(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?\n)/) - old_header = old_content.match(header).to_s - new_header = info_block.match(header).to_s - - old_columns = old_header && old_header.scan(/#[\t\s]+([\w\d]+)[\t\s]+\:([\d\w]+)/).sort - new_columns = new_header && new_header.scan(/#[\t\s]+([\w\d]+)[\t\s]+\:([\d\w]+)/).sort + header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?\n)/ + old_header = old_content.match(header_pattern).to_s + new_header = info_block.match(header_pattern).to_s + + column_pattern = /^#[\t ]+\w+[\t ]+:\w+/ + old_columns = old_header && old_header.scan(column_pattern).sort + new_columns = new_header && new_header.scan(column_pattern).sort if old_columns == new_columns false @@ -185,6 +190,8 @@ def annotate(klass, file, header,options={}) File.join(EXEMPLARS_TEST_DIR, "#{model_name}_exemplar.rb"), # Object Daddy File.join(EXEMPLARS_SPEC_DIR, "#{model_name}_exemplar.rb"), # Object Daddy File.join(BLUEPRINTS_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints + File.join(FACTORIES_TEST_DIR, "#{model_name.pluralize}.rb"), # FactoryGirl Factories + File.join(FACTORIES_SPEC_DIR, "#{model_name.pluralize}.rb"), # FactoryGirl Factories ].each do |file| annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) end From a645dc4e8997783bf9f05f85f0943ecdf5e119d8 Mon Sep 17 00:00:00 2001 From: Ryan Taylor Long Date: Wed, 14 Sep 2011 04:06:52 -0500 Subject: [PATCH 2/2] Count a model as 'annotated' if any of its tests/fixtures are annotated --- lib/annotate/annotate_models.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 200c8213a..c10d08b8c 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -181,7 +181,9 @@ def annotate(klass, file, header,options={}) File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec ].each do |file| # todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture - annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) + if annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) + annotated = true + end end end @@ -193,13 +195,17 @@ def annotate(klass, file, header,options={}) File.join(FACTORIES_TEST_DIR, "#{model_name.pluralize}.rb"), # FactoryGirl Factories File.join(FACTORIES_SPEC_DIR, "#{model_name.pluralize}.rb"), # FactoryGirl Factories ].each do |file| - annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) + if annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) + annotated = true + end end 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(options, :position_in_fixture)) + if annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture)) + annotated = true + end end end end