-
-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for validating multiple attributes at once
This commit adds support for validating multiple attributes at once with `validate_presence_of`. This is useful when you want to ensure that multiple attributes are required. ```ruby class Example include ActiveModel::Model attr_accessor :attr1, :attr2 validates_presence_of :attr1, :attr2 end RSpec.describe Example do it do expect(subject).to validate_presence_of(:attr1, :attr2) end end ``` We also add support for using qualifiers with multiple attributes. There's two caveats: if you use a qualifier, it will apply to all attributes and only the first failure will be reported. ```ruby class Example include ActiveModel::Model attr_accessor :attr1, :attr2 validates_presence_of :attr1, allow_nil: true validates_presence_of :attr2, allow_nil: true end RSpec.describe Example do it do expect(subject).to validate_presence_of(:attr1, :attr2) end end ```
- Loading branch information
1 parent
8890f3d
commit 22c7f16
Showing
7 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
# @private | ||
class MatcherCollection | ||
def initialize(matchers) | ||
@matchers = matchers | ||
end | ||
|
||
def description | ||
matchers.map(&:description).join(' and ') | ||
end | ||
|
||
def matches?(subject) | ||
@failed_matchers = failed_matchers_for(subject, :matches?) | ||
@failed_matchers.empty? | ||
end | ||
|
||
def does_not_match?(subject) | ||
@failed_matchers = failed_matchers_for(subject, :does_not_match?) | ||
@failed_matchers.empty? | ||
end | ||
|
||
def failure_message | ||
first_failure_message(:failure_message) | ||
end | ||
|
||
def failure_message_when_negated | ||
first_failure_message(:failure_message_when_negated) | ||
end | ||
|
||
def method_missing(method, *args, &block) | ||
if all_matchers_respond_to?(method) | ||
matchers.each { |matcher| matcher.send(method, *args, &block) } | ||
self | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(method, include_private = false) | ||
all_matchers_respond_to?(method) || super | ||
end | ||
|
||
private | ||
|
||
attr_reader :matchers | ||
|
||
def failed_matchers_for(subject, method) | ||
matchers.reject { |matcher| matcher.send(method, subject) } | ||
end | ||
|
||
def first_failure_message(method) | ||
@failed_matchers.first&.send(method) | ||
end | ||
|
||
def all_matchers_respond_to?(method) | ||
matchers.all? { |matcher| matcher.respond_to?(method) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters