Skip to content

Commit

Permalink
Make sure uuid is assigned before validation
Browse files Browse the repository at this point in the history
  • Loading branch information
exviva committed May 14, 2012
1 parent 8f2848e commit 6fb3387
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
### Not released yet:

* (exviva) Make sure uuid is assigned before validation

### 0.0.8 (2012-04-27):

* (troessner) Refactor the whole gem and use `class_attribute`.
1 change: 1 addition & 0 deletions lib/has_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def has_uuid(options = {})
include InstanceMethods

if options[:auto]
before_validation(:on => :create) { assign_uuid }
before_save(:on => :create) { assign_uuid }
end

Expand Down
12 changes: 12 additions & 0 deletions test/has_uuid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Thingy < ActiveRecord::Base
has_uuid :auto => false
end

class ParanoidWidget < Widget
set_table_name 'widgets'
validates_presence_of :uuid
end

class HasUuidTest < Test::Unit::TestCase
def test_should_generate_a_valid_uuid
uuid = Widget.generate_uuid
Expand Down Expand Up @@ -93,4 +98,11 @@ def test_should_assign_an_uuid_if_we_do_not_validate_the_overall_record
@widget.reload
assert @widget.uuid_valid?
end

def test_assign_uuid_before_validation
paranoid_widget = ParanoidWidget.new
assert !paranoid_widget.uuid_valid?
assert paranoid_widget.valid?
assert paranoid_widget.uuid_valid?
end
end

0 comments on commit 6fb3387

Please sign in to comment.