From d010c5b7973def4934318c51cf4f65ae18b755bb Mon Sep 17 00:00:00 2001 From: Patrick Arnett Date: Mon, 17 Nov 2025 11:34:59 -0500 Subject: [PATCH] fix: Respect configured sort --- .../annotation_diff_generator.rb | 4 +- .../annotation_diff_generator_spec.rb | 52 ++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/annotate_rb/model_annotator/annotation_diff_generator.rb b/lib/annotate_rb/model_annotator/annotation_diff_generator.rb index f5cabb54..3f303329 100644 --- a/lib/annotate_rb/model_annotator/annotation_diff_generator.rb +++ b/lib/annotate_rb/model_annotator/annotation_diff_generator.rb @@ -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 diff --git a/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb b/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb index fd4b2b84..f48d82fc 100644 --- a/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb +++ b/spec/lib/annotate_rb/model_annotator/annotation_diff_generator_spec.rb @@ -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 @@ -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