activerecord-database_comments 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = CommentableColumn
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'CommentableColumn'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+ module ActiveRecord
3
+ module DatabaseComments
4
+
5
+ module Adapters
6
+
7
+ module MysqlColumn
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ attr_reader :comment
13
+ alias_method_chain :initialize, :comment
14
+ end
15
+
16
+ def initialize_with_comment(name, default, sql_type = nil, null = true, collation = nil, comment = nil)
17
+ initialize_without_comment(name, default, sql_type, null, collation)
18
+ @comment = comment if comment.present?
19
+ end
20
+
21
+ end
22
+
23
+ module MysqlAdapter
24
+
25
+ extend ActiveSupport::Concern
26
+
27
+ included do
28
+ alias_method_chain :add_column_options!, :comment
29
+ alias_method_chain :columns, :comment
30
+ alias_method_chain :new_column, :comment
31
+ # alias_method_chain :create_table, :mysql_options
32
+
33
+ # Incluir patch a self::Column:
34
+ "#{self.name}::Column".constantize.send :include, ActiveRecord::DatabaseComments::Adapters::MysqlColumn
35
+ end
36
+
37
+ =begin
38
+ def create_table_with_mysql_options(table_name, options = {}) #:nodoc:
39
+ create_table_without_mysql_options(table_name, options.reverse_merge(options: options[:mysql_options] ? options[:mysql_options] : "ENGINE=InnoDB"))
40
+ end
41
+ =end
42
+ def new_column_with_comment(field, default, type, null, collation, comment) # :nodoc:
43
+ "#{self.class.name}::Column".constantize.new(field, default, type, null, collation, comment)
44
+ end
45
+
46
+ def table_options(table_name)
47
+ sql = "SHOW TABLE STATUS "
48
+ sql << "IN #{quote_table_name(current_database)} "
49
+ sql << "LIKE #{quote(table_name)}"
50
+ ohmy = execute_and_free(sql, 'SCHEMA') do |result|
51
+ each_hash(result).map do |field|
52
+ "ENGINE=#{field[:Engine]} COLLATE=#{field[:Collation]} COMMENT='#{field[:Comment]}'"
53
+ end
54
+ end
55
+ ohmy.first
56
+ end
57
+
58
+ # Returns an array of +Column+ objects for the table specified by +table_name+.
59
+ def columns_with_comment(table_name, name = nil)#:nodoc:
60
+ sql = "SHOW FULL FIELDS FROM #{quote_table_name(table_name)}"
61
+ execute_and_free(sql, 'SCHEMA') do |result|
62
+ each_hash(result).map do |field|
63
+ new_column_with_comment(field[:Field], field[:Default], field[:Type], field[:Null] == "YES", field[:Collation], field[:Comment])
64
+ end
65
+ end
66
+ end
67
+
68
+ protected
69
+
70
+ def add_column_sql_with_unsigned(table_name, column_name, type, options = {})
71
+ is_unsigned_valid = (((options.has_key? :unsigned) && (options[:unsigned] == true)) && (['integer', 'decimal', 'float', 'boolean'].include? type.to_s))
72
+ add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], is_unsigned_valid)}"
73
+ add_column_options!(add_column_sql, options)
74
+ add_column_position!(add_column_sql, options)
75
+ add_column_sql
76
+ end
77
+
78
+ def type_to_sql_with_unsigned(type, limit = nil, precision = nil, scale = nil, unsigned = false)
79
+ sql = type_to_sql_without_unsigned(type, limit, precision, scale)
80
+ sql << ' UNSIGNED' if unsigned && (['integer', 'decimal', 'float', 'boolean'].include? type.to_s)
81
+ sql
82
+ end
83
+
84
+ def add_column_options_with_comment!(sql, options = {})
85
+ add_column_options_without_comment!(sql, options)
86
+ sql << " COMMENT '#{options[:comment]}'" if options[:comment]
87
+ sql
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ module ActiveRecord
3
+ module DatabaseComments
4
+ module Definitions
5
+
6
+ module Column
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ attr_accessor :comment
12
+ alias_method_chain :to_sql, :comments
13
+ end
14
+
15
+ # Override de #to_sql
16
+ def to_sql_with_comments
17
+
18
+ column_sql = "#{base.quote_column_name(name)} #{sql_type}"
19
+ column_options = {}
20
+ column_options[:null] = null unless null.nil?
21
+ column_options[:default] = default unless default.nil?
22
+
23
+ # Cambio: column_options[:comment]
24
+ column_options[:comment] = comment unless comment.nil?
25
+
26
+ add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
27
+ column_sql
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ module ActiveRecord
3
+ module DatabaseComments
4
+ module Definitions
5
+
6
+ module Table
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ alias_method_chain :column, :comment
12
+ end
13
+
14
+ def column_with_comment(name, type, options = {})
15
+ ret_column = column_without_comment(name, type, options)
16
+ ret_column[name].comment = options[:comment]
17
+ ret_column
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ module ActiveRecord
3
+ module DatabaseComments
4
+
5
+ class Railtie < ::Rails::Railtie #:nodoc:
6
+
7
+ initializer 'active_record.extended_support.database_comments' do |app|
8
+
9
+ ActiveSupport.on_load(:active_record) do
10
+
11
+ # Patches para permitir definir columnas con COMMENTS en MySQL:
12
+ # Agregar el módulo para soportar definiciones de columnas unsigned:
13
+ require 'active_record/database_comments/definitions/column'
14
+ ActiveRecord::ConnectionAdapters::ColumnDefinition.send :include, ActiveRecord::DatabaseComments::Definitions::Column
15
+
16
+ # Agregar el módulo para soportar definiciones de tablas con comentarios en columnas y tabla:
17
+ require 'active_record/database_comments/definitions/table'
18
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, ActiveRecord::DatabaseComments::Definitions::Table
19
+
20
+ # Agregar el módulo a los adapters de BD:
21
+ require 'active_record/database_comments/adapters/mysql_adapter'
22
+ [
23
+ "ActiveRecord::ConnectionAdapters::MysqlAdapter",
24
+ "ActiveRecord::ConnectionAdapters::Mysql2Adapter",
25
+ "ActiveRecord::ConnectionAdapters::Mysql2SpatialAdapter", # Adapter para rgeo-mysql
26
+ "ArJdbc::MySQL" # Adapter para jRuby JDBC
27
+ ].each do |mysql_adapter_class|
28
+ adapter_class = mysql_adapter_class.safe_constantize
29
+ adapter_class.send :include, ActiveRecord::DatabaseComments::Adapters::MysqlAdapter if adapter_class.present?
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require 'active_record/database_comments/railtie' if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-database_comments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roberto Quintanilla
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.12
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.12
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord-database_schema
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.6
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.6
46
+ - !ruby/object:Gem::Dependency
47
+ name: mysql2
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Allows to add column and table comments to ActiveRecord migrations.
63
+ email:
64
+ - roberto.quintanilla@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/active_record/database_comments/adapters/mysql_adapter.rb
70
+ - lib/active_record/database_comments/definitions/column.rb
71
+ - lib/active_record/database_comments/definitions/table.rb
72
+ - lib/active_record/database_comments/railtie.rb
73
+ - lib/activerecord-database_comments.rb
74
+ - MIT-LICENSE
75
+ - Rakefile
76
+ - README.rdoc
77
+ homepage: https://github.com/vovimayhem
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.25
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Allows to add column and table comments to ActiveRecord migrations.
101
+ test_files: []