@@ -30,6 +30,29 @@ module AnnotateModels
30
30
FABRICATORS_TEST_DIR = File . join ( "test" , "fabricators" )
31
31
FABRICATORS_SPEC_DIR = File . join ( "spec" , "fabricators" )
32
32
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
+
33
56
# Don't show limit (#) on these column types
34
57
# Example: show "integer" instead of "integer(4)"
35
58
NO_LIMIT_COL_TYPES = [ "integer" , "boolean" ]
@@ -174,7 +197,7 @@ def annotate_one_file(file_name, info_block, options={})
174
197
encoding_header = old_content . match ( encoding ) . to_s
175
198
176
199
if old_columns == new_columns && !options [ :force ]
177
- false
200
+ return false
178
201
else
179
202
180
203
# 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={})
199
222
( encoding_header + info_block + old_content )
200
223
201
224
File . open ( file_name , "wb" ) { |f | f . puts new_content }
202
- true
225
+ return true
203
226
end
227
+ else
228
+ return false
204
229
end
205
230
end
206
231
@@ -211,6 +236,10 @@ def remove_annotation_of_file(file_name)
211
236
content . sub! ( PATTERN , '' )
212
237
213
238
File . open ( file_name , "wb" ) { |f | f . puts content }
239
+
240
+ return true
241
+ else
242
+ return false
214
243
end
215
244
end
216
245
@@ -220,50 +249,50 @@ def remove_annotation_of_file(file_name)
220
249
# of the model and fixture source files.
221
250
# Returns true or false depending on whether the source
222
251
# 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
+ #
223
262
def annotate ( klass , file , header , options = { } )
224
263
info = get_schema_info ( klass , header , options )
225
- annotated = false
264
+ did_annotate = false
226
265
model_name = klass . name . underscore
266
+ table_name = klass . table_name
227
267
model_file_name = File . join ( model_dir , file )
228
268
229
269
if annotate_one_file ( model_file_name , info , options_with_position ( options , :position_in_class ) )
230
- annotated = true
270
+ did_annotate = true
231
271
end
232
272
233
273
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
243
279
end
244
280
245
281
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
264
293
end
265
294
266
- annotated
295
+ return did_annotate
267
296
end
268
297
269
298
# position = :position_in_fixture or :position_in_class
@@ -297,7 +326,7 @@ def get_model_files(options)
297
326
puts "No models found in directory '#{ model_dir } '."
298
327
puts "Either specify models on the command line, or use the --model-dir option."
299
328
puts "Call 'annotate --help' for more info."
300
- exit 1 ;
329
+ exit 1
301
330
end
302
331
end
303
332
models
@@ -375,6 +404,7 @@ def annotate_model_file(annotated, file, header, options)
375
404
def remove_annotations ( options = { } )
376
405
self . model_dir = options [ :model_dir ] if options [ :model_dir ]
377
406
deannotated = [ ]
407
+ deannotated_klass = false
378
408
get_model_files ( options ) . each do |file |
379
409
begin
380
410
klass = get_model_class ( file )
@@ -383,27 +413,27 @@ def remove_annotations(options={})
383
413
384
414
model_name = klass . name . underscore
385
415
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 )
407
437
end
408
438
rescue Exception => e
409
439
puts "Unable to deannotate #{ file } : #{ e . message } "
@@ -416,5 +446,11 @@ def remove_annotations(options={})
416
446
def find_test_file ( dir , file_name )
417
447
Dir . glob ( File . join ( dir , "**" , file_name ) ) . first || File . join ( dir , file_name )
418
448
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
419
455
end
420
456
end
0 commit comments