Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails5 Update #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
44 changes: 15 additions & 29 deletions lib/activeuuid/patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'active_support/concern'

if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) ||
(ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0)
(ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0)
module ActiveRecord
module Type
class UUID < Binary # :nodoc:
Expand Down Expand Up @@ -52,25 +52,21 @@ 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
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
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
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

Expand Down Expand Up @@ -103,69 +99,59 @@ 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
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
end

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
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
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

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
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
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
end
end

Expand Down Expand Up @@ -225,4 +211,4 @@ def self.apply!
ActiveRecord::Base.singleton_class.prepend ConnectionHandling
end
end
end
end