From fcc7e29ecdee44e04488484ff8740bd5bd8e9719 Mon Sep 17 00:00:00 2001 From: Justin Sookikian Date: Tue, 10 Jan 2017 17:36:11 -0800 Subject: [PATCH 1/6] removing alias_method_chain in favor of prepend --- lib/activeuuid/patches.rb | 91 ++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/lib/activeuuid/patches.rb b/lib/activeuuid/patches.rb index dfb7903..54839cf 100644 --- a/lib/activeuuid/patches.rb +++ b/lib/activeuuid/patches.rb @@ -47,32 +47,35 @@ def uuid(*column_names) module Column extend ActiveSupport::Concern - included do + def self.prepended(klass) def type_cast_with_uuid(value) return UUIDTools::UUID.serialize(value) if type == :uuid - type_cast_without_uuid(value) + super + #type_cast_without_uuid(value) end def type_cast_code_with_uuid(var_name) return "UUIDTools::UUID.serialize(#{var_name})" if type == :uuid - type_cast_code_without_uuid(var_name) + super + #type_cast_code_without_uuid(var_name) end def simplified_type_with_uuid(field_type) return :uuid if field_type == 'binary(16)' || field_type == 'binary(16,0)' - simplified_type_without_uuid(field_type) + super + #simplified_type_without_uuid(field_type) end - alias_method_chain :type_cast, :uuid - alias_method_chain :type_cast_code, :uuid if ActiveRecord::VERSION::MAJOR < 4 - alias_method_chain :simplified_type, :uuid +# alias_method_chain :type_cast, :uuid +# alias_method_chain :type_cast_code, :uuid if ActiveRecord::VERSION::MAJOR < 4 +# alias_method_chain :simplified_type, :uuid end end module MysqlJdbcColumn extend ActiveSupport::Concern - included do + def self.prepended(klass) # This is a really hacky solution, but it's the only way to support the # MySql JDBC adapter without breaking backwards compatibility. # It would be a lot easier if AR had support for custom defined types. @@ -85,11 +88,12 @@ module MysqlJdbcColumn # (5) Since it's no a uuid (see step 3), simplified_type_without_uuid is called, # which maps to AR::ConnectionAdapters::Column.simplified_type (which has no super call, so we're good) # - alias_method :original_simplified_type, :simplified_type + # alias_method :original_simplified_type, :simplified_type def simplified_type(field_type) return :uuid if field_type == 'binary(16)' || field_type == 'binary(16,0)' - original_simplified_type(field_type) + super +# original_simplified_type(field_type) end end end @@ -98,102 +102,109 @@ def simplified_type(field_type) module PostgreSQLColumn extend ActiveSupport::Concern - included do + def self.prepended(klass) def type_cast_with_uuid(value) return UUIDTools::UUID.serialize(value) if type == :uuid - type_cast_without_uuid(value) + super + #type_cast_without_uuid(value) end alias_method_chain :type_cast, :uuid if ActiveRecord::VERSION::MAJOR >= 4 def simplified_type_with_pguuid(field_type) return :uuid if field_type == 'uuid' - simplified_type_without_pguuid(field_type) + super + #simplified_type_without_pguuid(field_type) end - alias_method_chain :simplified_type, :pguuid + #alias_method_chain :simplified_type, :pguuid end end module Quoting extend ActiveSupport::Concern - included do + def self.prepended(klass) def quote_with_visiting(value, column = nil) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid - quote_without_visiting(value, column) + super + #quote_without_visiting(value, column) end def type_cast_with_visiting(value, column = nil) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid - type_cast_without_visiting(value, column) + super + #type_cast_without_visiting(value, column) end def native_database_types_with_uuid @native_database_types ||= native_database_types_without_uuid.merge(uuid: { name: 'binary', limit: 16 }) end - alias_method_chain :quote, :visiting - alias_method_chain :type_cast, :visiting - alias_method_chain :native_database_types, :uuid + #alias_method_chain :quote, :visiting + #alias_method_chain :type_cast, :visiting + #alias_method_chain :native_database_types, :uuid end end module PostgreSQLQuoting extend ActiveSupport::Concern - included do + def self.prepended(klass) def quote_with_visiting(value, column = nil) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid value = value.to_s if value.is_a? UUIDTools::UUID - quote_without_visiting(value, column) + super + #quote_without_visiting(value, column) end def type_cast_with_visiting(value, column = nil, *args) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid value = value.to_s if value.is_a? UUIDTools::UUID - type_cast_without_visiting(value, column, *args) + super + #type_cast_without_visiting(value, column, *args) end def native_database_types_with_pguuid @native_database_types ||= native_database_types_without_pguuid.merge(uuid: { name: 'uuid' }) end - alias_method_chain :quote, :visiting - alias_method_chain :type_cast, :visiting - alias_method_chain :native_database_types, :pguuid + #alias_method_chain :quote, :visiting + #alias_method_chain :type_cast, :visiting + #alias_method_chain :native_database_types, :pguuid end end module AbstractAdapter extend ActiveSupport::Concern - included do + def self.prepended(klass) def initialize_type_map_with_uuid(m) - initialize_type_map_without_uuid(m) + #initialize_type_map_without_uuid(m) + super register_class_with_limit m, /binary\(16(,0)?\)/i, ::ActiveRecord::Type::UUID end - alias_method_chain :initialize_type_map, :uuid + #alias_method_chain :initialize_type_map, :uuid end end def self.apply! - ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table - ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition + ActiveRecord::ConnectionAdapters::Table.send :prepend, Migrations if defined? ActiveRecord::ConnectionAdapters::Table + ActiveRecord::ConnectionAdapters::TableDefinition.send :prepend, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition if ActiveRecord::VERSION::MAJOR == 4 and ActiveRecord::VERSION::MINOR == 2 - ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :include, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter - ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter + ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :prepend, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter + ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :prepend, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter else - ActiveRecord::ConnectionAdapters::Column.send :include, Column - ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn + ActiveRecord::ConnectionAdapters::Column.send :prepend, Column + ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :prepend, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn end - ArJdbc::MySQL::Column.send :include, MysqlJdbcColumn if defined? ArJdbc::MySQL::Column + ArJdbc::MySQL::Column.send :prepend, MysqlJdbcColumn if defined? ArJdbc::MySQL::Column - ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter - ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter - ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter - ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgreSQLQuoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter + ActiveRecord::ConnectionAdapters::MysqlAdapter.send :prepend, Quoting if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter + ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :prepend, Quoting if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter + ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :prepend, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter + ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :prepend, PostgreSQLQuoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter end end end From 1c32cb765293e8347740b03b896f71c73c63c999 Mon Sep 17 00:00:00 2001 From: Justin Sookikian Date: Thu, 12 Jan 2017 17:21:02 -0800 Subject: [PATCH 2/6] Refactored alias_method_chain for deprecation purposes --- Gemfile | 4 +- lib/activeuuid/patches.rb | 110 ++++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/Gemfile b/Gemfile index 5562177..9607a9c 100644 --- a/Gemfile +++ b/Gemfile @@ -2,5 +2,5 @@ source "http://rubygems.org" # Specify your gem's dependencies in activeuuid.gemspec gemspec - -gem "activerecord" +gem 'rake', '< 11.0' +gem "activerecord", "~>5.0" diff --git a/lib/activeuuid/patches.rb b/lib/activeuuid/patches.rb index 54839cf..84b1591 100644 --- a/lib/activeuuid/patches.rb +++ b/lib/activeuuid/patches.rb @@ -1,7 +1,8 @@ require 'active_record' require 'active_support/concern' -if ActiveRecord::VERSION::MAJOR == 4 and ActiveRecord::VERSION::MINOR == 2 +if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) || + (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0) module ActiveRecord module Type class UUID < Binary # :nodoc: @@ -9,6 +10,11 @@ def type :uuid end + def serialize(value) + return if value.nil? + UUIDTools::UUID.serialize(value) + end + def cast_value(value) UUIDTools::UUID.serialize(value) end @@ -24,7 +30,6 @@ class Uuid < Type::Value # :nodoc: def type_cast_from_user(value) UUIDTools::UUID.serialize(value) if value end - alias_method :type_cast_from_database, :type_cast_from_user end end end @@ -51,31 +56,24 @@ def self.prepended(klass) def type_cast_with_uuid(value) return UUIDTools::UUID.serialize(value) if type == :uuid super - #type_cast_without_uuid(value) end def type_cast_code_with_uuid(var_name) return "UUIDTools::UUID.serialize(#{var_name})" if type == :uuid super - #type_cast_code_without_uuid(var_name) end def simplified_type_with_uuid(field_type) return :uuid if field_type == 'binary(16)' || field_type == 'binary(16,0)' super - #simplified_type_without_uuid(field_type) end - -# alias_method_chain :type_cast, :uuid -# alias_method_chain :type_cast_code, :uuid if ActiveRecord::VERSION::MAJOR < 4 -# alias_method_chain :simplified_type, :uuid end end module MysqlJdbcColumn extend ActiveSupport::Concern - def self.prepended(klass) + included do # This is a really hacky solution, but it's the only way to support the # MySql JDBC adapter without breaking backwards compatibility. # It would be a lot easier if AR had support for custom defined types. @@ -88,12 +86,11 @@ def self.prepended(klass) # (5) Since it's no a uuid (see step 3), simplified_type_without_uuid is called, # which maps to AR::ConnectionAdapters::Column.simplified_type (which has no super call, so we're good) # - # alias_method :original_simplified_type, :simplified_type + alias_method :original_simplified_type, :simplified_type def simplified_type(field_type) return :uuid if field_type == 'binary(16)' || field_type == 'binary(16,0)' - super -# original_simplified_type(field_type) + original_simplified_type(field_type) end end end @@ -106,17 +103,13 @@ def self.prepended(klass) def type_cast_with_uuid(value) return UUIDTools::UUID.serialize(value) if type == :uuid super - #type_cast_without_uuid(value) end alias_method_chain :type_cast, :uuid if ActiveRecord::VERSION::MAJOR >= 4 def simplified_type_with_pguuid(field_type) return :uuid if field_type == 'uuid' super - #simplified_type_without_pguuid(field_type) end - - #alias_method_chain :simplified_type, :pguuid end end @@ -127,22 +120,16 @@ def self.prepended(klass) def quote_with_visiting(value, column = nil) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid super - #quote_without_visiting(value, column) end def type_cast_with_visiting(value, column = nil) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid super - #type_cast_without_visiting(value, column) end def native_database_types_with_uuid @native_database_types ||= native_database_types_without_uuid.merge(uuid: { name: 'binary', limit: 16 }) end - - #alias_method_chain :quote, :visiting - #alias_method_chain :type_cast, :visiting - #alias_method_chain :native_database_types, :uuid end end @@ -154,57 +141,74 @@ def quote_with_visiting(value, column = nil) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid value = value.to_s if value.is_a? UUIDTools::UUID super - #quote_without_visiting(value, column) end def type_cast_with_visiting(value, column = nil, *args) value = UUIDTools::UUID.serialize(value) if column && column.type == :uuid value = value.to_s if value.is_a? UUIDTools::UUID super - #type_cast_without_visiting(value, column, *args) end def native_database_types_with_pguuid @native_database_types ||= native_database_types_without_pguuid.merge(uuid: { name: 'uuid' }) end + end + end - #alias_method_chain :quote, :visiting - #alias_method_chain :type_cast, :visiting - #alias_method_chain :native_database_types, :pguuid + module PostgresqlTypeOverride + def deserialize(value) + UUIDTools::UUID.serialize(value) if value end + + alias_method :cast, :deserialize end - module AbstractAdapter - extend ActiveSupport::Concern + module TypeMapOverride + def initialize_type_map(m) + super - def self.prepended(klass) - def initialize_type_map_with_uuid(m) - #initialize_type_map_without_uuid(m) - super - register_class_with_limit m, /binary\(16(,0)?\)/i, ::ActiveRecord::Type::UUID - end + register_class_with_limit m, /binary\(16(,0)?\)/i, ::ActiveRecord::Type::UUID + end + end - #alias_method_chain :initialize_type_map, :uuid + module MysqlTypeToSqlOverride + def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil) + type.to_s == 'uuid' ? 'binary(16)' : super end end - def self.apply! - ActiveRecord::ConnectionAdapters::Table.send :prepend, Migrations if defined? ActiveRecord::ConnectionAdapters::Table - ActiveRecord::ConnectionAdapters::TableDefinition.send :prepend, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition - - if ActiveRecord::VERSION::MAJOR == 4 and ActiveRecord::VERSION::MINOR == 2 - ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :prepend, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter - ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :prepend, AbstractAdapter if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter - else - ActiveRecord::ConnectionAdapters::Column.send :prepend, Column - ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :prepend, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn + module ConnectionHandling + def establish_connection(_ = nil) + super + + ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table + ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition + + if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0 + if defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend TypeMapOverride + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend MysqlTypeToSqlOverride + end + + ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend TypeMapOverride if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter + ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid.prepend PostgresqlTypeOverride if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter + elsif ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2 + ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend TypeMapOverride if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter + ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend TypeMapOverride if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter + else + ActiveRecord::ConnectionAdapters::Column.send :include, Column + ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn + end + + ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter + ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter + ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter + ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgreSQLQuoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter end - ArJdbc::MySQL::Column.send :prepend, MysqlJdbcColumn if defined? ArJdbc::MySQL::Column + end - ActiveRecord::ConnectionAdapters::MysqlAdapter.send :prepend, Quoting if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter - ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :prepend, Quoting if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter - ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :prepend, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter - ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :prepend, PostgreSQLQuoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter + def self.apply! + ActiveRecord::Base.singleton_class.prepend ConnectionHandling end end -end +end \ No newline at end of file From 19a492e042b462f188f18802e83d9f9f5d70c835 Mon Sep 17 00:00:00 2001 From: Justin Sookikian Date: Fri, 13 Jan 2017 11:03:16 -0800 Subject: [PATCH 3/6] Temporarily adding in last_comment method --- Rakefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Rakefile b/Rakefile index 5ed955d..3222e8e 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,13 @@ require "bundler/gem_tasks" require 'rspec/core' require 'rspec/core/rake_task' +module TempFixForRakeLastComment + def last_comment + last_description + end +end +Rake::Application.send :include, TempFixForRakeLastComment + RSpec::Core::RakeTask.new(:spec) task :default => :spec From 915c2c05ea1929a0bc0f2f7f5101966393494cae Mon Sep 17 00:00:00 2001 From: Justin Sookikian Date: Wed, 18 Jan 2017 15:45:51 -0800 Subject: [PATCH 4/6] Update Rakefile --- Rakefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Rakefile b/Rakefile index 3222e8e..c14d4b0 100644 --- a/Rakefile +++ b/Rakefile @@ -3,12 +3,12 @@ require "bundler/gem_tasks" require 'rspec/core' require 'rspec/core/rake_task' -module TempFixForRakeLastComment - def last_comment - last_description - end -end -Rake::Application.send :include, TempFixForRakeLastComment +#module TempFixForRakeLastComment +# def last_comment +# last_description +# end +#end +#Rake::Application.send :include, TempFixForRakeLastComment RSpec::Core::RakeTask.new(:spec) From 39f1600cfba005883e9f0fa44db4b40a3d6871ec Mon Sep 17 00:00:00 2001 From: Justin Sookikian Date: Wed, 18 Jan 2017 15:54:40 -0800 Subject: [PATCH 5/6] Update .travis.yml --- .travis.yml | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index c9c94c2..db20e07 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,8 +12,6 @@ rvm: gemfile: - Gemfile - - gemfiles/Gemfile.rails-3-1 - - gemfiles/Gemfile.rails-3-2 - gemfiles/Gemfile.rails-4-0 - gemfiles/Gemfile.rails-4-1 - gemfiles/Gemfile.rails-4-2 @@ -44,26 +42,3 @@ matrix: gemfile: gemfiles/Gemfile.rails-3-2 - rvm: 2.3.1 gemfile: gemfiles/Gemfile.rails-3-2 - - rvm: jruby - gemfile: gemfiles/Gemfile.rails-3-2 - env: DB=postgresql - - rvm: jruby - gemfile: gemfiles/Gemfile.rails-4-0 - env: DB=postgresql - - rvm: jruby - gemfile: gemfiles/Gemfile.rails-4-1 - env: DB=postgresql - - rvm: 2.0.0 - gemfile: gemfiles/Gemfile.rails-5-0 - - rvm: 2.1.10 - gemfile: gemfiles/Gemfile.rails-5-0 - - rvm: jruby - gemfile: gemfiles/Gemfile.rails-5-0 - env: DB=postgresql - - rvm: 2.0.0 - gemfile: gemfiles/Gemfile.rails-head - - rvm: 2.1.10 - gemfile: gemfiles/Gemfile.rails-head - - rvm: jruby - gemfile: gemfiles/Gemfile.rails-head - env: DB=postgresql From 0c9e5acf8fd89bf3fa60240e5edfde937cf693df Mon Sep 17 00:00:00 2001 From: Justin Sookikian Date: Wed, 18 Jan 2017 16:00:10 -0800 Subject: [PATCH 6/6] Update Rakefile --- Rakefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Rakefile b/Rakefile index c14d4b0..3222e8e 100644 --- a/Rakefile +++ b/Rakefile @@ -3,12 +3,12 @@ require "bundler/gem_tasks" require 'rspec/core' require 'rspec/core/rake_task' -#module TempFixForRakeLastComment -# def last_comment -# last_description -# end -#end -#Rake::Application.send :include, TempFixForRakeLastComment +module TempFixForRakeLastComment + def last_comment + last_description + end +end +Rake::Application.send :include, TempFixForRakeLastComment RSpec::Core::RakeTask.new(:spec)