Skip to content

Commit 495c71f

Browse files
committed
remove excess newline when annotation puts after code
1 parent 8bd159c commit 495c71f

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

lib/annotate/annotate_models.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module AnnotateModels
55
PREFIX = "== Schema Information"
66
PREFIX_MD = "## Schema Information"
77
END_MARK = "== Schema Information End"
8-
PATTERN = /^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n/
8+
PATTERN = /^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*/
99

1010
# File.join for windows reverse bar compat?
1111
# I dont use windows, can`t test
@@ -160,9 +160,9 @@ def get_schema_info(klass, header, options = {})
160160
if options[:format_rdoc]
161161
info << "#--\n"
162162
info << "# #{END_MARK}\n"
163-
info << "#++\n\n"
163+
info << "#++\n"
164164
else
165-
info << "#\n\n"
165+
info << "#\n"
166166
end
167167
end
168168

@@ -204,7 +204,7 @@ def annotate_one_file(file_name, info_block, position, options={})
204204
return false if(old_content =~ /# -\*- SkipSchemaAnnotations.*\n/)
205205

206206
# Ignore the Schema version line because it changes with each migration
207-
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?\n)/
207+
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
208208
old_header = old_content.match(header_pattern).to_s
209209
new_header = info_block.match(header_pattern).to_s
210210

@@ -238,7 +238,7 @@ def annotate_one_file(file_name, info_block, position, options={})
238238

239239
new_content = options[position].to_s == 'after' ?
240240
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
241-
(encoding_header + info_block + old_content)
241+
(encoding_header + info_block + "\n" + old_content)
242242

243243
File.open(file_name, "wb") { |f| f.puts new_content }
244244
return true

spec/annotate/annotate_models_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def mock_column(name, type, options={})
5151
# id :integer not null, primary key
5252
# name :string(50) not null
5353
#
54-
5554
EOS
5655
end
5756

@@ -69,7 +68,6 @@ def mock_column(name, type, options={})
6968
# id :integer not null
7069
# name :string(50) not null
7170
#
72-
7371
EOS
7472
end
7573

@@ -88,7 +86,6 @@ def mock_column(name, type, options={})
8886
# b_id :integer not null
8987
# name :string(50) not null
9088
#
91-
9289
EOS
9390
end
9491

@@ -107,7 +104,6 @@ def mock_column(name, type, options={})
107104
#--
108105
# #{AnnotateModels::END_MARK}
109106
#++
110-
111107
EOS
112108
end
113109

@@ -177,7 +173,7 @@ class FooWithMacro < ActiveRecord::Base
177173
EOS
178174
check_class_name 'foo_with_macro.rb', 'FooWithMacro'
179175
end
180-
176+
181177
it "should not care about known macros" do
182178
create('foo_with_known_macro.rb', <<-EOS)
183179
class FooWithKnownMacro < ActiveRecord::Base
@@ -347,12 +343,12 @@ def annotate_one_file options = {}
347343

348344
it "should annotate the file before the model if position == 'before'" do
349345
annotate_one_file :position => "before"
350-
File.read(@model_file_name).should == "#{@schema_info}#{@file_content}"
346+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
351347
end
352348

353349
it "should annotate before if given :position => :before" do
354350
annotate_one_file :position => :before
355-
File.read(@model_file_name).should == "#{@schema_info}#{@file_content}"
351+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
356352
end
357353

358354
it "should annotate after if given :position => :after" do
@@ -384,7 +380,7 @@ class Foo::User < ActiveRecord::Base
384380
])
385381
schema_info = AnnotateModels.get_schema_info(klass, "== Schema Info")
386382
AnnotateModels.annotate_one_file(model_file_name, schema_info, :position => :before)
387-
File.read(model_file_name).should == "#{schema_info}#{file_content}"
383+
File.read(model_file_name).should == "#{schema_info}\n#{file_content}"
388384
end
389385

390386
describe "if a file can't be annotated" do

0 commit comments

Comments
 (0)