Skip to content

Commit ac98ea2

Browse files
committed
Fixes updating columns when using alternative formats
Updates `column_pattern` in `AnnotateModels.annotate_one_file` to support RDoc and Yard doc formats. `column_pattern` is responsible for searching for existing annotations in the file - if the patten is not matched in file, annotations are not updated. Since the previous regular expression did not match the Yard (`@!`) or RDoc (`**<attributed>**::`), when attempting to update annotations for a model, it would look like there were no changes since parsing the annotation blocks with the regular expression would return nothing.
1 parent 8666d89 commit ac98ea2

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,18 +351,23 @@ def get_foreign_key_info(klass, options = {})
351351
# :force<Symbol>:: whether to update the file even if it doesn't seem to need it.
352352
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
353353
# :before, :top, :after or :bottom. Default is :before.
354+
# :format_bare<Boolean>:: whether to format annotations using default, bare syntax
355+
# :format_markdown<Boolean>:: whether to format annotations using Markdown syntax
356+
# :format_rdoc<Boolean>:: whether to format annotations using RDoc syntax
357+
# :format_yard<Boolean>:: whether to format annotations using Yard syntax
354358
#
355359
def annotate_one_file(file_name, info_block, position, options = {})
356360
return false unless File.exist?(file_name)
357361
old_content = File.read(file_name)
358362
return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
359363

360364
# Ignore the Schema version line because it changes with each migration
365+
# TODO: may need look at this for Yard and RDoc as well
361366
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
362367
old_header = old_content.match(header_pattern).to_s
363368
new_header = info_block.match(header_pattern).to_s
364369

365-
column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
370+
column_pattern = /^#[\t ]+[\w\*\.\@\!\:]+[\t ]+.+$/
366371
old_columns = old_header && old_header.scan(column_pattern).sort
367372
new_columns = new_header && new_header.scan(column_pattern).sort
368373

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,86 @@ def annotate_one_file(options = {})
25152515
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
25162516
end
25172517
end
2518+
2519+
context 'adding a new field' do
2520+
let(:class_name) { :users }
2521+
let(:primary_key) { :id }
2522+
let(:original_columns) do
2523+
[
2524+
mock_column(primary_key, :integer),
2525+
mock_column(:name, :string)
2526+
]
2527+
end
2528+
2529+
before do
2530+
klass = mock_class(class_name, primary_key, original_columns)
2531+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
2532+
annotate_one_file(options)
2533+
2534+
# confirm we initialized annotaions in file before checking for changes
2535+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2536+
end
2537+
2538+
context 'when option "format_bare" is true' do
2539+
let :options do
2540+
{ format_bare: true }
2541+
end
2542+
2543+
it 'updates the fields list to include the new column' do
2544+
new_column_list = original_columns + [ mock_column(:new_column, :string) ]
2545+
klass = mock_class(class_name, primary_key, new_column_list)
2546+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
2547+
annotate_one_file(options)
2548+
2549+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2550+
end
2551+
end
2552+
2553+
context 'when option "format_yard" is true' do
2554+
let :options do
2555+
{ format_yard: true }
2556+
end
2557+
2558+
it 'updates the fields list to include the new column' do
2559+
new_column_list = original_columns + [ mock_column(:new_column, :string) ]
2560+
klass = mock_class(class_name, primary_key, new_column_list)
2561+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
2562+
annotate_one_file(options)
2563+
2564+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2565+
end
2566+
end
2567+
2568+
context 'when option "format_rdoc" is true' do
2569+
let :options do
2570+
{ format_rdoc: true }
2571+
end
2572+
2573+
it 'updates the fields list to include the new column' do
2574+
new_column_list = original_columns + [ mock_column(:new_column, :string) ]
2575+
klass = mock_class(class_name, primary_key, new_column_list)
2576+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
2577+
annotate_one_file(options)
2578+
2579+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2580+
end
2581+
end
2582+
2583+
context 'when option "format_markdown" is true' do
2584+
let :options do
2585+
{ format_markdown: true }
2586+
end
2587+
2588+
it 'updates the fields list to include the new column' do
2589+
new_column_list = original_columns + [ mock_column(:new_column, :string) ]
2590+
klass = mock_class(class_name, primary_key, new_column_list)
2591+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
2592+
annotate_one_file(options)
2593+
2594+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2595+
end
2596+
end
2597+
end
25182598
end
25192599

25202600
describe 'with existing annotation => :before' do

0 commit comments

Comments
 (0)