activerecord-draft_records 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -38,16 +38,21 @@ module ActiveRecord
38
38
  end
39
39
  end
40
40
 
41
+ # Override valid? to automatically and temporarily set it as a draft for the validation.
42
+ def valid?
43
+ self.draft, is_draft = false, self.draft
44
+ super
45
+ ensure
46
+ self.draft = is_draft
47
+ end
48
+
41
49
  # Save the record and, if it is a draft, attempt to transform it in a normal (non-draft) record.
42
50
  def save_and_attempt_to_undraft
43
- self.draft, is_draft = false, self.draft
44
-
45
- if self.valid?
46
- save
47
- elsif self.draft = is_draft
51
+ if self.draft?
52
+ self.draft = false if self.valid?
48
53
  save(false)
49
54
  else
50
- false
55
+ save
51
56
  end
52
57
  end
53
58
 
@@ -70,4 +70,13 @@ describe "Integration features" do
70
70
  user.should_not be_draft
71
71
  user.should be_changed
72
72
  end
73
+
74
+ it "should validate object as not draft" do
75
+ User.create :email => 'joe@example.com', :username => 'joe'
76
+ user = User.new :username => 'joe'
77
+ user.save_as_draft.should be_true
78
+ user.should_not be_valid
79
+ user.errors.on(:username).should_not be_blank
80
+ user.errors.on(:username).should include("has already been taken")
81
+ end
73
82
  end
data/spec/spec_helper.rb CHANGED
@@ -24,6 +24,7 @@ end
24
24
  class User < ActiveRecord::Base
25
25
  include ActiveRecord::DraftRecords
26
26
  validates_presence_of :username, :email
27
+ validates_uniqueness_of :username, :email, :scope => :draft
27
28
  end
28
29
 
29
30
  Spec::Runner.configure do |config|
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rodrigo Kochenburger
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-07 00:00:00 -03:00
17
+ date: 2010-07-29 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency