Permalink
Browse files

kill usesguid method

See comments in lib/usesguid/active_record_extensions.rb
for details.
[cr: ajit]
  • Loading branch information...
1 parent 4f3b5bf commit eb2250d34688f7a9879ba227ccfafbafe0c7f9b0 @avaynshtok avaynshtok committed Dec 13, 2011
Showing with 49 additions and 23 deletions.
  1. +5 −1 History.txt
  2. +1 −1 VERSION
  3. +42 −20 lib/usesguid/active_record_extensions.rb
  4. +1 −1 usesguid.gemspec
View
@@ -1,3 +1,7 @@
+= 3.0.0 2011-12-13
+
+* Killed usesguid method for safety.
+
= 2.0.0 2011-09-21
* Removed uuidtools.rb
@@ -6,4 +10,4 @@
= 1.0.0 2009-11-13
-* Ported from a plugin to gem by C. Jason Harrelson (midas)
+* Ported from a plugin to gem by C. Jason Harrelson (midas)
View
@@ -1 +1 @@
-2.0.0
+3.0.0
@@ -26,27 +26,49 @@ module ClassMethods
def guid_generator=(generator); class_eval { @guid_generator = generator } end
def guid_generator; class_eval { @guid_generator || :timestamp } end
- def usesguid(options = {})
-
- class_eval do
- set_primary_key options[:column] if options[:column]
-
- before_create :assign_guid
-
- # Give this record a guid id. Public method so people can call it before save if necessary.
- def assign_guid
- self[self.class.primary_key] ||= case ActiveRecord::Base.guid_generator
- when :mysql then UUIDTools::UUID.mysql_create(self.connection)
- when :timestamp then UUIDTools::UUID.timestamp_create()
- else raise "Unrecognized guid generator '#{ActiveRecord::Base.guid_generator.to_s}'"
- end.to_s22
- end
-
- end
-
- end
+# This method is unsafe to use as is in rails 3 for two reasons.
+# 1. In rails 2 a Model.new call did not initialize attributes with
+# default values. In rails 3 it does. This means the ||= assignment
+# to the primary key no longer works on attributes with default
+# values.
+#
+# 2. The set_primary_key method causes AR to alias all id and id=
+# methods to the getter/setter of the new primary key, in our
+# case the guid. This has the unfortunate effect that when a model
+# with an id column and a guid column is saved to the db, AR tries
+# to write the value of the guid to the id. This very well may be
+# a bug in rails, but we're not going to spend time sorting that out.
+#
+# Of these two issues #2 is the most severe. We could theoretically just
+# kill the id column on our models which use usesguid, but since there's
+# only 1 of them at the moment, we're just going to stop using the usesguid
+# method, and use our own version of assign_guid, which we already do elsewhere.
+#
+# This approach results in the minimal impact to our main code.
+# Once the rails 3 migration is complete we'll reevaluate our use
+# of usesguid.
+#
+# def usesguid(options = {})
+#
+# class_eval do
+# set_primary_key options[:column] if options[:column]
+#
+# before_create :assign_guid
+#
+# # Give this record a guid id. Public method so people can call it before save if necessary.
+# def assign_guid
+# self[self.class.primary_key] ||= case ActiveRecord::Base.guid_generator
+# when :mysql then UUIDTools::UUID.mysql_create(self.connection)
+# when :timestamp then UUIDTools::UUID.timestamp_create()
+# else raise "Unrecognized guid generator '#{ActiveRecord::Base.guid_generator.to_s}'"
+# end.to_s22
+# end
+#
+# end
+#
+# end
end
end
-end
+end
View
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{usesguid}
- s.version = "2.0.0"
+ s.version = "3.0.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = [%q{Brian Morearty}, %q{Demetrio Nunes}, %q{Robert Aman}, %q{C. Jason Harrelson(midas)}]

0 comments on commit eb2250d

Please sign in to comment.