From be82e0c007fa31d41e4934e6e8ad228b8dd7923f Mon Sep 17 00:00:00 2001 From: Tsutomu Kuroda Date: Fri, 20 Jul 2012 18:52:12 +0900 Subject: [PATCH] expect a model without primary key --- lib/annotate/annotate_models.rb | 2 +- spec/annotate/annotate_models_spec.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 091cbe794..3e0e0fcb1 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -82,7 +82,7 @@ def get_schema_info(klass, header, options = {}) attrs = [] attrs << "default(#{quote(col.default)})" unless col.default.nil? attrs << "not null" unless col.null - attrs << "primary key" if col.name.to_sym == klass.primary_key.to_sym + attrs << "primary key" if klass.primary_key && col.name.to_sym == klass.primary_key.to_sym col_type = (col.type || col.sql_type).to_s if col_type == "decimal" diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index 70fea5264..ebc9d2d97 100644 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -8,7 +8,7 @@ def mock_class(table_name, primary_key, columns) options = { :connection => mock("Conn", :indexes => []), :table_name => table_name, - :primary_key => primary_key.to_s, + :primary_key => primary_key && primary_key.to_s, :column_names => columns.map { |col| col.name.to_s }, :columns => columns } @@ -52,6 +52,24 @@ def mock_column(name, type, options={}) # name :string(50) not null # +EOS + end + + it "should get schema info even if the primary key is not set" do + klass = mock_class(:users, nil, [ + mock_column(:id, :integer), + mock_column(:name, :string, :limit => 50) + ]) + + AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS) +# Schema Info +# +# Table name: users +# +# id :integer not null +# name :string(50) not null +# + EOS end