From 1cd5a382f6347994e953396ea88b707204cb99a1 Mon Sep 17 00:00:00 2001 From: Christopher Peplin Date: Wed, 20 Oct 2010 15:56:34 -0400 Subject: [PATCH] Validate that lines don't start and end at the same spot. Fixed #61. --- app/models/interstate_line.rb | 2 ++ app/models/line.rb | 1 + app/validators/line_endpoint_validator.rb | 9 +++++++++ spec/models/interstate_line_spec.rb | 9 ++++++++- spec/models/line_spec.rb | 11 +++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/validators/line_endpoint_validator.rb diff --git a/app/models/interstate_line.rb b/app/models/interstate_line.rb index 7eeb52d..eb53b38 100644 --- a/app/models/interstate_line.rb +++ b/app/models/interstate_line.rb @@ -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 diff --git a/app/models/line.rb b/app/models/line.rb index 6b20d7c..be7f744 100644 --- a/app/models/line.rb +++ b/app/models/line.rb @@ -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 diff --git a/app/validators/line_endpoint_validator.rb b/app/validators/line_endpoint_validator.rb new file mode 100644 index 0000000..a4096d5 --- /dev/null +++ b/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 diff --git a/spec/models/interstate_line_spec.rb b/spec/models/interstate_line_spec.rb index 965c073..164d0a1 100644 --- a/spec/models/interstate_line_spec.rb +++ b/spec/models/interstate_line_spec.rb @@ -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 @@ -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 diff --git a/spec/models/line_spec.rb b/spec/models/line_spec.rb index 3404307..532f18a 100644 --- a/spec/models/line_spec.rb +++ b/spec/models/line_spec.rb @@ -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