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

Changes string_of_length to handle arrays #1016

Closed
wants to merge 3 commits into from

Conversation

LukasBarry
Copy link

Changes string_of_length to handle arrays

I recently discovered an error on a valid use case of the validates_length_of in the shoulda-matchers gem. I was attempting to test the length of an associated array, but kept receiving the same error:

NoMethodError:
       undefined method `each' for "":String

Here is the issue I submitted to Thoughtbot

Here is my suggested fix. I am new to contributing, so any feedback would be greatly appreciated!

Copy link
Collaborator

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Hey @LukasBarry, thanks so much for the fix. I had a couple of comments below. Also, it doesn't look like there are any tests for this new behavior. Do you mind adding those? Thank you!

def string_of_length(length)
'x' * length
def string_or_array_of_length(length)
if @attribute.is_a? String
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm... I'm not sure that this actually works like you think. I believe that @attribute here refers to the name of the attribute, not an attribute as a whole.

In order to properly check this, you have to look at the column type. Unfortunately, you can't make the assumption that the model being tested is an ActiveRecord model -- it could be an ActiveModel model, in which case there is no column and hence no type.

There are other matchers in which we do this properly; take a look at

for inspiration. (Although it shouldn't be that complicated in this case -- you don't need to map :integer, for instance, to :fixnum or anything like that.)

@matsales28
Copy link
Member

matsales28 commented Aug 18, 2023

The idea behind this PR was resolved on #1560. I appreciate your contribution and time @LukasBarry. I'll be closing this PR. Maintainers, feel free to re-open the PR if you think otherwise.

@matsales28 matsales28 closed this Aug 18, 2023
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