Skip to content

Commit 3689d9d

Browse files
committed
DRYing up fileset handling.
1 parent b916307 commit 3689d9d

File tree

2 files changed

+93
-54
lines changed

2 files changed

+93
-54
lines changed

CHANGELOG.rdoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
== 2.5.0
22

3+
* Bugfix: Remove-annotations wasn't properly finding test/spec files, and
4+
wasn't even looking for FactoryGirl factories under the new naming
5+
convention.
36
* Added support for new FactoryGirl naming convention.
47
* Fixed that schema kept prepending additional newlines
58
* Updates to make annotate smarter about when to touch a model

lib/annotate/annotate_models.rb

Lines changed: 90 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ module AnnotateModels
3030
FABRICATORS_TEST_DIR = File.join("test", "fabricators")
3131
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators")
3232

33+
TEST_PATTERNS = [
34+
[UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"],
35+
[SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"],
36+
]
37+
38+
FIXTURE_PATTERNS = [
39+
File.join(FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"),
40+
File.join(FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"),
41+
]
42+
43+
FACTORY_PATTERNS = [
44+
File.join(EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"),
45+
File.join(EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"),
46+
File.join(BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"),
47+
File.join(BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"),
48+
File.join(FACTORY_GIRL_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
49+
File.join(FACTORY_GIRL_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
50+
File.join(FACTORY_GIRL_TEST_DIR, "%TABLE_NAME%.rb"), # (new style)
51+
File.join(FACTORY_GIRL_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style)
52+
File.join(FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"),
53+
File.join(FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"),
54+
]
55+
3356
# Don't show limit (#) on these column types
3457
# Example: show "integer" instead of "integer(4)"
3558
NO_LIMIT_COL_TYPES = ["integer", "boolean"]
@@ -174,7 +197,7 @@ def annotate_one_file(file_name, info_block, options={})
174197
encoding_header = old_content.match(encoding).to_s
175198

176199
if old_columns == new_columns && !options[:force]
177-
false
200+
return false
178201
else
179202

180203
# todo: figure out if we need to extract any logic from this merge chunk
@@ -199,8 +222,10 @@ def annotate_one_file(file_name, info_block, options={})
199222
(encoding_header + info_block + old_content)
200223

201224
File.open(file_name, "wb") { |f| f.puts new_content }
202-
true
225+
return true
203226
end
227+
else
228+
return false
204229
end
205230
end
206231

@@ -211,6 +236,10 @@ def remove_annotation_of_file(file_name)
211236
content.sub!(PATTERN, '')
212237

213238
File.open(file_name, "wb") { |f| f.puts content }
239+
240+
return true
241+
else
242+
return false
214243
end
215244
end
216245

@@ -220,50 +249,50 @@ def remove_annotation_of_file(file_name)
220249
# of the model and fixture source files.
221250
# Returns true or false depending on whether the source
222251
# files were modified.
252+
#
253+
# === Options (opts)
254+
# :position_in_class<Symbol>:: where to place the annotated section in model file
255+
# :position_in_test<Symbol>:: where to place the annotated section in test/spec file(s)
256+
# :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
257+
# :position_in_factory<Symbol>:: where to place the annotated section in factory file
258+
# :exclude_tests<Symbol>:: whether to skip modification of test/spec files
259+
# :exclude_fixtures<Symbol>:: whether to skip modification of fixture files
260+
# :exclude_factories<Symbol>:: whether to skip modification of factory files
261+
#
223262
def annotate(klass, file, header, options={})
224263
info = get_schema_info(klass, header, options)
225-
annotated = false
264+
did_annotate = false
226265
model_name = klass.name.underscore
266+
table_name = klass.table_name
227267
model_file_name = File.join(model_dir, file)
228268

229269
if annotate_one_file(model_file_name, info, options_with_position(options, :position_in_class))
230-
annotated = true
270+
did_annotate = true
231271
end
232272

233273
unless options[:exclude_tests]
234-
[
235-
find_test_file(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
236-
find_test_file(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
237-
].each do |file|
238-
# todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture
239-
if annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
240-
annotated = true
241-
end
242-
end
274+
did_annotate = TEST_PATTERNS.
275+
map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)] }.
276+
map { |pat| find_test_file(*pat) }.
277+
map { |file| annotate_one_file(file, info, options_with_position(options, :position_in_test)) }.
278+
detect { |result| result } || did_annotate
243279
end
244280

245281
unless options[:exclude_fixtures]
246-
[
247-
File.join(FIXTURE_TEST_DIR, "#{klass.table_name}.yml"), # fixture
248-
File.join(FIXTURE_SPEC_DIR, "#{klass.table_name}.yml"), # fixture
249-
File.join(EXEMPLARS_TEST_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
250-
File.join(EXEMPLARS_SPEC_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
251-
File.join(BLUEPRINTS_TEST_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints
252-
File.join(BLUEPRINTS_SPEC_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints
253-
File.join(FACTORY_GIRL_TEST_DIR, "#{klass.table_name}.rb"), # Factory Girl Factories (new style)
254-
File.join(FACTORY_GIRL_SPEC_DIR, "#{klass.table_name}.rb"), # Factory Girl Factories (new style)
255-
File.join(FACTORY_GIRL_TEST_DIR, "#{model_name}_factory.rb"), # Factory Girl Factories (old style)
256-
File.join(FACTORY_GIRL_SPEC_DIR, "#{model_name}_factory.rb"), # Factory Girl Factories (old style)
257-
File.join(FABRICATORS_TEST_DIR, "#{model_name}_fabricator.rb"), # Fabrication Fabricators
258-
File.join(FABRICATORS_SPEC_DIR, "#{model_name}_fabricator.rb"), # Fabrication Fabricators
259-
].each do |file|
260-
if annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
261-
annotated = true
262-
end
263-
end
282+
did_annotate = FIXTURE_PATTERNS.
283+
map { |file| resolve_filename(file, model_name, table_name) }.
284+
map { |file| annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) }.
285+
detect { |result| result } || did_annotate
286+
end
287+
288+
unless options[:exclude_factories]
289+
did_annotate = FACTORY_PATTERNS.
290+
map { |file| resolve_filename(file, model_name, table_name) }.
291+
map { |file| annotate_one_file(file, info, options_with_position(options, :position_in_factory)) }.
292+
detect { |result| result } || did_annotate
264293
end
265294

266-
annotated
295+
return did_annotate
267296
end
268297

269298
# position = :position_in_fixture or :position_in_class
@@ -297,7 +326,7 @@ def get_model_files(options)
297326
puts "No models found in directory '#{model_dir}'."
298327
puts "Either specify models on the command line, or use the --model-dir option."
299328
puts "Call 'annotate --help' for more info."
300-
exit 1;
329+
exit 1
301330
end
302331
end
303332
models
@@ -375,6 +404,7 @@ def annotate_model_file(annotated, file, header, options)
375404
def remove_annotations(options={})
376405
self.model_dir = options[:model_dir] if options[:model_dir]
377406
deannotated = []
407+
deannotated_klass = false
378408
get_model_files(options).each do |file|
379409
begin
380410
klass = get_model_class(file)
@@ -383,27 +413,27 @@ def remove_annotations(options={})
383413

384414
model_name = klass.name.underscore
385415
model_file_name = File.join(model_dir, file)
386-
remove_annotation_of_file(model_file_name)
387-
388-
[
389-
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"),
390-
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"),
391-
File.join(FIXTURE_TEST_DIR, "#{klass.table_name}.yml"), # fixture
392-
File.join(FIXTURE_SPEC_DIR, "#{klass.table_name}.yml"), # fixture
393-
File.join(EXEMPLARS_TEST_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
394-
File.join(EXEMPLARS_SPEC_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
395-
File.join(BLUEPRINTS_TEST_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints
396-
File.join(BLUEPRINTS_SPEC_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints
397-
File.join(FACTORY_GIRL_TEST_DIR, "#{klass.table_name}.rb"), # Factory Girl Factories (new style)
398-
File.join(FACTORY_GIRL_SPEC_DIR, "#{klass.table_name}.rb"), # Factory Girl Factories (new style)
399-
File.join(FACTORY_GIRL_TEST_DIR, "#{model_name}_factory.rb"), # Factory Girl Factories (old style)
400-
File.join(FACTORY_GIRL_SPEC_DIR, "#{model_name}_factory.rb"), # Factory Girl Factories (old style)
401-
File.join(FABRICATORS_TEST_DIR, "#{model_name}_fabricator.rb"), # Fabrication Fabricators
402-
File.join(FABRICATORS_SPEC_DIR, "#{model_name}_fabricator.rb"), # Fabrication Fabricators
403-
].each do |file|
404-
remove_annotation_of_file(file) if File.exist?(file)
405-
end
406-
416+
deannotated_klass = true if(remove_annotation_of_file(model_file_name))
417+
418+
TEST_PATTERNS.
419+
map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)]}.
420+
map { |pat| find_test_file(*pat) }.each do |file|
421+
if(File.exist?(file))
422+
remove_annotation_of_file(file)
423+
deannotated_klass = true
424+
end
425+
end
426+
427+
(FIXTURE_PATTERNS + FACTORY_PATTERNS).
428+
map { |file| resolve_filename(file, model_name, table_name) }.
429+
each do |file|
430+
if File.exist?(file)
431+
remove_annotation_of_file(file)
432+
deannotated_klass = true
433+
end
434+
end
435+
436+
deannotated << klass if(deannotated_klass)
407437
end
408438
rescue Exception => e
409439
puts "Unable to deannotate #{file}: #{e.message}"
@@ -416,5 +446,11 @@ def remove_annotations(options={})
416446
def find_test_file(dir, file_name)
417447
Dir.glob(File.join(dir, "**", file_name)).first || File.join(dir, file_name)
418448
end
449+
450+
def resolve_filename(filename_template, model_name, table_name)
451+
return filename_template.
452+
gsub('%MODEL_NAME%', model_name).
453+
gsub('%TABLE_NAME%', table_name)
454+
end
419455
end
420456
end

0 commit comments

Comments
 (0)