diff --git a/README.rdoc b/README.rdoc index 3a18f360b..f7ff655c9 100644 --- a/README.rdoc +++ b/README.rdoc @@ -167,6 +167,10 @@ you can do so with a simple environment variable, instead of editing the Place the annotations at the top (before) or the bottom (after) of the routes.rb file --ps, --position-in-serializer [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of the serializer files + --w, --wrapper STR Wrap annotation with the text passed as parameter. + If --w option is used, the same text will be used as opening and closing + --wo, --wrapper-open STR Annotation wrapper opening. + --wc, --wrapper-close STR Annotation wrapper closing -r, --routes Annotate routes.rb with the output of 'rake routes' -v, --version Show the current version of this gem -m, --show-migration Include the migration version number in the annotation diff --git a/bin/annotate b/bin/annotate index b4fb90f76..3f1ca054a 100755 --- a/bin/annotate +++ b/bin/annotate @@ -78,6 +78,19 @@ OptionParser.new do |opts| has_set_position['position_in_serializer'] = true end + opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.', + 'If --w option is used, the same text will be used as opening and closing') do |p| + ENV['wrapper'] = p + end + + opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p| + ENV['wrapper_open'] = p + end + + opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p| + ENV['wrapper_close'] = p + end + opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do target = { diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 90f2553aa..2a5d8c71d 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -253,6 +253,9 @@ def annotate_one_file(file_name, info_block, position, options={}) new_content = old_content.sub(PATTERN, "\n" + info_block) end + wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : "" + wrapper_close = options[:wrapper_close] ? "\n# #{options[:wrapper_close]}" : "" + wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}" # if there *was* no old schema info (no substitution happened) or :force was passed, # we simply need to insert it in correct position if new_content == old_content || options[:force] @@ -260,8 +263,8 @@ def annotate_one_file(file_name, info_block, position, options={}) old_content.sub!(PATTERN, '') new_content = %w(after bottom).include?(options[position].to_s) ? - (encoding_header + (old_content.rstrip + "\n\n" + info_block)) : - (encoding_header + info_block + "\n" + old_content) + (encoding_header + (old_content.rstrip + "\n\n" + wrapped_info_block)) : + (encoding_header + wrapped_info_block + "\n" + old_content) end File.open(file_name, "wb") { |f| f.puts new_content } diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index a0034f9be..0a80af51a 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -31,6 +31,8 @@ task :annotate_models => :environment do options[:sort] = Annotate.true?(ENV['sort']) options[:force] = Annotate.true?(ENV['force']) options[:trace] = Annotate.true?(ENV['trace']) + options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper']) + options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper']) AnnotateModels.do_annotations(options) end diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index 8cd298477..27d228877 100644 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -455,6 +455,11 @@ def encoding_comments_list_each expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") end + it 'should wrap annotation if wrapper is specified' do + annotate_one_file :wrapper_open => 'START', :wrapper_close => 'END' + expect(File.read(@model_file_name)).to eq("# START\n#{@schema_info}\n# END\n#{@file_content}") + end + describe "with existing annotation => :before" do before do annotate_one_file :position => :before