Skip to content

Commit 026cee5

Browse files
committed
Fix #212 due to Rails 4.2 internal changes
1 parent 9c5837b commit 026cee5

File tree

14 files changed

+128
-7
lines changed

14 files changed

+128
-7
lines changed

lib/annotate/annotate_models.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'bigdecimal'
2+
13
module AnnotateModels
24
# Annotate Models plugin use this header
35
COMPAT_PREFIX = "== Schema Info"
@@ -94,6 +96,10 @@ def quote(value)
9496
end
9597
end
9698

99+
def schema_default(klass, column)
100+
quote(klass.column_defaults[column.name])
101+
end
102+
97103
# Use the column information in an ActiveRecord class
98104
# to create a comment block containing a line for
99105
# each column. The line contains the column name,
@@ -130,7 +136,7 @@ def get_schema_info(klass, header, options = {})
130136
cols = classified_sort(cols) if(options[:classified_sort])
131137
cols.each do |col|
132138
attrs = []
133-
attrs << "default(#{quote(col.default)})" unless col.default.nil?
139+
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil?
134140
attrs << "not null" unless col.null
135141
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)
136142

spec/annotate/annotate_models_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def mock_class(table_name, primary_key, columns)
1010
:table_name => table_name,
1111
:primary_key => primary_key,
1212
:column_names => columns.map { |col| col.name.to_s },
13-
:columns => columns
13+
:columns => columns,
14+
:column_defaults => Hash[columns.map { |col|
15+
[col.name, col.default]
16+
}]
1417
}
1518

1619
double("An ActiveRecord class", options)
@@ -106,6 +109,24 @@ def mock_column(name, type, options={})
106109
EOS
107110
end
108111

112+
it "should get schema info for integer and boolean with default" do
113+
klass = mock_class(:users, :id, [
114+
mock_column(:id, :integer),
115+
mock_column(:size, :integer, :default => 20),
116+
mock_column(:flag, :boolean, :default => false)
117+
])
118+
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
119+
# Schema Info
120+
#
121+
# Table name: users
122+
#
123+
# id :integer not null, primary key
124+
# size :integer default(20), not null
125+
# flag :boolean default(FALSE), not null
126+
#
127+
EOS
128+
end
129+
109130
it "should get schema info as RDoc" do
110131
klass = mock_class(:users, :id, [
111132
mock_column(:id, :integer),

spec/integration/rails_4.1.1/app/models/sub1/sub2/sub3/event.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# == Schema Information
2+
#
3+
# Table name: events
4+
#
5+
# id :integer not null, primary key
6+
# content :string(255)
7+
# created_at :datetime
8+
# updated_at :datetime
9+
#
10+
111
module Sub1::Sub2::Sub3
212
class Event < ActiveRecord::Base
313
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1+
# == Schema Information
2+
#
3+
# Table name: users
4+
#
5+
# id :integer not null, primary key
6+
# content :string(255)
7+
# created_at :datetime
8+
# updated_at :datetime
9+
#
10+
111
class Sub1::User < ActiveRecord::Base
212
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# == Schema Information
2+
#
3+
# Table name: tasks
4+
#
5+
# id :integer not null, primary key
6+
# content :string(255)
7+
# count :integer default(0)
8+
# status :boolean default(FALSE)
9+
# created_at :datetime
10+
# updated_at :datetime
11+
#
12+
113
class Task < ActiveRecord::Base
214
enum status: %w(normal active completed)
315
end

spec/integration/rails_4.1.1/db/migrate/20140526224112_create_tasks.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration
22
def change
33
create_table :tasks do |t|
44
t.string :content
5-
t.column :status, :default => 0
5+
t.integer :count, :default => 0
6+
t.boolean :status, :default => 0
67
t.timestamps
78
end
89
end

spec/integration/rails_4.1.1/db/schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
create_table "tasks", force: true do |t|
2323
t.string "content"
24+
t.integer "count", default: 0
25+
t.boolean "status", default: false
2426
t.datetime "created_at"
2527
t.datetime "updated_at"
2628
end

spec/integration/rails_4.1.1/test/models/task_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# == Schema Information
2+
#
3+
# Table name: tasks
4+
#
5+
# id :integer not null, primary key
6+
# content :string(255)
7+
# count :integer default(0)
8+
# status :boolean default(FALSE)
9+
# created_at :datetime
10+
# updated_at :datetime
11+
#
12+
113
require 'test_helper'
214

315
class TaskTest < ActiveSupport::TestCase

spec/integration/rails_4.2.0/app/models/sub1/sub2/sub3/event.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# == Schema Information
2+
#
3+
# Table name: events
4+
#
5+
# id :integer not null, primary key
6+
# content :string
7+
# created_at :datetime
8+
# updated_at :datetime
9+
#
10+
111
module Sub1::Sub2::Sub3
212
class Event < ActiveRecord::Base
313
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1+
# == Schema Information
2+
#
3+
# Table name: users
4+
#
5+
# id :integer not null, primary key
6+
# content :string
7+
# created_at :datetime
8+
# updated_at :datetime
9+
#
10+
111
class Sub1::User < ActiveRecord::Base
212
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# == Schema Information
2+
#
3+
# Table name: tasks
4+
#
5+
# id :integer not null, primary key
6+
# content :string
7+
# count :integer default(0)
8+
# status :boolean default(FALSE)
9+
# created_at :datetime
10+
# updated_at :datetime
11+
#
12+
113
class Task < ActiveRecord::Base
214
enum status: %w(normal active completed)
315
end

spec/integration/rails_4.2.0/db/migrate/20140526224112_create_tasks.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration
22
def change
33
create_table :tasks do |t|
44
t.string :content
5-
t.column :status, :default => 0
5+
t.integer :count, default: 0
6+
t.boolean :status, default: 0
67
t.timestamps
78
end
89
end

spec/integration/rails_4.2.0/db/schema.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313

1414
ActiveRecord::Schema.define(version: 20140705000010) do
1515

16-
create_table "events", force: true do |t|
16+
create_table "events", force: :cascade do |t|
1717
t.string "content"
1818
t.datetime "created_at"
1919
t.datetime "updated_at"
2020
end
2121

22-
create_table "tasks", force: true do |t|
22+
create_table "tasks", force: :cascade do |t|
2323
t.string "content"
24+
t.integer "count", default: 0
25+
t.boolean "status", default: false
2426
t.datetime "created_at"
2527
t.datetime "updated_at"
2628
end
2729

28-
create_table "users", force: true do |t|
30+
create_table "users", force: :cascade do |t|
2931
t.string "content"
3032
t.datetime "created_at"
3133
t.datetime "updated_at"

spec/integration/rails_4.2.0/test/models/task_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# == Schema Information
2+
#
3+
# Table name: tasks
4+
#
5+
# id :integer not null, primary key
6+
# content :string
7+
# count :integer default(0)
8+
# status :boolean default(FALSE)
9+
# created_at :datetime
10+
# updated_at :datetime
11+
#
12+
113
require 'test_helper'
214

315
class TaskTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)