-
-
Notifications
You must be signed in to change notification settings - Fork 911
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
Support qualifier(optional, required) of 'belongs_to' #956
Changes from all commits
045c2c6
0f24b3c
94f5d88
5def8da
78b2d5b
946bb06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,6 +255,46 @@ module ActiveRecord | |
# | ||
# @return [AssociationMatcher] | ||
# | ||
# ##### required | ||
# | ||
# Use `required` to assert that the `:required` option was specified. | ||
# | ||
# class Person < ActiveRecord::Base | ||
# belongs_to :organization, required: true | ||
# end | ||
# | ||
# # RSpec | ||
# describe Person | ||
# it { should belong_to(:organization).required } | ||
# end | ||
# | ||
# # Minitest (Shoulda) | ||
# class PersonTest < ActiveSupport::TestCase | ||
# should belong_to(:organization).required | ||
# end | ||
# | ||
# @return [AssociationMatcher] | ||
# | ||
# ##### optional | ||
# | ||
# Use `optional` to assert that the `:optional` option was specified. | ||
# | ||
# class Person < ActiveRecord::Base | ||
# belongs_to :organization, optional: true | ||
# end | ||
# | ||
# # RSpec | ||
# describe Person | ||
# it { should belong_to(:organization).optional } | ||
# end | ||
# | ||
# # Minitest (Shoulda) | ||
# class PersonTest < ActiveSupport::TestCase | ||
# should belong_to(:organization).optional | ||
# end | ||
# | ||
# @return [AssociationMatcher] | ||
# | ||
def belong_to(name) | ||
AssociationMatcher.new(:belongs_to, name) | ||
end | ||
|
@@ -937,6 +977,17 @@ def with_primary_key(primary_key) | |
self | ||
end | ||
|
||
# TODO: Removed when Rails 4.2 support finished. | ||
def required(required = true) | ||
@options[:required] = required | ||
self | ||
end | ||
|
||
def optional(optional = true) | ||
@options[:optional] = optional | ||
self | ||
end | ||
|
||
def validate(validate = true) | ||
@options[:validate] = validate | ||
self | ||
|
@@ -975,6 +1026,8 @@ def matches?(subject) | |
primary_key_exists? && | ||
class_name_correct? && | ||
join_table_correct? && | ||
required_correct? && # TODO: Removed when Rails 4.2 support finished. | ||
optional_correct? && | ||
autosave_correct? && | ||
conditions_correct? && | ||
validate_correct? && | ||
|
@@ -1130,6 +1183,33 @@ def autosave_correct? | |
end | ||
end | ||
|
||
# TODO: Removed when Rails 4.2 support finished. | ||
def required_correct? | ||
if options.key?(:required) | ||
if option_verifier.correct_for_boolean?(:required, options[:required]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] |
||
true | ||
else | ||
@missing = "#{name} should have required set to #{options[:required]}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [84/80] |
||
false | ||
end | ||
else | ||
true | ||
end | ||
end | ||
|
||
def optional_correct? | ||
if options.key?(:optional) | ||
if option_verifier.correct_for_boolean?(:optional, options[:optional]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] |
||
true | ||
else | ||
@missing = "#{name} should have optional set to #{options[:optional]}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [84/80] |
||
false | ||
end | ||
else | ||
true | ||
end | ||
end | ||
|
||
def conditions_correct? | ||
if options.key?(:conditions) | ||
if option_verifier.correct_for_relation_clause?(:conditions, options[:conditions]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [81/80]