Skip to content

Commit

Permalink
add index_errors association matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanthomas1 authored and mcmire committed Apr 4, 2018
1 parent e35f083 commit 795ca68
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,25 @@ def belong_to(name)
# should have_many(:games).autosave(true)
# end
#
# ##### index_errors
#
# Use `index_errors` to assert that the `:index_errors` option was
# specified.
#
# class Player < ActiveRecord::Base
# has_many :games, index_errors: true
# end
#
# # RSpec
# RSpec.describe Player, type: :model do
# it { should have_many(:games).index_errors(true) }
# end
#
# # Minitest (Shoulda)
# class PlayerTest < ActiveSupport::TestCase
# should have_many(:games).index_errors(true)
# end
#
# ##### inverse_of
#
# Use `inverse_of` to assert that the `:inverse_of` option was specified.
Expand Down Expand Up @@ -1022,6 +1041,11 @@ def autosave(autosave)
self
end

def index_errors(index_errors)
@options[:index_errors] = index_errors
self
end

def class_name(class_name)
@options[:class_name] = class_name
self
Expand Down Expand Up @@ -1096,6 +1120,7 @@ def matches?(subject)
class_name_correct? &&
join_table_correct? &&
autosave_correct? &&
index_errors_correct? &&
conditions_correct? &&
validate_correct? &&
touch_correct? &&
Expand Down Expand Up @@ -1257,6 +1282,22 @@ def autosave_correct?
end
end

def index_errors_correct?
return true unless options.key?(:index_errors)

if option_verifier.correct_for_boolean?(
:index_errors,
options[:index_errors]
)
true
else
@missing =
"#{name} should have index_errors set to " +
"#{options[:index_errors]}"
false
end
end

def conditions_correct?
if options.key?(:conditions)
if option_verifier.correct_for_relation_clause?(:conditions, options[:conditions])
Expand Down
4 changes: 4 additions & 0 deletions spec/support/unit/helpers/rails_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ def rails_gte_4_2?
def rails_lt_5?
rails_version < 5
end

def rails_5_x?
rails_version =~ '~> 5.0'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,33 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
}.to fail_with_message(message)
end

if rails_5_x?
context 'index_errors' do
it 'accepts an association with a matching :index_errors option' do
define_model :child, parent_id: :integer
define_model :parent do
has_many :children, index_errors: true
end
expect(Parent.new).to have_many(:children).index_errors(true)
end

it 'rejects an association with a non-matching :index_errors option and returns the correct message' do
define_model :child, parent_id: :integer
define_model :parent do
has_many :children, autosave: false
end

message =
'Expected Parent to have a has_many association called children ' +
'(children should have index_errors set to true)'

expect {
expect(Parent.new).to have_many(:children).index_errors(true)
}.to fail_with_message(message)
end
end
end

context 'validate' do
it 'accepts when the :validate option matches' do
expect(having_many_children(validate: false)).to have_many(:children).validate(false)
Expand Down

0 comments on commit 795ca68

Please sign in to comment.