Permalink
Browse files

Merge pull request #10 from jdck/master

Support for a primary column name other than 'id'
  • Loading branch information...
2 parents 0c83c85 + de0b9db commit 45ac3eeabf74055956c32e4832aa91560d8a407d @jashmenn committed Aug 2, 2012
Showing with 22 additions and 25 deletions.
  1. +22 −25 lib/activeuuid/uuid.rb
View
@@ -82,28 +82,8 @@ module UUID
extend ActiveSupport::Concern
included do
- before_create :generate_uuid_if_needed
-
- set_primary_key "id"
- serialize :id, ActiveUUID::UUIDSerializer.new
-
- def generate_uuid_if_needed
- generate_uuid unless self.id
- end
-
- def to_param
- id.to_param
- end
-
- def generate_uuid
- if nka = self.class.natural_key_attributes
- # TODO if all the attributes return nil you might want to warn about this
- chained = nka.collect{|a| self.send(a).to_s}.join("-")
- self.id = UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, chained)
- else
- self.id = UUIDTools::UUID.timestamp_create
- end
- end
+ uuids :id
+ before_create :generate_uuids_if_needed
end
module ClassMethods
@@ -115,19 +95,36 @@ def natural_key(*attributes)
@_activeuuid_natural_key_attributes = attributes
end
+ def uuid_attributes
+ @_activeuuid_attributes
+ end
+
def uuids(*attributes)
- attributes.each do |attribute|
+ @_activeuuid_attributes = attributes.collect(&:intern).each do |attribute|
serialize attribute.intern, ActiveUUID::UUIDSerializer.new
+ end
#class_eval <<-eos
# # def #{@association_name}
# # @_#{@association_name} ||= self.class.associations[:#{@association_name}].new_proxy(self)
# # end
#eos
- end
end
end
- module InstanceMethods
+ def create_uuid
+ if nka = self.class.natural_key_attributes
+ # TODO if all the attributes return nil you might want to warn about this
+ chained = nka.collect{|a| self.send(a).to_s}.join("-")
+ UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, chained)
+ else
+ UUIDTools::UUID.random_create
+ end
+ end
+
+ def generate_uuids_if_needed
+ self.class.uuid_attributes.each do |attr|
+ self.send("#{attr}=", create_uuid) unless self.send(attr)
+ end
end
end

0 comments on commit 45ac3ee

Please sign in to comment.