numeric_type_column 0.0.1

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 2012 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.md ADDED
@@ -0,0 +1,7 @@
1
+ numeric_type_column
2
+ =======================
3
+
4
+ A collection of patches to Rails ActiveRecord that supports numeric type columns, which helps with partitioning in MySQL
5
+
6
+ I'm currently importing my code from a private project I am currently working on, which has to be re-organized, and re-named,
7
+ so don't expect this gem to work right now - Wait a couple of days: I do need make this gem usable ASAP!
data/Rakefile ADDED
@@ -0,0 +1,38 @@
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 = 'NumericTypeColumn'
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
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module NumericTypeColumn
4
+ autoload :ActiveRecord, 'numeric_type_column/active_record'
5
+ end
6
+
7
+ require 'numeric_type_column/railtie' if defined?(Rails)
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module NumericTypeColumn
4
+
5
+ module ActiveRecord
6
+ autoload :AbstractMysqlAdapterExtension, 'numeric_type_column/active_record/abstract_mysql_adapter_extension'
7
+ autoload :BaseExtension, 'numeric_type_column/active_record/base_extension'
8
+ autoload :BelongsToPolimorphicAssociationExtension, 'numeric_type_column/active_record/belongs_to_polimorphic_association_extension'
9
+ autoload :ColumnDefinitionExtension, 'numeric_type_column/active_record/column_definition_extension'
10
+ autoload :TableDefinitionExtension, 'numeric_type_column/active_record/table_definition_extension'
11
+ end
12
+
13
+ end
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+
3
+ module NumericTypeColumn
4
+
5
+ module ActiveRecord
6
+
7
+ module AbstractMysqlAdapterExtension
8
+
9
+ def self.included(base)
10
+ #puts "NumericTypeColumn::ActiveRecord::AbstractMysqlAdapterExtension included to #{base.name}!"
11
+ base::NATIVE_DATABASE_TYPES[:primary_key] = 'INT UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY'
12
+ base.send :alias_method_chain, :add_column_sql, :unsigned
13
+ base.send :alias_method_chain, :type_to_sql, :unsigned
14
+ base.send :alias_method_chain, :add_column_options!, :comment
15
+ #base.send :alias_method_chain, :create_table, :mysql_options
16
+ end
17
+
18
+ =begin
19
+
20
+ class Column < ConnectionAdapters::Column # :nodoc:
21
+ attr_reader :unsigned
22
+
23
+ def initialize_with_unsigned(name, default, sql_type = nil, null = true)
24
+ initialize_without_unsigned(name, default, sql_type, null)
25
+ @unsigned = extract_unsigned(sql_type)
26
+ end
27
+ alias_method_chain :initialize, :unsigned
28
+
29
+ def extract_unsigned(sql_type)
30
+ false
31
+ end
32
+ end
33
+ =end
34
+ =begin
35
+ def create_table_with_mysql_options(table_name, options = {}) #:nodoc:
36
+ puts "NumericTypeColumn::ActiveRecord::AbstractMysqlAdapterExtension: create_table(table_name: #{table_name}, options: #{options})"
37
+ create_table_without_mysql_options(table_name, options.reverse_merge(options: options[:mysql_options] ? options[:mysql_options] : "ENGINE=InnoDB"))
38
+ end
39
+ =end
40
+ protected
41
+
42
+ def add_column_sql_with_unsigned(table_name, column_name, type, options = {})
43
+ #puts "NumericTypeColumn::ActiveRecord::AbstractMysqlAdapterExtension: add_column_sql_with_unsigned(table_name: #{table_name}, column_name: #{column_name}, type: #{type}, options: #{options})"
44
+ is_unsigned_valid = (((options.has_key? :unsigned) && (options[:unsigned] == true)) && (['integer', 'boolean'].include? type.to_s))
45
+ add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], is_unsigned_valid)}"
46
+ add_column_options!(add_column_sql, options)
47
+ add_column_position!(add_column_sql, options)
48
+ add_column_sql
49
+ end
50
+
51
+ def type_to_sql_with_unsigned(type, limit = nil, precision = nil, scale = nil, unsigned = false)
52
+ #puts "NumericTypeColumn::ActiveRecord::AbstractMysqlAdapterExtension: type_to_sql_with_unsigned(type: #{type}, limit: #{limit}, precision: #{precision}, scale: #{scale}, unsigned: #{unsigned})..."
53
+ sql = type_to_sql_without_unsigned(type, limit, precision, scale)
54
+ sql << ' UNSIGNED' if unsigned && (['integer', 'boolean'].include? type.to_s)
55
+ sql
56
+ end
57
+
58
+ def add_column_options_with_comment!(sql, options = {})
59
+ #puts "NumericTypeColumn::ActiveRecord::AbstractMysqlAdapterExtension: add_column_options_with_comment!(sql: #{sql}, options: #{})"
60
+ add_column_options_without_comment!(sql, options)
61
+ sql << " COMMENT '#{options[:comment]}'" if options[:comment]
62
+ sql
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+
3
+ module NumericTypeColumn
4
+
5
+ module ActiveRecord
6
+
7
+ module BaseExtension
8
+
9
+ # http://yehudakatz.com/2009/11/12/better-ruby-idioms/
10
+
11
+ attr_accessor :inheritance_map
12
+
13
+ def has_numeric_sti_column(sti_column, options = {})
14
+ self.inheritance_column = sti_column
15
+ self.store_full_sti_class = options[:store_full_sti_class] if options.has_key?(:store_full_sti_class)
16
+ self.inheritance_map = options[:inheritance_map] if options.has_key?(:inheritance_map)
17
+
18
+ extend ClassMethods
19
+ include InstanceMethods
20
+ end
21
+
22
+ module ClassMethods
23
+ private
24
+
25
+ # ActiveRecord::Inheritance::ClassMethods#find_sti_class
26
+ def find_sti_class(type_id)
27
+ puts "(MonkeyPatch) NumericTypeColumn::ActiveRecord::BaseExtension: find_sti_class(type_id: #{type_id} )... #{base_class.inheritance_map.inspect}"
28
+ base_class.inheritance_map[type_id].constantize
29
+ end
30
+
31
+ # ActiveRecord::Inheritance::ClassMethods#type_condition
32
+ def type_condition(table = arel_table)
33
+ #puts "(MonkeyPatch) VovCore::HasNumericInheritanceColumn::ClassMethods#type_condition"
34
+ sti_column = table[inheritance_column.to_sym]
35
+ sti_values = ([self] + descendants).map { |model| base_class.inheritance_map.key(model.sti_name) }
36
+ sti_column.in(sti_values)
37
+ end
38
+
39
+ # ActiveRecord::Base
40
+ def relation #:nodoc:
41
+ #puts "(MonkeyPatch) VovCore::HasNumericInheritanceColumn::ClassMethods#relation"
42
+ @relation ||= ::ActiveRecord::Relation.new(self, arel_table)
43
+ if finder_needs_type_condition?
44
+ sti_numeric_value = base_class.inheritance_map.key(sti_name)
45
+ @relation.where(type_condition).create_with(inheritance_column.to_sym => sti_numeric_value)
46
+ else
47
+ @relation
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ module InstanceMethods
54
+
55
+ private
56
+
57
+ # ActiveRecord::Persistence::ClassMethods
58
+ def create_or_update
59
+ #puts "(MonkeyPatch) VovCore::HasNumericInheritanceColumn::InstanceMethods#create_or_update - self.class.inheritance_column.to_sym: #{self.class.inheritance_column.to_sym}"
60
+
61
+ # Setear el atributo de inheritance_column:
62
+ send("#{self.class.inheritance_column.to_sym}=", self.class.base_class.inheritance_map.key(self.class.sti_name))
63
+
64
+ raise ReadOnlyRecord if readonly?
65
+ result = new_record? ? create : update
66
+ result != false
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ module NumericTypeColumn
4
+ module ActiveRecord
5
+
6
+ module BelongsToPolymorphicAssociationExtension
7
+
8
+ def self.included(base)
9
+ #puts "NumericTypeColumn::ActiveRecord::BelongsToPolymorphicAssociationExtension included to #{base.name}!"
10
+ base.send :alias_method_chain, :klass, :numeric_type_column
11
+ base.send :alias_method_chain, :replace_keys, :numeric_type_column
12
+ end
13
+
14
+ def klass_with_numeric_type_column
15
+
16
+ #puts "NumericTypeColumn::ActiveRecord::BelongsToPolymorphicAssociationExtension: klass_with_numeric_type_column"
17
+
18
+ type = owner[reflection.foreign_type]
19
+
20
+ # Hack: Llama al metodo en caso de que no exista el atributo con el nombre:
21
+ type = owner.send(reflection.foreign_type) unless !type.nil?
22
+
23
+ type.presence && type.constantize
24
+ end
25
+
26
+ private
27
+
28
+ def replace_keys_with_numeric_type_column(record)
29
+
30
+ #puts "NumericTypeColumn::ActiveRecord::BelongsToPolymorphicAssociationExtension: replace_keys_with_numeric_type_column(record: #{record})"
31
+
32
+ super
33
+ if owner.attribute_names.include?(reflection.foreign_type)
34
+ owner[reflection.foreign_type] = record && record.class.base_class.name
35
+ else
36
+ # Hack: llama al método '=' con .send, para que funcione en caso de que no sea un atributo, sino un método:
37
+ owner.send "#{reflection.foreign_type}=", record && record.class.base_class.name
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ module NumericTypeColumn
5
+
6
+ module ActiveRecord
7
+
8
+ # Basado en http://thewebfellas.com/blog/2008/6/2/unsigned-integers-for-mysql-on-rails
9
+ module ColumnDefinitionExtension
10
+
11
+ attr_accessor :unsigned, :comment
12
+
13
+ def self.included(base)
14
+ #puts "NumericTypeColumn::ActiveRecord::ColumnDefinitionExtension included to #{base.name}!"
15
+ base.send :alias_method_chain, :sql_type, :unsigned
16
+ base.send :alias_method_chain, :to_sql, :extra_options
17
+ end
18
+
19
+ # Override de #sql_type
20
+ def sql_type_with_unsigned
21
+ #puts "NumericTypeColumn::ActiveRecord::ColumnDefinitionExtension: sql_type_with_unsigned"
22
+ # Cambio: signature de call base.type_to_sql:
23
+ if base.method(:type_to_sql).parameters.include?([:opt, :unsigned])
24
+ base.type_to_sql(type.to_sym, limit, precision, scale, unsigned) rescue type
25
+ else
26
+ base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
27
+ end
28
+
29
+ end
30
+
31
+ # Override de #to_sql
32
+ def to_sql_with_extra_options
33
+ #puts "NumericTypeColumn::ActiveRecord::ColumnDefinitionExtension: to_sql_with_extra_options"
34
+ column_sql = "#{base.quote_column_name(name)} #{sql_type}"
35
+ column_options = {}
36
+ column_options[:null] = null unless null.nil?
37
+ column_options[:default] = default unless default.nil?
38
+
39
+ # Cambio: column_options[:comment]
40
+ column_options[:comment] = comment unless comment.nil?
41
+
42
+ add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
43
+ column_sql
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ module NumericTypeColumn
4
+
5
+ module ActiveRecord
6
+
7
+ module TableDefinitionExtension
8
+
9
+ def self.included(base)
10
+ #puts "NumericTypeColumn::ActiveRecord::TableDefinitionExtension included to #{base.name}!"
11
+ base.send :alias_method_chain, :column, :unsigned_and_comment
12
+ end
13
+
14
+ def column_with_unsigned_and_comment(name, type, options = {})
15
+ #puts "NumericTypeColumn::ActiveRecord::TableDefinitionExtension: column_with_unsigned_and_comment(name: #{name}, type: #{type}, options = {})..."
16
+ ret_column = column_without_unsigned_and_comment(name, type, options)
17
+ ret_column[name].unsigned = options[:unsigned]
18
+ ret_column[name].comment = options[:comment]
19
+ ret_column
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,23 @@
1
+ module NumericTypeColumn
2
+ class Railtie < ::Rails::Railtie #:nodoc:
3
+
4
+ initializer 'numeric_type_column' do |app|
5
+
6
+ ActiveSupport.on_load(:active_record) do
7
+
8
+ # Patches para permitir definir columnas UNSIGNED y COMMENTS en MySQL:
9
+ ::ActiveRecord::ConnectionAdapters::ColumnDefinition.send :include, NumericTypeColumn::ActiveRecord::ColumnDefinitionExtension
10
+ ::ActiveRecord::ConnectionAdapters::TableDefinition.send :include, NumericTypeColumn::ActiveRecord::TableDefinitionExtension
11
+ ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.send :include, NumericTypeColumn::ActiveRecord::AbstractMysqlAdapterExtension
12
+
13
+ # Patch para permitir STI con columna numerica:
14
+ ::ActiveRecord::Base.extend NumericTypeColumn::ActiveRecord::BaseExtension
15
+
16
+ # Patch para permitir polymorphs con columna foreign_type numerica:
17
+ #::ActiveRecord::Associations::BelongsToPolymorphicAssociation.send :include, NumericTypeColumn::ActiveRecord::BelongsToPolymorphicAssociationExtension
18
+
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module NumericTypeColumn
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numeric_type_column
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roberto Quintanilla Gonzalez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 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.3
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.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
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: A collection of patches to Rails ActiveRecord - Unsigned Integer Columns,
63
+ numeric STI column, numeric Polymorphic type column - which helps with partitioning
64
+ in MySQL.
65
+ email:
66
+ - roberto.quintanilla@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/numeric_type_column/active_record/abstract_mysql_adapter_extension.rb
72
+ - lib/numeric_type_column/active_record/base_extension.rb
73
+ - lib/numeric_type_column/active_record/belongs_to_polymorphic_association_extension.rb
74
+ - lib/numeric_type_column/active_record/column_definition_extension.rb
75
+ - lib/numeric_type_column/active_record/table_definition_extension.rb
76
+ - lib/numeric_type_column/active_record.rb
77
+ - lib/numeric_type_column/railtie.rb
78
+ - lib/numeric_type_column/version.rb
79
+ - lib/numeric_type_column.rb
80
+ - MIT-LICENSE
81
+ - Rakefile
82
+ - README.md
83
+ homepage: http://apphouse.naranya.net
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A collection of patches to Rails ActiveRecord that supports numeric type
107
+ columns.
108
+ test_files: []