Skip to content

[Fix #624] Correct default values when ActiveRecord::Enum is used #677

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

Merged
merged 1 commit into from
Nov 9, 2019
Merged
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
2 changes: 1 addition & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def quote(value)
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)
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,29 @@ def mock_column(name, type, options = {})
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,
Expand Down