activerecord-draft_records 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -38,6 +38,19 @@ module ActiveRecord
38
38
  end
39
39
  end
40
40
 
41
+ # Save the record and, if it is a draft, attempt to transform it in a normal (non-draft) record.
42
+ 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
48
+ save(false)
49
+ else
50
+ false
51
+ end
52
+ end
53
+
41
54
  # Save the record as a draft, setting the record attribute 'draft' to true
42
55
  # and saving the record ignoring the validations.
43
56
  def save_as_draft
@@ -44,5 +44,30 @@ describe "Integration features" do
44
44
  it "should provide a version of ActiveRecord::Base#find that will look for drafts" do
45
45
  user = User.create_as_draft :email => 'joe@example.com'
46
46
  User.find_drafts(:all).should include(user)
47
- end
47
+ end
48
+
49
+ it "should save a record and attempt to transform into a non-draft" do
50
+ user = User.create_as_draft :email => 'joe@example.com'
51
+ user.username = 'joe'
52
+ user.save_and_attempt_to_undraft.should be(true)
53
+ user.should_not be_draft
54
+ user.should_not be_changed
55
+ end
56
+
57
+ it "should save a record and attempt to transform into a non-draft but fail if validation fails" do
58
+ user = User.create_as_draft :email => 'joe@example.com'
59
+ user.age = 18
60
+ user.save_and_attempt_to_undraft.should be(true)
61
+ user.should be_draft
62
+ user.should_not be_changed
63
+ user.age.should == 18
64
+ end
65
+
66
+ it "should not transform a normal record in draft when calling save_and_attempt_to_undraft" do
67
+ user = User.create :email => 'joe@example.com', :username => 'joe'
68
+ user.username = nil
69
+ user.save_and_attempt_to_undraft.should be(false)
70
+ user.should_not be_draft
71
+ user.should be_changed
72
+ end
48
73
  end
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,7 @@ DatabaseCleaner.strategy = :transaction
16
16
  ActiveRecord::Schema.define do
17
17
  create_table :users do |t|
18
18
  t.string :username, :email
19
+ t.integer :age
19
20
  t.boolean :draft
20
21
  end
21
22
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
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-05-17 00:00:00 -03:00
17
+ date: 2010-06-07 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency