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

Conversation

brendanthomas1
Copy link
Contributor

Hello! Our team was adding index_errors to a couple has_many associations and we noticed that there's no matcher for that yet. It looked pretty similar to autosave since it's just a boolean, so I pretty much copied that 😄

end

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

Choose a reason for hiding this comment

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

Avoid using {...} for multi-line blocks.

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)'

Choose a reason for hiding this comment

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

Line is too long. [130/80]

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

Choose a reason for hiding this comment

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

Line is too long. [109/80]

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

Choose a reason for hiding this comment

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

Line is too long. [92/80]

@@ -1257,6 +1281,19 @@ def autosave_correct?
end
end

def index_errors_correct?
if options.key?(:index_errors)
if option_verifier.correct_for_boolean?(:index_errors, options[:index_errors])

Choose a reason for hiding this comment

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

Line is too long. [90/80]

@@ -524,6 +524,24 @@ 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.

Choose a reason for hiding this comment

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

Line is too long. [83/80]

def index_errors_correct?
if options.key?(:index_errors)
if option_verifier.correct_for_boolean?(:index_errors,
options[:index_errors])

Choose a reason for hiding this comment

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

Use one level of indentation for parameters following the first line of a multi-line method call.

@@ -1257,6 +1282,21 @@ 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]

def index_errors_correct?
if options.key?(:index_errors)
if option_verifier.correct_for_boolean?(
:index_errors, options[:index_errors]

Choose a reason for hiding this comment

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

Use one level of indentation for parameters following the first line of a multi-line method call.
Indent the first parameter one step more than the start of the previous line.

@@ -1257,6 +1282,22 @@ 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. [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 🙂

@brendanthomas1 brendanthomas1 force-pushed the add-index-errors-association-matcher branch from f8abd89 to 4d1fbae Compare March 9, 2018 18:15
@mcmire
Copy link
Collaborator

mcmire commented Apr 4, 2018

I've never seen the index_errors option before. What does it do?

@brendanthomas1
Copy link
Contributor Author

You can use it with a has_many to determine which nested object as an error

# Old style

guitar.errors["tuning_pegs.pitch"] = ["is not a number"]

# New style (if defined globally, or set in has_many_relationship)

guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]

The Rails PR is here, and it's in the changelog, but it hasn't made it into the Rails docs yet for some reason 🤷‍♂️

@mcmire
Copy link
Collaborator

mcmire commented Apr 4, 2018

Oh very interesting, okay. Thanks for the clarification!

@mcmire
Copy link
Collaborator

mcmire commented Apr 4, 2018

Thanks! I've tweaked this a bit for style and merged this as 795ca68.

@mcmire mcmire closed this Apr 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants