Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context support #313

Merged
merged 1 commit into from
Jul 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# HEAD

* Fix context support for validation matchers and disallowed values.

* Add a `counter_cache` submatcher for `belongs_to` associations

* Add a rescue_from matcher for Rails controllers which checks that the correct
Expand Down
5 changes: 5 additions & 0 deletions lib/shoulda/matchers/active_model/disallow_value_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def for(attribute)
self
end

def on(context)
@allow_matcher.on(context)
self
end

def with_message(message)
@allow_matcher.with_message(message)
self
Expand Down
2 changes: 2 additions & 0 deletions lib/shoulda/matchers/active_model/validation_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def allow_value_matcher(value, message)
matcher = AllowValueMatcher.
new(value).
for(@attribute).
on(@context).
with_message(message)

if strict?
Expand All @@ -71,6 +72,7 @@ def disallow_value_matcher(value, message)
matcher = DisallowValueMatcher.
new(value).
for(@attribute).
on(@context).
with_message(message)

if strict?
Expand Down
18 changes: 18 additions & 0 deletions spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
end
end

context "an attribute with a context-dependent validation" do
context "without the validation context" do
it "does not match" do
validating_format(:with => /abc/, :on => :customisable).should_not matcher("xyz").for(:attr)
end
end

context "with the validation context" do
it "disallows a bad value" do
validating_format(:with => /abc/, :on => :customisable).should matcher("xyz").for(:attr).on(:customisable)
end

it "does not match a good value" do
validating_format(:with => /abc/, :on => :customisable).should_not matcher("abcde").for(:attr).on(:customisable)
end
end
end

context 'an attribute with a format validation and a custom message' do
it 'does not match if the value and message are both correct' do
validating_format(:with => /abc/, :message => 'good message').
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@
end
end

context "an attribute with a context-dependent validation" do
context "without the validation context" do
it "does not match" do
validating_presence(:on => :customisable).should_not matcher
end
end

context "with the validation context" do
it "matches" do
validating_presence(:on => :customisable).should matcher.on(:customisable)
end
end
end

def matcher
validate_presence_of(:attr)
end
Expand Down