-
Notifications
You must be signed in to change notification settings - Fork 898
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
Rubocop fixes for MiqExpression #15488
Rubocop fixes for MiqExpression #15488
Conversation
lib/miq_expression.rb
Outdated
BASE_TABLES = config[:base_tables] | ||
INCLUDE_TABLES = config[:include_tables] | ||
EXCLUDE_COLUMNS = config[:exclude_columns] | ||
EXCLUDE_EXCEPTIONS = config[:exclude_exceptions] | ||
TAG_CLASSES = config[:tag_classes] | ||
EXCLUDE_FROM_RELATS = config[:exclude_from_relats] | ||
FORMAT_SUB_TYPES = config[:format_sub_types] | ||
FORMAT_BYTE_SUFFIXES = FORMAT_SUB_TYPES[:bytes][:units].inject({}) { |h, (v, k)| h[k] = v; h } | ||
FORMAT_BYTE_SUFFIXES = FORMAT_SUB_TYPES[:bytes][:units].each_with_object({}) { |(v, k), h| h[k] = v } |
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.
In this particular case, this feels cleaner to just do
FORMAT_BYTE_SUFFIXES = FORMAT_SUB_TYPES[:bytes][:units].to_h.invert
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.
or if you don't like the invert for whatever reason
FORMAT_BYTE_SUFFIXES = FORMAT_SUB_TYPES[:bytes][:units].transpose.reverse.transpose.to_h
;)
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.
Nice! Didn't know about invert
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.
invert
is nice if you are sure that your values are also unique (which in this case, we know they are because the original code was essentially doing the same thing)
lib/miq_expression.rb
Outdated
@@ -96,13 +97,13 @@ def self._to_human(exp, options = {}) | |||
when "between dates", "between times" | |||
col_name = exp[operator]["field"] | |||
col_type = get_col_type(col_name) | |||
col_human, dumy = operands2humanvalue(exp[operator], options) | |||
col_human, = operands2humanvalue(exp[operator], options) |
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 know it's proper Ruby, but whenever people come across this and don't know Ruby can do this, they get confused. I think it's more readable (and still rubocop compliant) to do:
col_human, _ = operands2humanvalue(exp[operator], options)
or
col_human, _val_human = operands2humanvalue(exp[operator], options)
(or whatever the right-hand-value is actually called)
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.
WDYT about global disabling the rule that complains about your preferred version?
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.
which rule is it (link?)
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.
Link here
lib/miq_expression.rb
Outdated
@@ -392,7 +392,7 @@ def self.get_cols_from_expression(exp, options = {}) | |||
elsif exp.key?("count") | |||
result[exp["count"]] = get_col_info(exp["count"], options) | |||
elsif exp.key?("tag") | |||
# ignore | |||
# ignore |
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.
The original was actually better. Rubocop complains but might not if you put a nil here..
elsif exp.key?("tag")
nil # ignore
else
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.
either that or collapse this with the else
elsif !exp.key?("tag")
exp.each_value { |v| result.merge!(get_cols_from_expression(v, options)) }
end
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.
Oops. This was a mistake! I'll put this back as it wasn't a cop complaint
lib/miq_expression.rb
Outdated
classification = options[:classification] || Classification.find_by_name(col) | ||
# rubocop:enable Rails/DynamicFindBy |
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.
You can inline this without the explicit disable/enable pair
classification = options[:classification] || Classification.find_by_name(col) # rubocop:disable Rails/DynamicFindBy
lib/miq_expression.rb
Outdated
config = YAML.load(ERB.new(File.read(Rails.root.join("config", "miq_expression.yml"))).result) | ||
# rubocop:enable Security/YAMLLoad |
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.
You can inline this without the explicit disable/enable pair
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.
Nice! TIL
lib/miq_expression.rb
Outdated
@@ -666,7 +672,8 @@ def self.quote_human(val, typ) | |||
when "integer", "decimal", "fixnum", "float" | |||
return val.to_i unless val.to_s.number_with_method? || typ.to_s == "float" | |||
if val =~ /^([0-9\.,]+)\.([a-z]+)$/ | |||
val = $1; sfx = $2 | |||
val = $1 | |||
sfx = $2 |
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.
Alternately,
val, sfx = $1, $2
lib/miq_expression.rb
Outdated
@@ -678,7 +685,7 @@ def self.quote_human(val, typ) | |||
when "string", "date", "datetime" | |||
return "\"#{val}\"" | |||
else | |||
return quote(val, typ) | |||
quote(val, typ) |
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.
If you are killing the return here, you can kill it in every other conditional above
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.
Yeah I thought this too....I kind of had blinders on only addressing the ones the cop found, but I'll nix these too
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.
yeah, I figured as much
if c.match(excl) | ||
col = nil | ||
break | ||
unless EXCLUDE_EXCEPTIONS.include?(c) |
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'm not sure why this is needed when it's checked a few lines up
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 weird I must have screwed this up when rebasing. Fix coming up!
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.
the original code had it as well, so I don't think you screwed it up, I was just looking at it without those blinders on 🕶
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.
Actually I think this is equivalent to before, I was seeing something else ;) I'm finding this way too confusing to think about, perhaps I can look into this in a follow up?
lib/miq_expression.rb
Outdated
field | ||
else | ||
get_col_type(field.to_s) || :string | ||
end |
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.
My personal preference, but I can't stand the super indents...I much prefer
col_type =
if field == :count || field == :regkey
field
else
get_col_type(field.to_s) || :string
end
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.
There are a few like that here...what do you think?
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'm OK with that 👍 (more specifically, Matz' auto indent rules are OK with that ;))
lib/miq_expression.rb
Outdated
catobj = Classification.find_by_name(cat) | ||
# rubocop:enable Rails/DynamicFindBy |
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.
inline the disable
lib/miq_expression.rb
Outdated
@@ -1229,8 +1252,6 @@ def fields(expression = exp) | |||
end | |||
end | |||
|
|||
private | |||
|
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.
Although it didn't actually make the ruby_for_date_compare
method private, I believe the intention is for the method to be private. Thus, I think that you need to add a private_class_method :ruby_for_date_compare
lib/miq_expression.rb
Outdated
last_path = ref.collection? ? association_class.model_name.plural : association_class.model_name.singular | ||
end | ||
last_path | ||
end |
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.
Why is this being make public? (Yes, I know it was not actually private, but the intention seems for it to have been a private_class_method
)
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 there are some tests for this that invoke it directly....would you still prefer to declare it private and use send
in the tests?
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.
yes, if it's only tests, then I prefer keeping it private, make the test call send
, and somewhere in the describe line note that it's testing a private method...I usually do something like
describe ".determine_relat_path (private)" do
Cool stuff, @imtayadeway ! |
Thanks for the close 👀 , @Fryguy ! Amendments coming up.... |
This stuff is controlled by us so it isn't a security risk.
It's our method, not legacy Rails
d3cabf4
to
f2e7b0f
Compare
Style/TrailingUnderscoreVariable 🙄 Do we know what the second return value is supposed to be? I've always preferred code that was explicit in this case by prefixing with |
@Fryguy yeah I can update to have a descriptive _variable 👍 |
f2e7b0f
to
072ebcb
Compare
Checked commits imtayadeway/manageiq@5af676d~...072ebcb with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 lib/miq_expression.rb
|
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.
LGTM 👍
Fixes all but one complaints (over the
eval
, which it should probably continue to complain about 😉 )Probably best viewed as individual commits
@miq-bot add-label core, technical debt
@miq-bot assign @gtanzillo