Skip to content

Commit 95327bd

Browse files
Allow running annotate for when the columns are frozen
1 parent 5d01c41 commit 95327bd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/annotate/annotate_models.rb

+2
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ def get_attributes(column, column_type, klass, options)
940940
attrs << 'not null' unless column.null
941941
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(column.name.to_sym) : column.name.to_sym == klass.primary_key.to_sym)
942942

943+
column_type = column_type.dup if column_type.frozen?
944+
943945
if column_type == 'decimal'
944946
column_type << "(#{column.precision}, #{column.scale})"
945947
elsif !%w[spatial geometry geography].include?(column_type)

spec/lib/tasks/annotate_models_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,31 @@
3333
it { is_expected.to be_truthy }
3434
end
3535
end
36+
37+
context 'when column_type is frozen' do
38+
let(:klass) do
39+
Class.new(ActiveRecord::Base) do
40+
self.table_name = 'users'
41+
end
42+
end
43+
44+
before do
45+
allow(klass).to receive(:columns).and_return([
46+
double('Column', name: 'id', type: 'integer'.freeze, sql_type: 'integer', limit: nil, null: false, default: nil, comment: nil)
47+
])
48+
allow(klass).to receive(:table_exists?).and_return(true)
49+
allow(klass).to receive(:primary_key).and_return('id')
50+
end
51+
52+
it 'does not raise an error when modifying column_type' do
53+
expect {
54+
AnnotateModels.get_schema_info(klass, "Schema Info", {})
55+
}.not_to raise_error
56+
end
57+
58+
it 'includes the column information in the schema info' do
59+
schema_info = AnnotateModels.get_schema_info(klass, "Schema Info", {})
60+
expect(schema_info).to include('id :integer')
61+
end
62+
end
3663
end

0 commit comments

Comments
 (0)