-
-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add required/optional qual's to belongs_to/has_one
Rails 5 made two changes to `belongs_to` associations: * `required` and `optional` were added as options (which add and remove a presence validation on the association, respectively) * `required` was made the default In addition, a `required` option was also added to `has_one`. These new qualifiers allow us to test these options appropriately. Credit: Shia <rise.shia@gmail.com>
- Loading branch information
Showing
9 changed files
with
375 additions
and
32 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
47 changes: 47 additions & 0 deletions
47
lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb
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,47 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveRecord | ||
module AssociationMatchers | ||
# @private | ||
class OptionalMatcher | ||
attr_reader :missing_option | ||
|
||
def initialize(attribute_name, optional) | ||
@attribute_name = attribute_name | ||
@missing_option = '' | ||
@submatcher = submatcher_class_for(optional).new(nil). | ||
for(attribute_name). | ||
with_message(:required) | ||
end | ||
|
||
def description | ||
'required: true' | ||
end | ||
|
||
def matches?(subject) | ||
if submatcher.matches?(subject) | ||
true | ||
else | ||
@missing_option = | ||
'the association should have been defined ' + | ||
'with `optional: true`, but was not' | ||
false | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :subject, :submatcher | ||
|
||
def submatcher_class_for(optional) | ||
if optional | ||
ActiveModel::AllowValueMatcher | ||
else | ||
ActiveModel::DisallowValueMatcher | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb
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,50 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveRecord | ||
module AssociationMatchers | ||
# @private | ||
class RequiredMatcher | ||
attr_reader :missing_option | ||
|
||
def initialize(attribute_name, required) | ||
@missing_option = '' | ||
@submatcher = submatcher_class_for(required).new(nil). | ||
for(attribute_name). | ||
with_message(validation_message_key) | ||
end | ||
|
||
def description | ||
'required: true' | ||
end | ||
|
||
def matches?(subject) | ||
if submatcher.matches?(subject) | ||
true | ||
else | ||
@missing_option = | ||
'the association should have been defined ' + | ||
'with `required: true`, but was not' | ||
false | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :subject, :submatcher | ||
|
||
def submatcher_class_for(required) | ||
if required | ||
ActiveModel::DisallowValueMatcher | ||
else | ||
ActiveModel::AllowValueMatcher | ||
end | ||
end | ||
|
||
def validation_message_key | ||
RailsShim.validation_message_key_for_association_required_option | ||
end | ||
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
Oops, something went wrong.