@@ -33,14 +33,12 class IssueRelation < ActiveRecord::Base | |||
|
33 | 33 | }.freeze |
|
34 | 34 | |
|
35 | 35 | validates_presence_of :issue_from, :issue_to, :relation_type |
|
36 |
validates_inclusion_of :relation_type, :in => |
|
|
36 | validates_inclusion_of :relation_type, :in => TYPES.keys | |
|
37 | 37 | validates_numericality_of :delay, :allow_nil => true |
|
38 | 38 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id |
|
39 | 39 | |
|
40 | 40 | attr_protected :issue_from_id, :issue_to_id |
|
41 | 41 | |
|
42 | before_validation :reverse_if_needed | |
|
43 | ||
|
44 | 42 | def validate |
|
45 | 43 | if issue_from && issue_to |
|
46 | 44 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id |
@@ -58,6 +56,8 class IssueRelation < ActiveRecord::Base | |||
|
58 | 56 | end |
|
59 | 57 | |
|
60 | 58 | def before_save |
|
59 | reverse_if_needed | |
|
60 | ||
|
61 | 61 | if TYPE_PRECEDES == relation_type |
|
62 | 62 | self.delay ||= 0 |
|
63 | 63 | else |
@@ -43,4 +43,15 class IssueRelationTest < ActiveSupport::TestCase | |||
|
43 | 43 | assert_equal to, relation.issue_from |
|
44 | 44 | assert_equal from, relation.issue_to |
|
45 | 45 | end |
|
46 | ||
|
47 | def test_follows_relation_should_not_be_reversed_if_validation_fails | |
|
48 | from = Issue.find(1) | |
|
49 | to = Issue.find(2) | |
|
50 | ||
|
51 | relation = IssueRelation.new :issue_from => from, :issue_to => to, :relation_type => IssueRelation::TYPE_FOLLOWS, :delay => 'xx' | |
|
52 | assert !relation.save | |
|
53 | assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type | |
|
54 | assert_equal from, relation.issue_from | |
|
55 | assert_equal to, relation.issue_to | |
|
56 | end | |
|
46 | 57 | end |
General Comments 0
You need to be logged in to leave comments.
Login now