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

Validate enum column is an integer in define_enum_for #829

Merged
merged 1 commit into from
Jan 11, 2016

Conversation

geoffharcourt
Copy link
Contributor

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".

:record_with_hash_values,
{
_enum_attribute => { type: :integer },
}

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.

@geoffharcourt geoffharcourt force-pushed the gh-validate-enum-has-integer-column branch 2 times, most recently from 1343ad5 to ab347d4 Compare October 30, 2015 15:44
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"

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]

@geoffharcourt geoffharcourt force-pushed the gh-validate-enum-has-integer-column branch from ab347d4 to 0d3ad55 Compare October 30, 2015 15:46
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

Choose a reason for hiding this comment

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

Unnecessary spacing detected.

Copy link
Collaborator

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 {

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.

@geoffharcourt geoffharcourt force-pushed the gh-validate-enum-has-integer-column branch from ac6aedc to 27877ba Compare October 30, 2015 16:47
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

lambda here as well.

@geoffharcourt geoffharcourt force-pushed the gh-validate-enum-has-integer-column branch 2 times, most recently from ee43f19 to cac5bf9 Compare October 30, 2015 18:40
:record_with_array_values,
_enum_attribute => { type: :string },
) do |model|
enum enum_attribute => %w(published unpublished draft)
Copy link
Collaborator

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.

@geoffharcourt
Copy link
Contributor Author

@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
Copy link
Collaborator

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.

@mcmire
Copy link
Collaborator

mcmire commented Jan 10, 2016

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.

@mcmire mcmire force-pushed the gh-validate-enum-has-integer-column branch 2 times, most recently from 0339edc to 69786b9 Compare January 11, 2016 04:26

assertion = lambda do
expect(record_with_array_values).
to define_enum_for(non_enum_attribute).with(["open", "close"])

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.
@mcmire mcmire force-pushed the gh-validate-enum-has-integer-column branch from 69786b9 to 68dd70a Compare January 11, 2016 04:40
@mcmire mcmire merged commit 68dd70a into master Jan 11, 2016
@mcmire mcmire deleted the gh-validate-enum-has-integer-column branch January 11, 2016 04:40
@mcmire
Copy link
Collaborator

mcmire commented Jan 11, 2016

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 mcmire added this to the 3.0.2 milestone Jan 11, 2016
@geoffharcourt
Copy link
Contributor Author

@mcmire no need to apologize, excited to see this get merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants