Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture columns with multibyte comments #29

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 3 additions & 2 deletions lib/annotate_rb/model_annotator/annotation_diff_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module AnnotateRb
module ModelAnnotator
# Compares the current file content and new annotation block and generates the column annotation differences
class AnnotationDiffGenerator
HEADER_PATTERN = /(^# Table name:.*?\n(#.*\r?\n)*\r?)/
COLUMN_PATTERN = /^#[\t ]+[\w*.`\[\]():]+[\t ]+.+$/
HEADER_PATTERN = /(^# Table name:.*?\n(#.*\r?\n)*\r?)/.freeze
# COLUMN_PATTERN = /^#[\t ]+[\w\*\.`\[\]():]+[\t ]+.+$/.freeze
COLUMN_PATTERN = /^#[\t ]+[^\t ]+[\t ]+.+$/.freeze

class << self
def call(file_content, annotation_block)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe AnnotateRb::ModelAnnotator::AnnotationDiffGenerator do
include AnnotateTestHelpers

def test_columns_match_expected
remove_whitespace = proc { |str| str.delete(" \t\r\n") }

Expand Down Expand Up @@ -142,5 +144,53 @@ class User < ApplicationRecord
expect(subject.changed?).to eq(true)
end
end

context "when model file has existing annotations with column multi-byte comments" do
let(:annotation_block) do
klass = mock_class(:users,
:id,
[
mock_column(:id, :bigint, comment: "ID"),
mock_column(:active, :boolean, limit: 1, comment: "ACTIVE")
],
[],
[])
options = AnnotateRb::Options.from({})
AnnotateRb::ModelAnnotator::AnnotationBuilder.new(klass, options).build
end

let(:file_content) do
<<~FILE
# == Schema Information
#
# Table name: users
#
# id(ID) :bigint not null, primary key
#
class User < ApplicationRecord
end
FILE
end

let(:current_columns) do
[
"# id(ID) :bigint not null, primary key",
"# Table name: users"
]
end
let(:new_columns) do
[
"# id(ID) :bigint not null, primary key",
"# active(ACTIVE) :boolean not null",
"# 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
end
end
Loading