Skip to content

Commit

Permalink
Added ability to quote uuid columns as uuid during querying
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Oct 3, 2012
1 parent bb9fcf4 commit fd63821
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
18 changes: 17 additions & 1 deletion lib/activeuuid/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 15 additions & 14 deletions lib/activeuuid/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def quoted_id
"x'#{s}'"
end

def id
quoted_id
end

def as_json(options = nil)
hexdigest.upcase
end
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit fd63821

Please sign in to comment.