Commit a4be28ad by Alexander Belozerov Committed by Cuong Tran

[Fix #624] Correct default values for columns when ActiveRecord::Enum is used (#677)

parent 1df2153f
......@@ -211,7 +211,7 @@ module AnnotateModels
end
def schema_default(klass, column)
quote(klass.column_defaults[column.name])
quote(klass.columns.find { |x| x.name.to_s == column.name.to_s }.try(:default))
end
def retrieve_indexes_from_table(klass)
......
......@@ -225,6 +225,29 @@ EOS
EOS
end
it 'sets correct default value for integer column when ActiveRecord::Enum is used' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:status, :integer, default: 0)
])
# column_defaults may be overritten when ActiveRecord::Enum is used, e.g:
# class User < ActiveRecord::Base
# enum status: [ :disabled, :enabled ]
# end
allow(klass).to receive(:column_defaults).and_return('id' => nil, 'status' => 'disabled')
expect(AnnotateModels.get_schema_info(klass, 'Schema Info')).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# status :integer default(0), not null
#
EOS
end
it 'should get foreign key info' do
klass = mock_class(:users,
:id,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment