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

Add index_errors association matcher #1089

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
39 changes: 39 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,20 @@ def autosave_correct?
end
end

def index_errors_correct?

Choose a reason for hiding this comment

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

Method has too many lines. [12/10]

Choose a reason for hiding this comment

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

Method has too many lines. [13/10]

Copy link
Contributor Author

@brendanthomas1 brendanthomas1 Mar 9, 2018

Choose a reason for hiding this comment

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

This is down to 10 lines now 🙂

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,32 @@ 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 do
expect(Parent.new).to have_many(:children).index_errors(true)
end.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