Skip to content

Commit 3faf06f

Browse files
committed
Merge pull request #116 from Mik-die/optional_ending_lines
Remove excessive newline when annotation is put after code
2 parents f509941 + 495c71f commit 3faf06f

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
@@ -164,9 +164,9 @@ def get_schema_info(klass, header, options = {})
164164
if options[:format_rdoc]
165165
info << "#--\n"
166166
info << "# #{END_MARK}\n"
167-
info << "#++\n\n"
167+
info << "#++\n"
168168
else
169-
info << "#\n\n"
169+
info << "#\n"
170170
end
171171
end
172172

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

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

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

243243
new_content = options[position].to_s == 'after' ?
244244
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
245-
(encoding_header + info_block + old_content)
245+
(encoding_header + info_block + "\n" + old_content)
246246

247247
File.open(file_name, "wb") { |f| f.puts new_content }
248248
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

@@ -89,7 +87,6 @@ def mock_column(name, type, options={})
8987
# b_id :integer not null
9088
# name :string(50) not null
9189
#
92-
9390
EOS
9491
end
9592
it "should get schema info with enum type " do
@@ -125,7 +122,6 @@ def mock_column(name, type, options={})
125122
#--
126123
# #{AnnotateModels::END_MARK}
127124
#++
128-
129125
EOS
130126
end
131127

@@ -195,7 +191,7 @@ class FooWithMacro < ActiveRecord::Base
195191
EOS
196192
check_class_name 'foo_with_macro.rb', 'FooWithMacro'
197193
end
198-
194+
199195
it "should not care about known macros" do
200196
create('foo_with_known_macro.rb', <<-EOS)
201197
class FooWithKnownMacro < ActiveRecord::Base
@@ -365,12 +361,12 @@ def annotate_one_file options = {}
365361

366362
it "should annotate the file before the model if position == 'before'" do
367363
annotate_one_file :position => "before"
368-
File.read(@model_file_name).should == "#{@schema_info}#{@file_content}"
364+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
369365
end
370366

371367
it "should annotate before if given :position => :before" do
372368
annotate_one_file :position => :before
373-
File.read(@model_file_name).should == "#{@schema_info}#{@file_content}"
369+
File.read(@model_file_name).should == "#{@schema_info}\n#{@file_content}"
374370
end
375371

376372
it "should annotate after if given :position => :after" do
@@ -402,7 +398,7 @@ class Foo::User < ActiveRecord::Base
402398
])
403399
schema_info = AnnotateModels.get_schema_info(klass, "== Schema Info")
404400
AnnotateModels.annotate_one_file(model_file_name, schema_info, :position => :before)
405-
File.read(model_file_name).should == "#{schema_info}#{file_content}"
401+
File.read(model_file_name).should == "#{schema_info}\n#{file_content}"
406402
end
407403

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

0 commit comments

Comments
 (0)