Skip to content

Commit 8bd159c

Browse files
committed
Merge pull request #105 from garysweaver/master
add support for composite_primary_keys
2 parents 1340635 + 8bfeeb2 commit 8bd159c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_schema_info(klass, header, options = {})
113113
attrs = []
114114
attrs << "default(#{quote(col.default)})" unless col.default.nil?
115115
attrs << "not null" unless col.null
116-
attrs << "primary key" if klass.primary_key && col.name.to_sym == klass.primary_key.to_sym
116+
attrs << "primary key" if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect{|c|c.to_sym}.include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)
117117

118118
col_type = (col.type || col.sql_type).to_s
119119
if col_type == "decimal"

spec/annotate/annotate_models_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ def mock_column(name, type, options={})
7070
# name :string(50) not null
7171
#
7272
73+
EOS
74+
end
75+
76+
it "should get schema info even if the primary key is array, if using composite_primary_keys" do
77+
klass = mock_class(:users, nil, [
78+
[mock_column(:a_id, :integer), mock_column(:b_id, :integer)],
79+
mock_column(:name, :string, :limit => 50)
80+
])
81+
82+
AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS)
83+
# Schema Info
84+
#
85+
# Table name: users
86+
#
87+
# a_id :integer not null
88+
# b_id :integer not null
89+
# name :string(50) not null
90+
#
91+
7392
EOS
7493
end
7594

0 commit comments

Comments
 (0)