-
-
Notifications
You must be signed in to change notification settings - Fork 911
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
Validate enum column is an integer in define_enum_for
#829
Conversation
:record_with_hash_values, | ||
{ | ||
_enum_attribute => { type: :integer }, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a comma after the last parameter of a multiline method call.
1343ad5
to
ab347d4
Compare
enum(_enum_attribute => %w(published unpublished draft)) | ||
end | ||
|
||
message = "Expected #{model} to define :statuses as an enum and store the value in a column with an integer type" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [121/80]
ab347d4
to
0d3ad55
Compare
message = "Expected #{record.class} to define :#{plural_enum_attribute} as an enum" | ||
assertion = -> { | ||
message = "Expected #{record.class} to define :#{plural_enum_attribute} as an enum and store the value in a column with an integer type" | ||
assertion = -> do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary spacing detected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the style I've adopted (mostly across the board, except in a couple spots) has been to use lambda do
instead of -> do
, for a multi-line lambda.
expect do | ||
expect(record_with_array_values).to_not define_enum_for(enum_attribute) | ||
end.to fail_with_message(message) | ||
expect { |
There was a problem hiding this comment.
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.
ac6aedc
to
27877ba
Compare
assertion = -> { | ||
message = "Expected #{record.class} to define :#{plural_enum_attribute} as an enum and store the value in a column with an integer type" | ||
|
||
assertion = lamdba do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lambda
here as well.
ee43f19
to
cac5bf9
Compare
:record_with_array_values, | ||
_enum_attribute => { type: :string }, | ||
) do |model| | ||
enum enum_attribute => %w(published unpublished draft) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like enum
should be model.enum
.
cac5bf9
to
9055174
Compare
@mcmire, other than Hound warnings, is this GTG? I'd like to ship it today if possible. |
@@ -111,6 +113,18 @@ def enum_values_match? | |||
expected_enum_values.empty? || actual_enum_values == expected_enum_values | |||
end | |||
|
|||
def matched_column | |||
@_matched_column ||= begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ActiveRecord provides a columns_hash
method you can use if you want the Column object for a particular attribute. So you can replace this with @model.class.columns_hash[attribute_name.to_s]
instead, here.
Hey @geoffharcourt, sorry for the lack of attention on your PR. I've been preoccupied with fixing some regressions with 3.0 so I'm afraid I forgot about this 😞 I had a few more comments above. If you would like to add an entry to the NEWS section and rebase this against master, you are free to merge this in at will. |
0339edc
to
69786b9
Compare
|
||
assertion = lambda do | ||
expect(record_with_array_values). | ||
to define_enum_for(non_enum_attribute).with(["open", "close"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer single-quoted strings when you don't need string interpolation or special symbols.
If your ActiveRecord model stores its `enum` data in a non-integer column, ActiveRecord will save the data without error. However, when you access the attribute on the record after saving, AR will look for the string to what it expects to be a list of numbers and return `nil` rather than the mapped value. This change adds a third criterion to the `define_enum_for` matcher, verifying that the underlying database column has a `sql_type` of `"integer"`. Fix #827.
69786b9
to
68dd70a
Compare
Hey, sorry for stepping in, but I made these changes and merged this in because I wanted to release 3.0.2. As a plus, this change will be available probably by the time you read this :) |
@mcmire no need to apologize, excited to see this get merged! |
If your ActiveRecord model stores its
enum
data in a non-integercolumn, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return
nil
rather than the mapped value.
This change adds a third criterion to the
define_enum_for
matcher,verifying that the underlying database column has a
sql_type
of"integer"
.