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

Rails 4 Support (Updated, w/ smdern's patches) + Ms SqlServer Support #37

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .rvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rvm use 1.9.3@activeuuid --create
rvm use 2.0.0@activeuuid --create
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ source "http://rubygems.org"

# Specify your gem's dependencies in activeuuid.gemspec
gemspec

gem "activerecord"
5 changes: 3 additions & 2 deletions activeuuid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ Gem::Specification.new do |s|

s.add_development_dependency "rake"
s.add_development_dependency "rspec"
s.add_development_dependency "activesupport"
s.add_development_dependency "activesupport", ">= 4.0"
s.add_development_dependency "database_cleaner"
s.add_development_dependency "forgery"
s.add_development_dependency "fabrication"
s.add_development_dependency "sqlite3"
s.add_development_dependency "pg"
s.add_development_dependency "mysql2"

s.add_dependency "activerecord", ">= 4.0"

s.add_runtime_dependency "uuidtools"
s.add_runtime_dependency "activerecord", '>= 3.1'
end
16 changes: 11 additions & 5 deletions lib/activeuuid/patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module Migrations
def uuid(*column_names)
options = column_names.extract_options!
column_names.each do |name|
type = @base.adapter_name.downcase == 'postgresql' ? 'uuid' : 'binary(16)'
type = case ActiveRecord::Base.connection.adapter_name.downcase
when 'postgresql' then 'uuid'
when 'sqlserver' then 'uniqueidentifier'
else 'binary(16)'
end
column(name, "#{type}#{' PRIMARY KEY' if options.delete(:primary_key)}", options)
end
end
Expand All @@ -34,7 +38,6 @@ def simplified_type_with_uuid(field_type)
end

alias_method_chain :type_cast, :uuid
alias_method_chain :type_cast_code, :uuid
alias_method_chain :simplified_type, :uuid
end
end
Expand Down Expand Up @@ -103,15 +106,18 @@ def native_database_types_with_pguuid
end

def self.apply!
is_pg = defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
has_uuid_type = is_pg and Rails.version[0].to_i >= 4

ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition

ActiveRecord::ConnectionAdapters::Column.send :include, Column
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
ActiveRecord::ConnectionAdapters::Column.send :include, Column unless has_uuid_type
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if is_pg and !has_uuid_type

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::PostgreSQLAdapter.send :include, PostgreSQLQuoting if is_pg and !has_uuid_type
end
end
end
18 changes: 9 additions & 9 deletions lib/activeuuid/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ def self.parse_string(str)
module Arel
module Visitors
class DepthFirst < Arel::Visitors::Visitor
def visit_UUIDTools_UUID(o)
o.quoted_id
def visit_UUIDTools_UUID(object, attribute)
object.quoted_id
end
end

class MySQL < Arel::Visitors::ToSql
def visit_UUIDTools_UUID(o)
o.quoted_id
def visit_UUIDTools_UUID(object, attribute)
object.quoted_id
end
end

class SQLite < Arel::Visitors::ToSql
def visit_UUIDTools_UUID(o)
o.quoted_id
def visit_UUIDTools_UUID(object, attribute)
object.quoted_id
end
end

class PostgreSQL < Arel::Visitors::ToSql
def visit_UUIDTools_UUID(o)
"'#{o.to_s}'"
def visit_UUIDTools_UUID(object, attribute)
"'#{object.to_s}'"
end
end
end
Expand Down Expand Up @@ -114,7 +114,7 @@ def uuids(*attributes)
EOS
end

def instantiate_with_uuid(record)
def instantiate_with_uuid(record, column_types)
uuid_columns.each do |uuid_column|
record[uuid_column] = UUIDTools::UUID.serialize(record[uuid_column]).to_s if record[uuid_column]
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
adapters.sqlite3 { column.sql_type.should == 'binary(16)' }
adapters.mysql2 { column.sql_type.should == 'binary(16)' }
adapters.postgresql { column.sql_type.should == 'uuid' }
adapters.sqlserver { column.sql_type.should == 'uniqueidentifier' }
end
end
end
Expand All @@ -43,7 +44,7 @@
spec_for_adapter do |adapters|
adapters.sqlite3 { connection.change_column table_name, column_name, :uuid }
adapters.mysql2 { connection.change_column table_name, column_name, :uuid }
# adapters.postgresql { connection.change_column table_name, column_name, :uuid }
adapters.postgresql { connection.change_column table_name, column_name, :uuid }
end
end

Expand All @@ -52,6 +53,7 @@
adapters.sqlite3 { column.sql_type.should == 'binary(16)' }
adapters.mysql2 { column.sql_type.should == 'binary(16)' }
adapters.postgresql { pending('postgresql can`t change column type to uuid') }
adapters.sqlserver { pending('sqlserver can`t change column type to uuid') }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/spec_for_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize
@specs = {}
end

[:sqlite3, :mysql2, :postgresql].each do |name|
[:sqlite3, :mysql2, :postgresql, :sqlserver].each do |name|
send :define_method, name do |&block|
@specs[name] = block
end
Expand Down