Skip to content

Commit 62d31ea

Browse files
committed
Merge pull request #86 from kuroda/without_primary_key
expect a model without primary key
2 parents bef0c49 + be82e0c commit 62d31ea

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def get_schema_info(klass, header, options = {})
8282
attrs = []
8383
attrs << "default(#{quote(col.default)})" unless col.default.nil?
8484
attrs << "not null" unless col.null
85-
attrs << "primary key" if col.name.to_sym == klass.primary_key.to_sym
85+
attrs << "primary key" if klass.primary_key && col.name.to_sym == klass.primary_key.to_sym
8686

8787
col_type = (col.type || col.sql_type).to_s
8888
if col_type == "decimal"

spec/annotate/annotate_models_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def mock_class(table_name, primary_key, columns)
88
options = {
99
:connection => mock("Conn", :indexes => []),
1010
:table_name => table_name,
11-
:primary_key => primary_key.to_s,
11+
:primary_key => primary_key && primary_key.to_s,
1212
:column_names => columns.map { |col| col.name.to_s },
1313
:columns => columns
1414
}
@@ -52,6 +52,24 @@ def mock_column(name, type, options={})
5252
# name :string(50) not null
5353
#
5454
55+
EOS
56+
end
57+
58+
it "should get schema info even if the primary key is not set" do
59+
klass = mock_class(:users, nil, [
60+
mock_column(:id, :integer),
61+
mock_column(:name, :string, :limit => 50)
62+
])
63+
64+
AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS)
65+
# Schema Info
66+
#
67+
# Table name: users
68+
#
69+
# id :integer not null
70+
# name :string(50) not null
71+
#
72+
5573
EOS
5674
end
5775

0 commit comments

Comments
 (0)