Skip to content

Commit

Permalink
For #3045
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jan 4, 2010
1 parent 3981310 commit 4c7d3a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
18 changes: 18 additions & 0 deletions app/models/task.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,20 @@
class Task < ActiveRecord::Base class Task < ActiveRecord::Base
attr_accessor :fail
belongs_to :project

before_save :cause_before_save_problems
after_save :cause_after_save_problems
validate :cause_validation_problems

def cause_validation_problems
errors.add(:fail, 'in validation') if fail == :in_validation
end

def cause_before_save_problems
return false if fail == :before_save
end

def cause_after_save_problems
raise ActiveRecord::Rollback if fail == :after_save
end
end end
25 changes: 19 additions & 6 deletions test/unit/project_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,26 @@
require 'test_helper' require 'test_helper'


class ProjectTest < ActiveSupport::TestCase class ProjectTest < ActiveSupport::TestCase
def test_case_name self.use_transactional_fixtures = false
project = Project.create! :owner_attributes => { :name => 'Bello' }


# without attr_accessible def test_project_rolls_back_when_task_validation_fails
p project.owner # => #<Author id: 980190963, name: "Bello", project_id: 980190963, owner: true, admin: true> @project = Project.create :name => 'Foo', :tasks_attributes => { '0' => { :name => 'Bar', :fail => :in_validation } }


# with: attr_accessible :name assert @project.tasks.first.new_record?
p project.owner # => #<Author id: 980190963, name: "Bello", project_id: 980190963, owner: nil, admin: nil> assert @project.new_record?
end

def test_project_rolls_back_when_task_before_save_fails
@project = Project.create :name => 'Foo', :tasks_attributes => { '0' => { :name => 'Bar', :fail => :before_save } }

assert @project.tasks.first.new_record?
assert @project.new_record?
end

def test_project_rolls_back_when_task_after_save_fails
@project = Project.create :name => 'Foo', :tasks_attributes => { '0' => { :name => 'Bar', :fail => :after_save } }

assert @project.tasks.first.new_record?
assert @project.new_record?
end end
end end

0 comments on commit 4c7d3a7

Please sign in to comment.