Permalink
Browse files

Fixed uuid field type for migrations, added ability to specify uuid a…

…s primary key
  • Loading branch information...
1 parent c992cbe commit bb9fcf46c88313415df1d34d2053375d530c68fb @pyromaniac pyromaniac committed Sep 26, 2012
Showing with 15 additions and 11 deletions.
  1. +1 −1 README.mkd
  2. +11 −8 lib/activeuuid/railtie.rb
  3. +3 −2 lib/activeuuid/uuid.rb
View
@@ -12,7 +12,7 @@ Add `binary(16)` UUIDs to ActiveRecord.
class CreateEmails < ActiveRecord::Migration
def self.up
create_table :emails, :id => false do |t|
- t.uuid :id, :unique => true
+ t.uuid :id, :primary_key => true
t.uuid :sender_id # belongs_to :sender
t.string :subject
@@ -4,17 +4,20 @@
module ActiveUUID
class Railtie < Rails::Railtie
railtie_name :activeuuid
- initializer "activeuuid.configure_rails_initialization" do
- module ActiveRecord::ConnectionAdapters
- class Table
- def uuid (*args)
- options = args.extract_options!
- column_names = args
- column_names.each { |name| column(name, 'binary(16)', options) }
- end
+
+ module Migrations
+ def uuid(*args)
+ options = args.extract_options!
+ column_names = args
+ column_names.each do |name|
+ column(name, "binary(16)#{' primary key' if options.delete(:primary_key)}", options)
end
end
+ end
+ initializer "activeuuid.configure_rails_initialization" do
+ ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition
end
end
end
@@ -78,6 +78,7 @@ def dump(uuid)
private
def parse_string str
+ return nil if str.blank?
if str.length == 36
UUIDTools::UUID.parse str
elsif str.length == 32
@@ -112,7 +113,7 @@ def uuid_attributes
def uuid_generator(generator_name=nil)
@_activeuuid_kind = generator_name if generator_name
@_activeuuid_kind || :random
- end
+ end
def uuids(*attributes)
@_activeuuid_attributes = attributes.collect(&:intern).each do |attribute|
@@ -146,6 +147,6 @@ def generate_uuids_if_needed
self.send("#{attr}=", create_uuid) unless self.send(attr)
end
end
-
+
end
end

0 comments on commit bb9fcf4

Please sign in to comment.