-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |