openrain-activerecord-comments 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +85 -0
- data/VERSION.yml +4 -0
- data/lib/activerecord-comments.rb +29 -0
- data/lib/activerecord-comments/abstract_adapter_ext.rb +103 -0
- data/lib/activerecord-comments/base_ext.rb +71 -0
- data/lib/activerecord-comments/column_ext.rb +8 -0
- data/lib/activerecord-comments/mysql_adapter.rb +139 -0
- data/spec/mysql_comments_spec.rb +109 -0
- data/spec/spec_database.yml +6 -0
- data/spec/spec_helper.rb +26 -0
- metadata +64 -0
data/README.markdown
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
activerecord-comments
|
2
|
+
=====================
|
3
|
+
|
4
|
+
Super-duper simple gem for getting table/column comments defined in the database.
|
5
|
+
|
6
|
+
Background
|
7
|
+
----------
|
8
|
+
|
9
|
+
I wanted a way to easily get to the comments defined in the database, via ActiveRecord.
|
10
|
+
While the underlying implementation may change to become faster and more database agnostic,
|
11
|
+
the public API should remain the same.
|
12
|
+
|
13
|
+
Install
|
14
|
+
-------
|
15
|
+
|
16
|
+
$ sudo gem install remi-activerecord-comments -s http://gems.github.com
|
17
|
+
|
18
|
+
Usage
|
19
|
+
-----
|
20
|
+
|
21
|
+
>> require 'activerecord-comments'
|
22
|
+
|
23
|
+
>> Fox.comment
|
24
|
+
=> "Represents a Fox, a creature that craves chunky bacon"
|
25
|
+
|
26
|
+
>> ActiveRecord::Base.comment :foxes
|
27
|
+
=> "Represents a Fox, a creature that craves chunky bacon"
|
28
|
+
|
29
|
+
>> ActiveRecord::Base.connection.comment :foxes
|
30
|
+
=> "Represents a Fox, a creature that craves chunky bacon"
|
31
|
+
|
32
|
+
>> Fox.columns
|
33
|
+
=> [#<ActiveRecord::...>, #<ActiveRecord::...>]
|
34
|
+
|
35
|
+
>> Fox.columns.first.name
|
36
|
+
=> "id"
|
37
|
+
|
38
|
+
>> Fox.columns.first.comment
|
39
|
+
=> "Primary Key"
|
40
|
+
|
41
|
+
>> Fox.column_comment :id
|
42
|
+
=> "Primary Key"
|
43
|
+
|
44
|
+
>> ActiveRecord::Base.column_comment :id, :foxes
|
45
|
+
=> "Primary Key"
|
46
|
+
|
47
|
+
>> ActiveRecord::Base.connection.column_comment :id, :foxes
|
48
|
+
=> "Primary Key"
|
49
|
+
|
50
|
+
|
51
|
+
Database Support
|
52
|
+
----------------
|
53
|
+
|
54
|
+
For right now, I'm just supporting MySQL as it's the only database I'm currently using
|
55
|
+
that supports database comments.
|
56
|
+
|
57
|
+
If you want to extend activerecord-comments to support comments for your favorite database,
|
58
|
+
the gem is coded in such a way that it should be really easy to extend.
|
59
|
+
|
60
|
+
See [mysql_adapter.rb][mysql_adapter] for an example of the methods your database adapter
|
61
|
+
needs to support (just `#comment(table)` and `#column_comment(column,table)`).
|
62
|
+
|
63
|
+
|
64
|
+
SQL
|
65
|
+
---
|
66
|
+
|
67
|
+
If you're unsure how to add comments to your MySQL tables/columns, most MySQL GUIs support
|
68
|
+
this, or you can add comments to your `CREATE TABLE` declarations ...
|
69
|
+
|
70
|
+
CREATE TABLE foo (
|
71
|
+
id INT COMMENT 'i am the primary key',
|
72
|
+
foo VARCHAR(100) COMMENT 'foo!'
|
73
|
+
) COMMENT 'this table rocks'
|
74
|
+
|
75
|
+
for more MySQL examples, see [spec/mysql_comments_spec.rb][mysql_spec]
|
76
|
+
|
77
|
+
|
78
|
+
Future Ideas
|
79
|
+
------------
|
80
|
+
|
81
|
+
- create `db-doc` or (something) that provides a system for documenting your database schema, whether it be via .yml files or as database comments or whatever. then make `activerecord-comments` an extension to that (basically just a different data store for your schema documentation). there should always be a way to easily document your schema, regardless of whether or not your database(s) supports comments. and, with or without database comments, there should be an easy way to see which tables/columns you have and haven't documented!
|
82
|
+
|
83
|
+
|
84
|
+
[mysql_adapter]: http://github.com/remi/activerecord-comments/tree/master/lib/activerecord-comments/mysql_adapter.rb
|
85
|
+
[mysql_spec]: http://github.com/remi/activerecord-comments/tree/master/spec/mysql_comments_spec.rb
|
data/VERSION.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
if defined?(ActiveRecord)
|
4
|
+
|
5
|
+
# Main class for ActiveRecord::Comments ActiveRecord extension gem
|
6
|
+
#
|
7
|
+
# Used for global configuration options / etc for the extension
|
8
|
+
class ActiveRecord::Comments
|
9
|
+
end
|
10
|
+
|
11
|
+
class ActiveRecord::Comments::UnsupportedDatabase < Exception; end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
if defined?(ActiveRecord::Base) &&
|
16
|
+
defined?(ActiveRecord::ConnectionAdapters::Column) &&
|
17
|
+
defined?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
|
18
|
+
|
19
|
+
# require and include our modules which add the 'comment' functionality to ActiveRecord
|
20
|
+
|
21
|
+
require 'activerecord-comments/base_ext'
|
22
|
+
require 'activerecord-comments/column_ext'
|
23
|
+
require 'activerecord-comments/abstract_adapter_ext'
|
24
|
+
|
25
|
+
ActiveRecord::Base.send :include, ActiveRecord::Comments::BaseExt
|
26
|
+
ActiveRecord::ConnectionAdapters::Column.send :include, ActiveRecord::Comments::ColumnExt
|
27
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, ActiveRecord::Comments::AbstractAdapterExt
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module ActiveRecord::Comments::AbstractAdapterExt
|
2
|
+
|
3
|
+
def self.included base
|
4
|
+
puts "including abstract adapter extensions in #{ base.inspect }"
|
5
|
+
|
6
|
+
base.instance_eval {
|
7
|
+
alias_method_chain :columns, :table_name # this is evil!!! how to fix? column needs to know its table :(
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
# Get the database comment (if any) defined for a table
|
12
|
+
#
|
13
|
+
# ==== Parameters
|
14
|
+
# table<~to_s>::
|
15
|
+
# The name of the table to get the comment for
|
16
|
+
#
|
17
|
+
# ==== Returns
|
18
|
+
# String:: The comment for the given table (or nil if no comment)
|
19
|
+
#
|
20
|
+
# :api: public
|
21
|
+
def comment table
|
22
|
+
adapter = adapter_name.downcase
|
23
|
+
database_specific_method_name = "#{ adapter }_comment"
|
24
|
+
|
25
|
+
if self.respond_to? database_specific_method_name
|
26
|
+
send database_specific_method_name, table.to_s
|
27
|
+
else
|
28
|
+
|
29
|
+
# try requiring 'activerecord-comments/[name-of-adapter]_adapter'
|
30
|
+
begin
|
31
|
+
|
32
|
+
# see if there right method exists after requiring
|
33
|
+
require "activerecord-comments/#{ adapter }_adapter"
|
34
|
+
if self.respond_to? database_specific_method_name
|
35
|
+
send database_specific_method_name, table.to_s
|
36
|
+
else
|
37
|
+
raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
|
38
|
+
end
|
39
|
+
|
40
|
+
rescue LoadError
|
41
|
+
raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Get the database comment (if any) defined for a column
|
47
|
+
#
|
48
|
+
# ==== Parameters
|
49
|
+
# column<~to_s>::
|
50
|
+
# The name of the column to get the comment for
|
51
|
+
#
|
52
|
+
# table<~to_s>::
|
53
|
+
# The name of the table to get the column comment for
|
54
|
+
#
|
55
|
+
# ==== Returns
|
56
|
+
# String:: The comment for the given column (or nil if no comment)
|
57
|
+
#
|
58
|
+
# :api: public
|
59
|
+
def column_comment column, table
|
60
|
+
adapter = adapter_name.downcase
|
61
|
+
database_specific_method_name = "#{ adapter }_column_comment"
|
62
|
+
|
63
|
+
if self.respond_to? database_specific_method_name
|
64
|
+
send database_specific_method_name, column.to_s, table.to_s
|
65
|
+
else
|
66
|
+
|
67
|
+
# try requiring 'activerecord-comments/[name-of-adapter]_adapter'
|
68
|
+
begin
|
69
|
+
|
70
|
+
# see if there right method exists after requiring
|
71
|
+
require "activerecord-comments/#{ adapter }_adapter"
|
72
|
+
if self.respond_to? database_specific_method_name
|
73
|
+
send database_specific_method_name, column.to_s, table.to_s
|
74
|
+
else
|
75
|
+
raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
|
76
|
+
end
|
77
|
+
|
78
|
+
rescue LoadError
|
79
|
+
raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Extends #columns, setting @table_name as an instance variable
|
85
|
+
# on each of the column instances that are returned
|
86
|
+
#
|
87
|
+
# ==== Returns
|
88
|
+
# Array[ActiveRecord::ConnectionAdapters::Column]::
|
89
|
+
# Returns an Array of column objects, each with @table_name set
|
90
|
+
#
|
91
|
+
# :api: private
|
92
|
+
def columns_with_table_name *args
|
93
|
+
puts "\n\n HELLO ???? HELLO ????? \n\n"
|
94
|
+
puts "asking adapter for columns for #{ args.inspect }"
|
95
|
+
columns = columns_without_table_name *args
|
96
|
+
table = self.table_name # make table_name available as variable in instanve_eval closure
|
97
|
+
columns.each do |column|
|
98
|
+
column.instance_eval { @table_name = table }
|
99
|
+
end
|
100
|
+
columns
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module ActiveRecord::Comments::BaseExt
|
2
|
+
|
3
|
+
def self.included base
|
4
|
+
base.extend ClassMethods
|
5
|
+
|
6
|
+
base.instance_eval {
|
7
|
+
class << self
|
8
|
+
alias_method_chain :columns, :table_name # this is evil!!! how to fix? column needs to know its table :(
|
9
|
+
end
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
# Get the database comment (if any) defined for a table
|
16
|
+
#
|
17
|
+
# ==== Parameters
|
18
|
+
# table<~to_s>::
|
19
|
+
# The name of the table to get the comment for, default is
|
20
|
+
# the #table_name of the ActiveRecord::Base class this is
|
21
|
+
# being called on, eg. +User.comment+
|
22
|
+
#
|
23
|
+
# ==== Returns
|
24
|
+
# String:: The comment for the given table (or nil if no comment)
|
25
|
+
#
|
26
|
+
# :api: public
|
27
|
+
def comment table = self.table_name
|
28
|
+
connection.comment table
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get the database comment (if any) defined for a column
|
32
|
+
#
|
33
|
+
# TODO move into adapter!
|
34
|
+
#
|
35
|
+
# ==== Parameters
|
36
|
+
# column<~to_s>::
|
37
|
+
# The name of the column to get the comment for
|
38
|
+
#
|
39
|
+
# table<~to_s>::
|
40
|
+
# The name of the table to get the column comment for, default is
|
41
|
+
# the #table_name of the ActiveRecord::Base class this is
|
42
|
+
# being called on, eg. +User.column_comment 'username'+
|
43
|
+
#
|
44
|
+
# ==== Returns
|
45
|
+
# String:: The comment for the given column (or nil if no comment)
|
46
|
+
#
|
47
|
+
# :api: public
|
48
|
+
def column_comment column, table = self.table_name
|
49
|
+
connection.column_comment column, table
|
50
|
+
end
|
51
|
+
|
52
|
+
# Extends ActiveRecord::Base#columns, setting @table_name as an instance variable
|
53
|
+
# on each of the column instances that are returned
|
54
|
+
#
|
55
|
+
# ==== Returns
|
56
|
+
# Array[ActiveRecord::ConnectionAdapters::Column]::
|
57
|
+
# Returns an Array of column objects, each with @table_name set
|
58
|
+
#
|
59
|
+
# :api: private
|
60
|
+
def columns_with_table_name *args
|
61
|
+
columns = columns_without_table_name *args
|
62
|
+
table = self.table_name # make table_name available as variable in instanve_eval closure
|
63
|
+
columns.each do |column|
|
64
|
+
column.instance_eval { @table_name = table }
|
65
|
+
end
|
66
|
+
columns
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module ActiveRecord::Comments::MysqlAdapter
|
2
|
+
|
3
|
+
def self.included base
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# MySQL implementation of ActiveRecord::Comments::BaseExt#comment
|
10
|
+
def mysql_comment table
|
11
|
+
table_options = create_table_sql(table).split("\n").last
|
12
|
+
if table_options =~ /COMMENT='/
|
13
|
+
/COMMENT='(.*)'/.match(table_options).captures.first
|
14
|
+
else
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# MySQL implementation of ActiveRecord::Comments::BaseExt#column_comment
|
20
|
+
def mysql_column_comment column, table
|
21
|
+
column_creation_sql = create_column_sql(column, table)
|
22
|
+
if column_creation_sql =~ /COMMENT '/
|
23
|
+
/COMMENT '(.*)'/.match(column_creation_sql).captures.first
|
24
|
+
else
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Returns the SQL used to create the given table
|
32
|
+
#
|
33
|
+
# ==== Parameters
|
34
|
+
# table<~to_s>::
|
35
|
+
# The name of the table to get the 'CREATE TABLE' SQL for
|
36
|
+
#
|
37
|
+
# ==== Returns
|
38
|
+
# String:: the SQL used to create the table
|
39
|
+
#
|
40
|
+
# :api: private
|
41
|
+
def create_table_sql table = table_name
|
42
|
+
connection.execute("show create table `#{ table }`").all_hashes.first['Create Table']
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the SQL used to create the given column for the given table
|
46
|
+
#
|
47
|
+
# ==== Parameters
|
48
|
+
# column<~to_s>::
|
49
|
+
# The name of the column to get the creation SQL for
|
50
|
+
#
|
51
|
+
# table<~to_s>::
|
52
|
+
# The name of the table to get the 'CREATE TABLE' SQL for
|
53
|
+
#
|
54
|
+
# ==== Returns
|
55
|
+
# String:: the SQL used to create the column
|
56
|
+
#
|
57
|
+
# :api: private
|
58
|
+
def create_column_sql column, table = table_name
|
59
|
+
full_table_create_sql = create_table_sql(table)
|
60
|
+
parts = full_table_create_sql.split("\n")
|
61
|
+
create_table = parts.shift # take off the first CREATE TABLE part
|
62
|
+
create_table_options = parts.pop # take off the last options for the table, leaving just the columns
|
63
|
+
sql_for_this_column = parts.find {|str| str =~ /^ *`#{ column }`/ }
|
64
|
+
sql_for_this_column.strip! if sql_for_this_column
|
65
|
+
sql_for_this_column
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
ActiveRecord::Base.send :include, ActiveRecord::Comments::MysqlAdapter
|
73
|
+
|
74
|
+
################ WE SHOULD DO EVERYTHING ON THE CONNECTION (ADAPTER) INSTEAD!!!!!!
|
75
|
+
|
76
|
+
module ActiveRecord::Comments::MysqlAdapterAdapter
|
77
|
+
|
78
|
+
# MySQL implementation of ActiveRecord::Comments::BaseExt#comment
|
79
|
+
def mysql_comment table
|
80
|
+
table_options = create_table_sql(table).split("\n").last
|
81
|
+
if table_options =~ /COMMENT='/
|
82
|
+
/COMMENT='(.*)'/.match(table_options).captures.first
|
83
|
+
else
|
84
|
+
nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# MySQL implementation of ActiveRecord::Comments::BaseExt#column_comment
|
89
|
+
def mysql_column_comment column, table
|
90
|
+
column_creation_sql = create_column_sql(column, table)
|
91
|
+
if column_creation_sql =~ /COMMENT '/
|
92
|
+
/COMMENT '(.*)'/.match(column_creation_sql).captures.first
|
93
|
+
else
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
# Returns the SQL used to create the given table
|
101
|
+
#
|
102
|
+
# ==== Parameters
|
103
|
+
# table<~to_s>::
|
104
|
+
# The name of the table to get the 'CREATE TABLE' SQL for
|
105
|
+
#
|
106
|
+
# ==== Returns
|
107
|
+
# String:: the SQL used to create the table
|
108
|
+
#
|
109
|
+
# :api: private
|
110
|
+
def create_table_sql table = table_name
|
111
|
+
execute("show create table `#{ table }`").all_hashes.first['Create Table']
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns the SQL used to create the given column for the given table
|
115
|
+
#
|
116
|
+
# ==== Parameters
|
117
|
+
# column<~to_s>::
|
118
|
+
# The name of the column to get the creation SQL for
|
119
|
+
#
|
120
|
+
# table<~to_s>::
|
121
|
+
# The name of the table to get the 'CREATE TABLE' SQL for
|
122
|
+
#
|
123
|
+
# ==== Returns
|
124
|
+
# String:: the SQL used to create the column
|
125
|
+
#
|
126
|
+
# :api: private
|
127
|
+
def create_column_sql column, table = table_name
|
128
|
+
full_table_create_sql = create_table_sql(table)
|
129
|
+
parts = full_table_create_sql.split("\n")
|
130
|
+
create_table = parts.shift # take off the first CREATE TABLE part
|
131
|
+
create_table_options = parts.pop # take off the last options for the table, leaving just the columns
|
132
|
+
sql_for_this_column = parts.find {|str| str =~ /^ *`#{ column }`/ }
|
133
|
+
sql_for_this_column.strip! if sql_for_this_column
|
134
|
+
sql_for_this_column
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, ActiveRecord::Comments::MysqlAdapterAdapter
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Comments, 'MySQL' do
|
4
|
+
|
5
|
+
# HELPERS
|
6
|
+
|
7
|
+
def connection
|
8
|
+
ActiveRecord::Base.connection
|
9
|
+
end
|
10
|
+
|
11
|
+
def drop_table
|
12
|
+
connection.execute 'DROP TABLE IF EXISTS foxes;'
|
13
|
+
end
|
14
|
+
|
15
|
+
before do
|
16
|
+
drop_table
|
17
|
+
@foxes = Class.new(ActiveRecord::Base){ set_table_name 'foxes' }
|
18
|
+
end
|
19
|
+
|
20
|
+
# EXAMPLES
|
21
|
+
|
22
|
+
describe ActiveRecord::Base, 'extensions' do
|
23
|
+
|
24
|
+
it "should create table OK" do
|
25
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY );"
|
26
|
+
@foxes.count.should == 0
|
27
|
+
end
|
28
|
+
|
29
|
+
it "Model#comment should return nil for a model that doesn't have a database comment" do
|
30
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY );"
|
31
|
+
@foxes.comment.should be_nil
|
32
|
+
ActiveRecord::Base.comment(:foxes).should be_nil
|
33
|
+
ActiveRecord::Base.comment('foxes').should be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "Model#comment should return the database comment for a model that has a database comment" do
|
37
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ) COMMENT 'foxes Rule';"
|
38
|
+
@foxes.comment.should == 'foxes Rule'
|
39
|
+
ActiveRecord::Base.comment(:foxes).should == 'foxes Rule'
|
40
|
+
ActiveRecord::Base.comment('foxes').should == 'foxes Rule'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "Model#comment should return the database comment for a model that has a database comment in different formats" do
|
44
|
+
pending
|
45
|
+
#[ 'foxes Rule', 'i has Numbers123', 'i have " double " quotes', "i have ' single quotes'" ].each do
|
46
|
+
# connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ) COMMENT 'foxes Rule';"
|
47
|
+
#@foxes.comment.should == 'foxes Rule'
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
describe ActiveRecord::ConnectionAdapters::Column, 'extensions' do
|
53
|
+
|
54
|
+
it 'Model#column_comment should return the comment for a column' do
|
55
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'i am the ID column' );"
|
56
|
+
@foxes.column_comment('id').should == 'i am the ID column'
|
57
|
+
@foxes.column_comment(:id).should == 'i am the ID column'
|
58
|
+
ActiveRecord::Base.column_comment(:id, :foxes).should == 'i am the ID column'
|
59
|
+
ActiveRecord::Base.column_comment(:id, 'foxes').should == 'i am the ID column'
|
60
|
+
ActiveRecord::Base.column_comment('id', 'foxes').should == 'i am the ID column'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "@column#comment should return nil for a column that doesn't have a database comment" do
|
64
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY );"
|
65
|
+
@foxes.columns.first.name.should == 'id'
|
66
|
+
@foxes.columns.first.comment.should be_nil
|
67
|
+
end
|
68
|
+
|
69
|
+
# need to add this extension (tho it's yucky) so a column can easily find its comment (needs its table name)
|
70
|
+
it "@column should know its #table_name" do
|
71
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY );"
|
72
|
+
@foxes.columns.length.should == 1
|
73
|
+
@foxes.columns.first.table_name.should == 'foxes'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "@column#comment should return the database comment for a column that has a database comment" do
|
77
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'i am the ID column' );"
|
78
|
+
@foxes.columns.first.name.should == 'id'
|
79
|
+
@foxes.columns.first.comment.should == 'i am the ID column'
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'Connection', 'extensions' do
|
85
|
+
|
86
|
+
it "@connection#comment(table) should return the database comment for a table that has a database comment" do
|
87
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ) COMMENT 'foxes Rule';"
|
88
|
+
connection.comment('foxes').should == 'foxes Rule'
|
89
|
+
connection.comment(:foxes).should == 'foxes Rule'
|
90
|
+
end
|
91
|
+
|
92
|
+
it "@connection.columns(table) should inject the table name to column objects" do
|
93
|
+
pending "Not implementing this (for now?)"
|
94
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'i am the ID column' );"
|
95
|
+
connection.columns(:foxes).length.should == 1
|
96
|
+
connection.columns(:foxes).first.name.should == 'id'
|
97
|
+
connection.columns(:foxes).first.comment.should == 'i am the ID column'
|
98
|
+
end
|
99
|
+
|
100
|
+
it "@connection#column_comment should return the database comment for a column that has a database comment" do
|
101
|
+
connection.execute "CREATE TABLE foxes( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'i am the ID column' );"
|
102
|
+
connection.column_comment(:id, :foxes).should == 'i am the ID column'
|
103
|
+
connection.column_comment('id', :foxes).should == 'i am the ID column'
|
104
|
+
connection.column_comment('id', 'foxes').should == 'i am the ID column'
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'yaml'
|
4
|
+
begin
|
5
|
+
require 'activerecord'
|
6
|
+
rescue LoadError
|
7
|
+
raise "dependency ActiveRecord not found. try: $ sudo gem install activerecord"
|
8
|
+
end
|
9
|
+
|
10
|
+
require File.dirname(__FILE__) + '/../lib/activerecord-comments'
|
11
|
+
|
12
|
+
# right now, we run all tests against MySQL (I would also do sqlite but I don't think it supports comments!)
|
13
|
+
database_hash = YAML::load File.read(File.dirname(__FILE__) + '/spec_database.yml')
|
14
|
+
ActiveRecord::Base.establish_connection database_hash
|
15
|
+
|
16
|
+
begin
|
17
|
+
# touch the connection to see if it's OK
|
18
|
+
ActiveRecord::Base.connection
|
19
|
+
rescue Mysql::Error => ex
|
20
|
+
if ex.to_s =~ /unknown database/i
|
21
|
+
db = database_hash['database']
|
22
|
+
raise "\n\nMySQL database not found: #{db}.\ntry: $ mysqladmin create #{db}\n\n"
|
23
|
+
else
|
24
|
+
raise ex
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openrain-activerecord-comments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- remi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-03 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Provides an easy to access database table/column comments from ActiveRecord
|
17
|
+
email: remi@remitaylor.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- README.markdown
|
27
|
+
- lib/activerecord-comments
|
28
|
+
- lib/activerecord-comments/column_ext.rb
|
29
|
+
- lib/activerecord-comments/mysql_adapter.rb
|
30
|
+
- lib/activerecord-comments/abstract_adapter_ext.rb
|
31
|
+
- lib/activerecord-comments/base_ext.rb
|
32
|
+
- lib/activerecord-comments.rb
|
33
|
+
- spec/mysql_comments_spec.rb
|
34
|
+
- spec/spec_database.yml
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/openrain/activerecord-comments
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --inline-source
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.2.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 2
|
62
|
+
summary: Provides an easy to access database table/column comments from ActiveRecord
|
63
|
+
test_files: []
|
64
|
+
|