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

Rubocop fixes for MiqExpression #15488

Merged
merged 25 commits into from
Jul 12, 2017

Conversation

imtayadeway
Copy link
Contributor

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

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 }
Copy link
Member

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

Copy link
Member

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

;)

Copy link
Contributor Author

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

Copy link
Member

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)

@@ -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)
Copy link
Member

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)

Copy link
Contributor Author

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?

Copy link
Member

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?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Link here

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

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

Copy link
Member

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

Copy link
Contributor Author

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

classification = options[:classification] || Classification.find_by_name(col)
# rubocop:enable Rails/DynamicFindBy
Copy link
Member

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

config = YAML.load(ERB.new(File.read(Rails.root.join("config", "miq_expression.yml"))).result)
# rubocop:enable Security/YAMLLoad
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! TIL

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

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

@@ -678,7 +685,7 @@ def self.quote_human(val, typ)
when "string", "date", "datetime"
return "\"#{val}\""
else
return quote(val, typ)
quote(val, typ)
Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

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)
Copy link
Member

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

Copy link
Contributor Author

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!

Copy link
Member

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 🕶

Copy link
Contributor Author

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?

field
else
get_col_type(field.to_s) || :string
end
Copy link
Member

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

Copy link
Member

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?

Copy link
Contributor Author

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 ;))

catobj = Classification.find_by_name(cat)
# rubocop:enable Rails/DynamicFindBy
Copy link
Member

Choose a reason for hiding this comment

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

inline the disable

@@ -1229,8 +1252,6 @@ def fields(expression = exp)
end
end

private

Copy link
Member

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

last_path = ref.collection? ? association_class.model_name.plural : association_class.model_name.singular
end
last_path
end
Copy link
Member

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)

Copy link
Contributor Author

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?

Copy link
Member

@Fryguy Fryguy Jun 30, 2017

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

@Fryguy
Copy link
Member

Fryguy commented Jun 30, 2017

Cool stuff, @imtayadeway !

@imtayadeway
Copy link
Contributor Author

Thanks for the close 👀 , @Fryguy ! Amendments coming up....

@imtayadeway imtayadeway force-pushed the rubocop/fix-miq-expression branch 2 times, most recently from d3cabf4 to f2e7b0f Compare June 30, 2017 21:52
@Fryguy
Copy link
Member

Fryguy commented Jun 30, 2017

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 _

@imtayadeway
Copy link
Contributor Author

@Fryguy yeah I can update to have a descriptive _variable 👍

@miq-bot
Copy link
Member

miq-bot commented Jul 6, 2017

Checked commits imtayadeway/manageiq@5af676d~...072ebcb with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
2 files checked, 1 offense detected

lib/miq_expression.rb

Copy link
Member

@gtanzillo gtanzillo left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@gtanzillo gtanzillo added this to the Sprint 65 Ending Jul 24, 2017 milestone Jul 12, 2017
@gtanzillo gtanzillo merged commit 7659538 into ManageIQ:master Jul 12, 2017
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.

4 participants