-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Detect enum fields as Selects rather than as Strings #1655
Conversation
This fails due to the vulnerabilities found in 6.0.2.2 version of some action gems, not sure if it's ok to bump them in this PR. |
Thanks for putting this up, just ran into this problem and it was really nice to see that someone had taken the time to make it better. |
Don't worry about the vulns. We just upgraded some gems, so a rebase might be enough. |
Regarding the PR, I think it could do the trick, although here's another potential idea. Since #1589 (not yet released in the gem) it's possible to pass a There's one problem I see: at the moment,
Then we can have the field be something like this: state: Field::Select.with_options(
collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys) },
) OK, I'm not 100% sure of what I'm saying... The good thing about your approach is that it impacts only the generator, so no interfaces have to change. I have to think about this. Thoughts anyone? /cc @nickcharlton |
@pablobm alright, I think I figured it out. The only problem here is that it's pretty tricky to fetch Proc's definition as a String. The easiest way is to read the definition line from a file and parse it, but this forces us to define Procs in one line (or write a really good parser for multi-liners). The other solution would be to add a external gem to handle this, but it's an additional dependency. Please let me know what you think about this. (I have to violate the line length rule in |
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.
A few style comments; I don't find myself with many implementation thoughts, so I'll defer to @pablobm here as he's already thinking about it.
remove_constants :Shipment, :ShipmentDashboard | ||
end | ||
|
||
it "properly handles collection procs option in the 'Select' field" 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.
We can drop "properly" on this descriptor.
enum: { searchable: false }, | ||
# procs must be defined in one line! | ||
enum: { searchable: false, | ||
collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys } }, |
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's a little thing, but I wonder if we can use the lamdba do; end
syntax here to achieve the same goal?
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 all depends on how good the parser will be, because I fetch raw file line there and parse it using regexp. We'll see how this idea works out and I'll try to implement parser for all possible syntaxes if it's the way to go.
@@ -1,13 +1,12 @@ | |||
require "rails/generators/named_base" | |||
|
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.
Can you add the the deleted line back in?
enum: { searchable: false }, | ||
# procs must be defined in one line! | ||
enum: { searchable: false, | ||
collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys } }, |
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.
Metrics/LineLength: Line is too long. [108/80]
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.
OK, I think I prefer your approach to what I mentioned. Your change only affects generators and doesn't change the internal APIs. It'll be safer particularly as we are starting to think about changing those APIs and we'll want to be careful about it.
If you can address that last couple of comments, I think we can merge this.
Do you mean that I should revert this to the previous version? Because actually it does what you mentioned, but imho it's pretty safe - the only difference in the select field is one ternary operator to check if proc requires any arguments. I've upgraded my regexp so it will handle btw some vulns were found |
Kaminari's update was merged in #1659 so if you rebase against current |
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.
Oh, I see 😅 Sorry, I should have paid more attention.
Hm, I have to admit I'm easy. Your change (sending the field as an argument) actually looks good, and I think it's the reasonable thing to do. I don't think we'd have done differently in the future.
Fixes #1629
Improve enum field definition in the dashboard generator