Skip to content

Commit

Permalink
Fix #212 due to Rails 4.2 internal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran committed Mar 8, 2015
1 parent 9c5837b commit 026cee5
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'bigdecimal'

module AnnotateModels
# Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info"
Expand Down Expand Up @@ -94,6 +96,10 @@ def quote(value)
end
end

def schema_default(klass, column)
quote(klass.column_defaults[column.name])
end

# Use the column information in an ActiveRecord class
# to create a comment block containing a line for
# each column. The line contains the column name,
Expand Down Expand Up @@ -130,7 +136,7 @@ def get_schema_info(klass, header, options = {})
cols = classified_sort(cols) if(options[:classified_sort])
cols.each do |col|
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil?
attrs << "not null" unless col.null
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)

Expand Down
23 changes: 22 additions & 1 deletion spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def mock_class(table_name, primary_key, columns)
:table_name => table_name,
:primary_key => primary_key,
:column_names => columns.map { |col| col.name.to_s },
:columns => columns
:columns => columns,
:column_defaults => Hash[columns.map { |col|
[col.name, col.default]
}]
}

double("An ActiveRecord class", options)
Expand Down Expand Up @@ -106,6 +109,24 @@ def mock_column(name, type, options={})
EOS
end

it "should get schema info for integer and boolean with default" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:size, :integer, :default => 20),
mock_column(:flag, :boolean, :default => false)
])
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# size :integer default(20), not null
# flag :boolean default(FALSE), not null
#
EOS
end

it "should get schema info as RDoc" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/rails_4.1.1/app/models/sub1/sub2/sub3/event.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# == Schema Information
#
# Table name: events
#
# id :integer not null, primary key
# content :string(255)
# created_at :datetime
# updated_at :datetime
#

module Sub1::Sub2::Sub3
class Event < ActiveRecord::Base
end
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/rails_4.1.1/app/models/sub1/user.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# content :string(255)
# created_at :datetime
# updated_at :datetime
#

class Sub1::User < ActiveRecord::Base
end
12 changes: 12 additions & 0 deletions spec/integration/rails_4.1.1/app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# content :string(255)
# count :integer default(0)
# status :boolean default(FALSE)
# created_at :datetime
# updated_at :datetime
#

class Task < ActiveRecord::Base
enum status: %w(normal active completed)
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :content
t.column :status, :default => 0
t.integer :count, :default => 0
t.boolean :status, :default => 0
t.timestamps
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/integration/rails_4.1.1/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

create_table "tasks", force: true do |t|
t.string "content"
t.integer "count", default: 0
t.boolean "status", default: false
t.datetime "created_at"
t.datetime "updated_at"
end
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/rails_4.1.1/test/models/task_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# content :string(255)
# count :integer default(0)
# status :boolean default(FALSE)
# created_at :datetime
# updated_at :datetime
#

require 'test_helper'

class TaskTest < ActiveSupport::TestCase
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/rails_4.2.0/app/models/sub1/sub2/sub3/event.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# == Schema Information
#
# Table name: events
#
# id :integer not null, primary key
# content :string
# created_at :datetime
# updated_at :datetime
#

module Sub1::Sub2::Sub3
class Event < ActiveRecord::Base
end
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/rails_4.2.0/app/models/sub1/user.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# content :string
# created_at :datetime
# updated_at :datetime
#

class Sub1::User < ActiveRecord::Base
end
12 changes: 12 additions & 0 deletions spec/integration/rails_4.2.0/app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# content :string
# count :integer default(0)
# status :boolean default(FALSE)
# created_at :datetime
# updated_at :datetime
#

class Task < ActiveRecord::Base
enum status: %w(normal active completed)
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :content
t.column :status, :default => 0
t.integer :count, default: 0
t.boolean :status, default: 0
t.timestamps
end
end
Expand Down
8 changes: 5 additions & 3 deletions spec/integration/rails_4.2.0/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@

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

create_table "events", force: true do |t|
create_table "events", force: :cascade do |t|
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "tasks", force: true do |t|
create_table "tasks", force: :cascade do |t|
t.string "content"
t.integer "count", default: 0
t.boolean "status", default: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", force: true do |t|
create_table "users", force: :cascade do |t|
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/rails_4.2.0/test/models/task_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# content :string
# count :integer default(0)
# status :boolean default(FALSE)
# created_at :datetime
# updated_at :datetime
#

require 'test_helper'

class TaskTest < ActiveSupport::TestCase
Expand Down

0 comments on commit 026cee5

Please sign in to comment.