Commit 026cee5a by cuong.tran

Fix #212 due to Rails 4.2 internal changes

parent 9c5837be
require 'bigdecimal'
module AnnotateModels module AnnotateModels
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = "== Schema Info" COMPAT_PREFIX = "== Schema Info"
...@@ -94,6 +96,10 @@ module AnnotateModels ...@@ -94,6 +96,10 @@ module AnnotateModels
end end
end end
def schema_default(klass, column)
quote(klass.column_defaults[column.name])
end
# Use the column information in an ActiveRecord class # Use the column information in an ActiveRecord class
# to create a comment block containing a line for # to create a comment block containing a line for
# each column. The line contains the column name, # each column. The line contains the column name,
...@@ -130,7 +136,7 @@ module AnnotateModels ...@@ -130,7 +136,7 @@ module AnnotateModels
cols = classified_sort(cols) if(options[:classified_sort]) cols = classified_sort(cols) if(options[:classified_sort])
cols.each do |col| cols.each do |col|
attrs = [] 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 << "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) 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)
......
...@@ -10,7 +10,10 @@ describe AnnotateModels do ...@@ -10,7 +10,10 @@ describe AnnotateModels do
:table_name => table_name, :table_name => table_name,
:primary_key => primary_key, :primary_key => primary_key,
:column_names => columns.map { |col| col.name.to_s }, :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) double("An ActiveRecord class", options)
...@@ -106,6 +109,24 @@ EOS ...@@ -106,6 +109,24 @@ EOS
EOS EOS
end 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 it "should get schema info as RDoc" do
klass = mock_class(:users, :id, [ klass = mock_class(:users, :id, [
mock_column(:id, :integer), mock_column(:id, :integer),
......
# == Schema Information
#
# Table name: events
#
# id :integer not null, primary key
# content :string(255)
# created_at :datetime
# updated_at :datetime
#
module Sub1::Sub2::Sub3 module Sub1::Sub2::Sub3
class Event < ActiveRecord::Base class Event < ActiveRecord::Base
end end
......
# == 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 class Sub1::User < ActiveRecord::Base
end end
# == 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 class Task < ActiveRecord::Base
enum status: %w(normal active completed) enum status: %w(normal active completed)
end end
...@@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration ...@@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration
def change def change
create_table :tasks do |t| create_table :tasks do |t|
t.string :content t.string :content
t.column :status, :default => 0 t.integer :count, :default => 0
t.boolean :status, :default => 0
t.timestamps t.timestamps
end end
end end
......
...@@ -21,6 +21,8 @@ ActiveRecord::Schema.define(version: 20140705000010) do ...@@ -21,6 +21,8 @@ ActiveRecord::Schema.define(version: 20140705000010) do
create_table "tasks", force: true do |t| create_table "tasks", force: true do |t|
t.string "content" t.string "content"
t.integer "count", default: 0
t.boolean "status", default: false
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
end end
......
# == 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' require 'test_helper'
class TaskTest < ActiveSupport::TestCase class TaskTest < ActiveSupport::TestCase
......
# == Schema Information
#
# Table name: events
#
# id :integer not null, primary key
# content :string
# created_at :datetime
# updated_at :datetime
#
module Sub1::Sub2::Sub3 module Sub1::Sub2::Sub3
class Event < ActiveRecord::Base class Event < ActiveRecord::Base
end end
......
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# content :string
# created_at :datetime
# updated_at :datetime
#
class Sub1::User < ActiveRecord::Base class Sub1::User < ActiveRecord::Base
end end
# == 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 class Task < ActiveRecord::Base
enum status: %w(normal active completed) enum status: %w(normal active completed)
end end
...@@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration ...@@ -2,7 +2,8 @@ class CreateTasks < ActiveRecord::Migration
def change def change
create_table :tasks do |t| create_table :tasks do |t|
t.string :content t.string :content
t.column :status, :default => 0 t.integer :count, default: 0
t.boolean :status, default: 0
t.timestamps t.timestamps
end end
end end
......
...@@ -13,19 +13,21 @@ ...@@ -13,19 +13,21 @@
ActiveRecord::Schema.define(version: 20140705000010) do ActiveRecord::Schema.define(version: 20140705000010) do
create_table "events", force: true do |t| create_table "events", force: :cascade do |t|
t.string "content" t.string "content"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "tasks", force: true do |t| create_table "tasks", force: :cascade do |t|
t.string "content" t.string "content"
t.integer "count", default: 0
t.boolean "status", default: false
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "users", force: true do |t| create_table "users", force: :cascade do |t|
t.string "content" t.string "content"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
......
# == 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' require 'test_helper'
class TaskTest < ActiveSupport::TestCase class TaskTest < ActiveSupport::TestCase
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment