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

Support qualifier(optional, required) of 'belongs_to' #956

Closed
wants to merge 6 commits into from
Closed
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
80 changes: 80 additions & 0 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

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]

optional_correct? &&
autosave_correct? &&
conditions_correct? &&
validate_correct? &&
Expand Down Expand Up @@ -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])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

Choose a reason for hiding this comment

The 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]}"

Choose a reason for hiding this comment

The 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])

Choose a reason for hiding this comment

The 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]}"

Choose a reason for hiding this comment

The 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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,44 @@
expect(Child.new).to belong_to(:parent)
end

# TODO: Removed when Rails 4.2 support finished.
it 'accepts an association with a valid :required option' do
begin
expect(belonging_to_parent(required: true)).
to belong_to(:parent).required
rescue
skip 'This version does not support required qualifier'
end
end

# TODO: Removed when Rails 4.2 support finished.
it 'accepts an association with a valid :required option' do
begin
expect(belonging_to_parent(required: false)).
not_to belong_to(:parent).required
rescue
skip 'This version does not support required qualifier'
end
end

it 'accepts an association with a valid :optional option' do
begin
expect(belonging_to_parent(optional: true)).
to belong_to(:parent).optional
rescue
skip 'This version does not support optional qualifier'
end
end

it 'accepts an association with a valid :optional option' do
begin
expect(belonging_to_parent(optional: false)).
not_to belong_to(:parent).optional
rescue
skip 'This version does not support optional qualifier'
end
end

it 'accepts an association with a valid :dependent option' do
expect(belonging_to_parent(dependent: :destroy)).
to belong_to(:parent).dependent(:destroy)
Expand Down