Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Validate that lines don't start and end at the same spot.
Browse files Browse the repository at this point in the history
Fixed #61.
  • Loading branch information
Christopher Peplin committed Oct 20, 2010
1 parent 6528c2e commit 1cd5a38
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/interstate_line.rb
Expand Up @@ -12,6 +12,8 @@ class InterstateLine < ActiveRecord::Base
validates :incoming_state, :presence => true
validates :outgoing_state, :presence => true
validates :operating_level, :presence => true
validates_with LineEndpointValidator, :start => :incoming_state,
:end => :outgoing_state

attr_readonly :incoming_state, :outgoing_state, :line_type

Expand Down
1 change: 1 addition & 0 deletions app/models/line.rb
Expand Up @@ -10,4 +10,5 @@ class Line < TechnicalComponentInstance
validates :line_type, :presence => true
validates :city, :presence => true
validates :other_city, :presence => true
validates_with LineEndpointValidator, :start => :city, :end => :other_city
end
9 changes: 9 additions & 0 deletions app/validators/line_endpoint_validator.rb
@@ -0,0 +1,9 @@
class LineEndpointValidator < ActiveModel::Validator
def validate record
starting = record.send(options[:start])
ending = record.send(options[:end])
if starting and ending and starting == ending
record.errors[:base] << "start and endpoint cannot be the same"
end
end
end
9 changes: 8 additions & 1 deletion spec/models/interstate_line_spec.rb
Expand Up @@ -12,7 +12,7 @@
it { should validate_presence_of :operating_level }

context "an instance of InterstateLine" do
before :all do
before do
@line = Factory :interstate_line
end

Expand All @@ -37,5 +37,12 @@

lambda { @line.accepted = false }.should raise_error
end

it "should validate that the incoming and outgoing states are different" do
line = Factory.build :interstate_line
line.incoming_state = @line.incoming_state
line.outgoing_state = line.incoming_state
line.save.should raise_error
end
end
end
11 changes: 11 additions & 0 deletions spec/models/line_spec.rb
Expand Up @@ -8,4 +8,15 @@
it { should validate_presence_of :line_type }

it { should have_many :repairs }

context "an instance of Line" do
before :all do
@line = Factory :line
end

it "should validate that the incoming and outgoing states are different" do
@line.other_city = @line.city
@line.save.should raise_error
end
end
end

0 comments on commit 1cd5a38

Please sign in to comment.