Skip to content

Prevent root_dir option from being empty #448

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

Merged
merged 3 commits into from
Mar 5, 2017

Conversation

lime
Copy link
Contributor

@lime lime commented Mar 2, 2017

This is a quick fix for #340.

When setting root_dir to an empty string (as is done in the default rake task config) this line previously caused root_dir to become an empty array.

This in turn caused factories, specs etc not to be annotated.

This is just a quick band-aid on a larger problem. We have option parsing spread out over many different places, with slight mismatches like this in the assumptions made.

When setting root_dir to an empty string (as is done in the default rake
task config) this line previously caused root_dir to become an empty
array.

This in turn caused factories, specs etc not to be annotated.

This is just a quick band-aid on a larger problem. We have option
parsing spread out over many different places, with slight mismatches
like this in the assumptions made.
@lime
Copy link
Contributor Author

lime commented Mar 2, 2017

I would probably approach the underlying issue by creating a Annotate::Options class, which could encapsulate all the quirks of our available options.

Then each place that uses these options (bin/annotate, rake tasks, direct invocation from code) wouldn't have to encode the same fallbacks & defaults, reducing the risk for bugs like this one.

I most likely won't have time to attempt that kind of overhaul, but maybe someone else would like to give it a try?

@@ -21,7 +21,7 @@ task annotate_models: :environment do
options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models']
options[:root_dir] = ENV['root_dir'] ? ENV['root_dir'].split(',') : ['']
options[:root_dir] = ENV['root_dir'] && !ENV['root_dir'].empty? ? ENV['root_dir'].split(',') : ['']
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if we should fix it where options[:root_dir] being used, make it work whether it's [], [''], or ['', '',...].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

100% yes, we should. :)

I wasn't sure at first of how large the surface of root_dir was, so I wanted to start off with this simple fix. It seems though like this could really easily be taken care of inside AnnotateModels.root_dir.

Would you like me to give that a shot?

Copy link
Owner

Choose a reason for hiding this comment

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

Please and thank you.

@ctran ctran added the bug label Mar 2, 2017
lime added 2 commits March 5, 2017 00:27
These cases are currently handled (inconsistently) in the different
option parsing routines.
The different cases are now handled inside AnnotateModels
@lime
Copy link
Contributor Author

lime commented Mar 4, 2017

There, I changed the root_dir method to return [''] for all blank values, split comma-separated strings and leave other values (i.e. arrays) untouched. How does that look?

Copy link
Owner

@ctran ctran left a comment

Choose a reason for hiding this comment

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

This looks much better! However, can I request that you move this logic into the setter (writer) so it's done once? Thanks so much for indulging me.

@lime
Copy link
Contributor Author

lime commented Mar 5, 2017

However, can I request that you move this logic into the setter (writer) so it's done once?

I guess, although personally I think I would keep it in the getter. :)

Should model_dir be changed then too, to keep some consistency between the two?

@lime
Copy link
Contributor Author

lime commented Mar 5, 2017

As an aside, how do you feel about the idea I mentioned earlier about moving all of this logic into Annotate::Options or something similar? If we know you are interested it might be more likely that someone takes a stab at it. :)

@lime
Copy link
Contributor Author

lime commented Mar 5, 2017

However, can I request that you move this logic into the setter (writer) so it's done once?

I went to do this and then realized why it's problematic: with the getter you'll get [''] even when you never set the value, while with the setter you run the risk of getting nil by default.

I would suggest we keep the current approach.

Copy link
Owner

@ctran ctran left a comment

Choose a reason for hiding this comment

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

Thanks for taking a closer look. I'll merge this.

Regarding the other suggestion of making an options class, I'm all for it if someone wants to take a stab at it.

@ctran ctran merged commit 8341983 into ctran:develop Mar 5, 2017
@ctran ctran added this to the v2.7.2 milestone Jun 2, 2017
johncarney pushed a commit to johncarney/annotate_models that referenced this pull request Jun 19, 2017
* Prevent root_dir option from being empty

When setting root_dir to an empty string (as is done in the default rake
task config) this line previously caused root_dir to become an empty
array.

This in turn caused factories, specs etc not to be annotated.

This is just a quick band-aid on a larger problem. We have option
parsing spread out over many different places, with slight mismatches
like this in the assumptions made.

* Handle blank values & comma-separated strings in AnnotateModels.root_dir

These cases are currently handled (inconsistently) in the different
option parsing routines.

* Pass root_dir forward unmodified

The different cases are now handled inside AnnotateModels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants