Permalink
Browse files

Befote type_cast columns patch, for the great justice!

  • Loading branch information...
pyromaniac committed Nov 6, 2012
1 parent 4206fa4 commit b4bdc4c619a079aab597650fbddf721cf5f13fe1
Showing with 52 additions and 13 deletions.
  1. +13 −1 lib/activeuuid/patches.rb
  2. +14 −2 lib/activeuuid/uuid.rb
  3. +25 −10 spec/lib/activerecord_spec.rb
View
@@ -15,7 +15,7 @@ module Column
included do
def uuid?
- %w(binary(16) uuid).include? sql_type
+ sql_type == 'binary(16)'
end
def type_cast_with_uuid(value)
@@ -39,6 +39,16 @@ def type_cast_code_with_uuid(var_name)
end
end
+ module PostgreSQLColumn
+ extend ActiveSupport::Concern
+
+ included do
+ def uuid?
+ sql_type == 'uuid'
+ end
+ end
+ end
+
module Quoting
extend ActiveSupport::Concern
@@ -88,6 +98,8 @@ def self.apply!
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Migrations if defined? ActiveRecord::ConnectionAdapters::TableDefinition
ActiveRecord::ConnectionAdapters::Column.send :include, Column
+ ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, PostgreSQLColumn if defined? ActiveRecord::ConnectionAdapters::PostgreSQLColumn
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :include, Quoting if defined? ActiveRecord::ConnectionAdapters::SQLite3Adapter
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgreSQLQuoting if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
View
@@ -84,6 +84,7 @@ module UUID
class_attribute :_uuid_generator, instance_writer: false
self._uuid_generator = :random
+ singleton_class.alias_method_chain :instantiate, :uuid
before_create :generate_uuids_if_needed
end
@@ -102,6 +103,17 @@ def uuids(*attributes)
There is no more need to use uuid method.
EOS
end
+
+ def instantiate_with_uuid(record)
+ uuid_columns.each do |uuid_column|
+ record[uuid_column] = UUIDTools::UUID.serialize(record[uuid_column]).to_s
+ end
+ instantiate_without_uuid(record)
+ end
+
+ def uuid_columns
+ @uuid_columns ||= columns.select(&:uuid?).map(&:name)
+ end
end
def create_uuid
@@ -121,8 +133,8 @@ def create_uuid
def generate_uuids_if_needed
primary_key = self.class.primary_key
- if self.class.columns_hash[primary_key].uuid?
- self.send("#{primary_key}=", create_uuid) unless self.send("#{primary_key}?")
+ if self.class.uuid_columns.include?(primary_key)
+ send("#{primary_key}=", create_uuid) unless send("#{primary_key}?")
end
end
@@ -62,27 +62,42 @@
end
context 'typecasting' do
+ let(:input) { "e4618518-cb9f-11e1-aa7c-14dae903e06a" }
+ let(:uuid) { UUIDTools::UUID.parse input }
+ let(:string) { uuid.to_s }
context 'primary' do
- before { article.id = '234' }
+ before { article.id = input }
specify do
- article.id.should == UUIDTools::UUID.parse_raw('234')
- article.id_before_type_cast.should == '234'
+ article.id.should == uuid
+ article.id_before_type_cast.should == input
end
specify do
- article.id_before_type_cast.should == '234'
- article.id.should == UUIDTools::UUID.parse_raw('234')
+ article.id_before_type_cast.should == input
+ article.id.should == uuid
end
+ # specify do
+ # article.save
+ # article.reload
+ # article.id_before_type_cast.should == string
+ # article.id.should == uuid
+ # end
end
context 'non-primary' do
- before { article.another_uuid = '234' }
+ before { article.another_uuid = input }
specify do
- article.another_uuid.should == UUIDTools::UUID.parse_raw('234')
- article.another_uuid_before_type_cast.should == '234'
+ article.another_uuid.should == uuid
+ article.another_uuid_before_type_cast.should == input
end
specify do
- article.another_uuid_before_type_cast.should == '234'
- article.another_uuid.should == UUIDTools::UUID.parse_raw('234')
+ article.another_uuid_before_type_cast.should == input
+ article.another_uuid.should == uuid
+ end
+ specify do
+ article.save
+ article.reload
+ article.another_uuid_before_type_cast.should == string
+ article.another_uuid.should == uuid
end
end
end

0 comments on commit b4bdc4c

Please sign in to comment.