Skip to content
Open
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
4 changes: 2 additions & 2 deletions lib/annotate_rb/model_annotator/annotation_diff_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def generate
new_annotations = @annotation_block.match(HEADER_PATTERN).to_s

current_annotation_columns = if current_annotations.present?
current_annotations.scan(COLUMN_PATTERN).sort
current_annotations.scan(COLUMN_PATTERN)
else
[]
end

new_annotation_columns = if new_annotations.present?
new_annotations.scan(COLUMN_PATTERN).sort
new_annotations.scan(COLUMN_PATTERN)
else
[]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def test_columns_match_expected
resulting_new_columns_data = subject.new_columns.map(&remove_whitespace)
expected_new_columns_data = new_columns.map(&remove_whitespace)

expect(resulting_current_columns_data).to eq(expected_current_columns_data)
expect(resulting_new_columns_data).to eq(expected_new_columns_data)
expect(resulting_current_columns_data).to match_array(expected_current_columns_data)
expect(resulting_new_columns_data).to match_array(expected_new_columns_data)
end

describe ".call" do
Expand Down Expand Up @@ -95,6 +95,54 @@ class User < ApplicationRecord
end
end

context "when model file has existing annotation with unordered columns" do
let(:file_content) do
<<~FILE
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# user_id :bigint
#
class User < ApplicationRecord
end
FILE
end
let(:annotation_block) do
<<~ANNOTATION
# == Schema Information
#
# Table name: users
#
# user_id :bigint
# id :bigint not null, primary key
#
ANNOTATION
end

let(:current_columns) do
[
"# id :bigint not null, primary key",
"# user_id :bigint",
"# Table name: users"
]
end
let(:new_columns) do
[
"# id :bigint not null, primary key",
"# user_id :bigint",
"# Table name: users"
]
end

it "returns an AnnotationDiff object with the expected old and new columns" do
test_columns_match_expected

expect(subject.changed?).to eq(true)
end
end

context "when model file has existing annotations with column comments" do
let(:annotation_block) do
<<~SCHEMA
Expand Down