Permalink
Browse files

Added ability to quote uuid columns as uuid during querying

  • Loading branch information...
1 parent bb9fcf4 commit fd63821fcbc943c06870999b56bcc56a87bd145e @pyromaniac pyromaniac committed Sep 26, 2012
Showing with 32 additions and 15 deletions.
  1. +17 −1 lib/activeuuid/railtie.rb
  2. +15 −14 lib/activeuuid/uuid.rb
View
18 lib/activeuuid/railtie.rb
@@ -15,9 +15,25 @@ def uuid(*args)
end
end
- initializer "activeuuid.configure_rails_initialization" do
+ module Quoting
+ extend ActiveSupport::Concern
+
+ included do
+ def quote_with_visiting(value, column = nil)
+ value = ActiveUUID::UUIDSerializer.new.load(value) if column && column.sql_type == 'binary(16)'
+ quote_without_visiting(value, column)
+ end
+
+ alias_method_chain :quote, :visiting
+ end
+ end
+
+ config.to_prepare do
ActiveRecord::ConnectionAdapters::Table.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::Table
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition
+ ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
end
end
end
View
29 lib/activeuuid/uuid.rb
@@ -8,6 +8,10 @@ def quoted_id
"x'#{s}'"
end
+ def id
+ quoted_id
+ end
+
def as_json(options = nil)
hexdigest.upcase
end
@@ -55,10 +59,8 @@ def load(binary)
binary
when String
parse_string(binary)
- when nil
- nil
else
- raise TypeError, "the given type cannot be serialized"
+ nil
end
end
@@ -67,11 +69,9 @@ def dump(uuid)
when UUIDTools::UUID
uuid.raw
when String
- parse_string(uuid).raw
- when nil
- nil
+ parse_string(uuid).try(:raw)
else
- raise TypeError, "the given type cannot be serialized"
+ nil
end
end
@@ -93,6 +93,7 @@ module UUID
extend ActiveSupport::Concern
included do
+ class_attribute :uuid_attributes, :instance_writer => true
uuids :id
before_create :generate_uuids_if_needed
end
@@ -106,18 +107,18 @@ def natural_key(*attributes)
@_activeuuid_natural_key_attributes = attributes
end
- def uuid_attributes
- @_activeuuid_attributes
- end
-
def uuid_generator(generator_name=nil)
@_activeuuid_kind = generator_name if generator_name
@_activeuuid_kind || :random
end
def uuids(*attributes)
- @_activeuuid_attributes = attributes.collect(&:intern).each do |attribute|
- serialize attribute.intern, ActiveUUID::UUIDSerializer.new
+ self.uuid_attributes = attributes.collect(&:intern).each do |attribute|
+ serialize attribute, ActiveUUID::UUIDSerializer.new
+ # serializing attributes on the fly
+ define_method "#{attribute}=" do |value|
+ write_attribute attribute, serialized_attributes[attribute.to_s].load(value)
+ end
end
#class_eval <<-eos
# # def #{@association_name}
@@ -143,7 +144,7 @@ def create_uuid
end
def generate_uuids_if_needed
- self.class.uuid_attributes.each do |attr|
+ (uuid_attributes & [self.class.primary_key.intern]).each do |attr|
self.send("#{attr}=", create_uuid) unless self.send(attr)
end
end

0 comments on commit fd63821

Please sign in to comment.