From 044553079e46a9e43abd63db69f44fd89023c335 Mon Sep 17 00:00:00 2001 From: Matthew Boehlig Date: Sun, 4 Nov 2012 18:08:18 -0600 Subject: [PATCH 1/2] Generate uuid after initialize to support validations on id --- spec/lib/activerecord_spec.rb | 18 ++++++++++++++++++ spec/support/models/uuid_article.rb | 3 +++ 2 files changed, 21 insertions(+) diff --git a/spec/lib/activerecord_spec.rb b/spec/lib/activerecord_spec.rb index 316d086..316ec51 100644 --- a/spec/lib/activerecord_spec.rb +++ b/spec/lib/activerecord_spec.rb @@ -96,6 +96,24 @@ let(:model) { described_class } subject { model } + context 'new record' do + let!(:article) { Fabricate.build :uuid_article } + subject { article } + + context 'validation' do + its(:id) { should be_nil } + specify { subject.should be_new_record } + specify { subject.should be_valid } + end + + context 'save without validation' do + before { subject.save(validate: false) } + + its(:id) { should be_a UUIDTools::UUID } + specify { subject.should be_valid } + end + end + context 'model' do its(:primary_key) { should == 'id' } its(:all) { should == [article] } diff --git a/spec/support/models/uuid_article.rb b/spec/support/models/uuid_article.rb index 0292bb9..50e8803 100644 --- a/spec/support/models/uuid_article.rb +++ b/spec/support/models/uuid_article.rb @@ -1,3 +1,6 @@ class UuidArticle < ActiveRecord::Base include ActiveUUID::UUID + + validates :id, uniqueness: true, length: { in: 32..40 }, unless: :new_record? + end From b5a72a8de9df1d0d7e455de98888f2dbd50e7b9e Mon Sep 17 00:00:00 2001 From: Matthew Boehlig Date: Thu, 31 Jan 2013 17:30:06 -0600 Subject: [PATCH 2/2] Test another uuid --- lib/activeuuid/uuid.rb | 1 + spec/lib/activerecord_spec.rb | 6 +++++- spec/support/models/uuid_article.rb | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/activeuuid/uuid.rb b/lib/activeuuid/uuid.rb index 48b40c6..8e59862 100644 --- a/lib/activeuuid/uuid.rb +++ b/lib/activeuuid/uuid.rb @@ -86,6 +86,7 @@ module UUID singleton_class.alias_method_chain :instantiate, :uuid before_create :generate_uuids_if_needed + after_initialize :generate_uuids_if_needed end module ClassMethods diff --git a/spec/lib/activerecord_spec.rb b/spec/lib/activerecord_spec.rb index 316ec51..ed2ba65 100644 --- a/spec/lib/activerecord_spec.rb +++ b/spec/lib/activerecord_spec.rb @@ -99,9 +99,13 @@ context 'new record' do let!(:article) { Fabricate.build :uuid_article } subject { article } + let(:uuid) { UUIDTools::UUID.random_create } + let(:string) { uuid.to_s } + before { subject.another_uuid = string } context 'validation' do its(:id) { should be_nil } + its(:another_uuid) { should be_a UUIDTools::UUID } specify { subject.should be_new_record } specify { subject.should be_valid } end @@ -202,4 +206,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/support/models/uuid_article.rb b/spec/support/models/uuid_article.rb index 50e8803..0894d0c 100644 --- a/spec/support/models/uuid_article.rb +++ b/spec/support/models/uuid_article.rb @@ -1,6 +1,7 @@ class UuidArticle < ActiveRecord::Base include ActiveUUID::UUID - validates :id, uniqueness: true, length: { in: 32..40 }, unless: :new_record? + validates :id, presence: true, uniqueness: true, length: { in: 32..40 }, unless: :new_record? + validates :another_uuid, presence: true, uniqueness: true, length: { in: 32..40 }, unless: :new_record? end