Skip to content

Commit f1af392

Browse files
committed
Merge pull request #120 from AMekss/master
Fix #113 (for encoding comments generated by magic_encoding gem)
2 parents 3faf06f + 3aac227 commit f1af392

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def annotate_one_file(file_name, info_block, position, options={})
216216
old_columns = old_header && old_header.scan(column_pattern).sort
217217
new_columns = new_header && new_header.scan(column_pattern).sort
218218

219-
encoding = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)/)
219+
encoding = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)/)
220220
encoding_header = old_content.match(encoding).to_s
221221

222222
if old_columns == new_columns && !options[:force]

spec/annotate/annotate_models_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ def annotate_one_file options = {}
359359
Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
360360
end
361361

362+
def encoding_comments_list_each
363+
[
364+
'# encoding: UTF-8',
365+
'# coding: UTF-8',
366+
'# -*- coding: UTF-8 -*-',
367+
'#encoding: utf-8',
368+
'# -*- encoding : utf-8 -*-'
369+
].each{|encoding_comment| yield encoding_comment }
370+
end
371+
362372
it "should annotate the file before the model if position == 'before'" do
363373
annotate_one_file :position => "before"
364374
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
@@ -401,6 +411,20 @@ class Foo::User < ActiveRecord::Base
401411
File.read(model_file_name).should == "#{schema_info}\n#{file_content}"
402412
end
403413

414+
it "should not touch encoding comments" do
415+
encoding_comments_list_each do |encoding_comment|
416+
write_model "user.rb", <<-EOS
417+
#{encoding_comment}
418+
class User < ActiveRecord::Base
419+
end
420+
EOS
421+
422+
annotate_one_file :position => :before
423+
424+
File.open(@model_file_name, &:readline).should == "#{encoding_comment}\n"
425+
end
426+
end
427+
404428
describe "if a file can't be annotated" do
405429
before do
406430
write_model('user.rb', <<-EOS)

0 commit comments

Comments
 (0)