Skip to content

[WIP] Fix updating annotation when formatted for Yard docs #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ def get_foreign_key_info(klass, options = {})
# :force<Symbol>:: whether to update the file even if it doesn't seem to need it.
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
# :before, :top, :after or :bottom. Default is :before.
# :format_rdoc<Boolean>:: whether to format annotations using RDoc syntax
# :format_yard<Boolean>:: whether to format annotations using Yard syntax
# :format_bare<Boolean>:: whether to format annotations using default, bare syntax
#
def annotate_one_file(file_name, info_block, position, options = {})
return false unless File.exist?(file_name)
Expand All @@ -515,7 +518,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
old_header = old_content.match(header_pattern).to_s
new_header = info_block.match(header_pattern).to_s

column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
column_pattern = column_pattern_for(format_from(options))
old_columns = old_header && old_header.scan(column_pattern).sort
new_columns = new_header && new_header.scan(column_pattern).sort

Expand Down Expand Up @@ -996,6 +999,40 @@ def ignored_translation_table_colums(klass)
foreign_column_name
]
end

##
# The format of documentation to use based on the options
#
# @param [Hash] options
# @option options [Boolean] :format_rdoc
# @option options [Boolean] :format_yard
# @option options [Boolean] :format_bare
# @return [Symbol]
def format_from(options)
if options[:format_rdoc]
:rdoc
elsif options[:format_yard]
:yard
else
:bare
end
end

##
# Regular expression pattern used to search for a column's annotation
#
# @param [Symbol] format
# @return [RegularExpression]
def column_pattern_for(format)
case format
when :yard then /^#[\t ]+@[!]?[\w]+[\s]+.*/
# Default behavior matches the column pattern used by`:bare`. Instead
# of explicitly matching `:bare` `else` is used as a fallback to match
# original functionality.
else /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
end
end

end

class BadModelFileError < LoadError
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,39 @@ def annotate_one_file(options = {})
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
end
end

context 'adding a new field' do
let(:class_name) { :users }
let(:primary_key) { :id }
let(:original_columns) do
[
mock_column(primary_key, :integer),
mock_column(:name, :string)
]
end

before do
klass = mock_class(class_name, primary_key, original_columns)
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
puts "Format yard?: #{options[:format_yard]}"
annotate_one_file(options)
end

context 'when option "format_yard" is true' do
let :options do
{ format_yard: true }
end

it 'update the fields list to include the new column' do
new_column_list = original_columns + [ mock_column(:new_column, :string) ]
klass = mock_class(class_name, primary_key, new_column_list)
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', options)
annotate_one_file(options)

expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
end
end
end
end

describe 'with existing annotation => :before' do
Expand Down