Skip to content

Commit

Permalink
WIP: Fix positve error description spec issues and add method for mat…
Browse files Browse the repository at this point in the history
…ched_error.
  • Loading branch information
drapergeek authored and Gabe Berke-Williams committed Sep 12, 2012
1 parent 9d24782 commit 94a0cbd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/shoulda/matchers/active_model/positive_error_description.rb
Expand Up @@ -17,6 +17,14 @@ def matches?
end
end

def matched_error
if @expected_message
error_matching_expected || ""
else
first_error_message
end
end

private

def error_message_matches_string?
Expand All @@ -33,6 +41,16 @@ def error_message_matches_regexp?
end
end

def error_matching_expected
errors_for_attribute.select do |error|
@expected_message.match(error)
end.first
end

def first_error_message
errors_for_attribute.first.to_s
end

def errors_for_attribute
valid?
Array.wrap(@instance.errors[@attribute])
Expand Down
6 changes: 3 additions & 3 deletions spec/shoulda/active_model/positive_error_description_spec.rb
Expand Up @@ -20,7 +20,6 @@

context 'with an error that matches the expected message regexp' do
it 'returns the matched error' do
define_model(:Example, :attr => :string) do
define_model(:Example, :attr => :string) do
validates :attr, :presence => true
end
Expand All @@ -31,6 +30,7 @@

context 'with an error that matches the expected message regexp' do
it 'returns the matched error' do
define_model(:Example, :attr => :string) do
validates :attr, :presence => true
end
positive_error_description = described_class.new(Example.new, :attr, '', /blank/)
Expand All @@ -52,7 +52,7 @@
it 'returns the first matching error when there are errors' do
define_model(:Example, :attr => :string) do
validates :attr, :presence => true,
:format => { :with => 'abc' }
:format => { :with => /abc/ }
end
positive_error_description = described_class.new(Example.new, :attr, '', nil)
positive_error_description.matched_error.should == "can't be blank"
Expand All @@ -74,7 +74,7 @@
validates :attr, :presence => true
end
positive_error_description = described_class.new(Example.new, :attr, '', nil)
positive_error_description.matches?.should be_false
positive_error_description.matches?.should be_true
end

it 'returns false when there is no matched error' do
Expand Down

0 comments on commit 94a0cbd

Please sign in to comment.