-
-
Notifications
You must be signed in to change notification settings - Fork 278
Let RSpec/SpecFilePathFormat
leverage ActiveSupport inflections when configured
#2090
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
base: master
Are you sure you want to change the base?
Let RSpec/SpecFilePathFormat
leverage ActiveSupport inflections when configured
#2090
Conversation
RSpec/SpecFilePathFormat
leverage ActiveSupport inflections, if…RSpec/SpecFilePathFormat
leverage ActiveSupport inflections, if defined
6899764
to
b403e05
Compare
edit: I was missing the need to |
0d66eec
to
11f3e2a
Compare
RSpec/SpecFilePathFormat
leverage ActiveSupport inflections, if definedRSpec/SpecFilePathFormat
leverage ActiveSupport inflections, if configured and defined
RSpec/SpecFilePathFormat
leverage ActiveSupport inflections, if configured and definedRSpec/SpecFilePathFormat
leverage ActiveSupport inflections when configured and defined
Alternatively: We could remove all the handling to cache the lookup by rescuing and warning on a load error when the user has configured to use Inflectors but they aren't available. The current approach fails gracefully when configured on, but unavailable. |
922fa1e
to
49b0b1c
Compare
RSpec/SpecFilePathFormat
leverage ActiveSupport inflections when configured and definedRSpec/SpecFilePathFormat
leverage ActiveSupport inflections when configured
89d6175
to
b798556
Compare
@@ -924,6 +924,8 @@ RSpec/SpecFilePathFormat: | |||
IgnoreMethods: false | |||
IgnoreMetadata: | |||
type: routing | |||
InflectorPath: "./config/initializers/inflections.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.
most should never need to set this, but just in case
b798556
to
2a70680
Compare
2a70680
to
eb47846
Compare
@bquorning I think I addressed all the comments on the original proposed fix PR. We tested this locally in one of our repos with good results. I made the path configurable, but I didn't think an array of possible integrations in config was warranted at this point. In the event that someone does come along and adds an adapter to a new Inflector library, they could add a new top level config key and use the same InflectorPath key. I guess I just think it's unlikely we'll reach more than 3 custom inflectors at which point we could always switch to making users pick a style instead of having a boolean option. |
bd33b0f
to
84381d1
Compare
f0188e5
to
eb31614
Compare
@@ -924,6 +924,8 @@ RSpec/SpecFilePathFormat: | |||
IgnoreMethods: false | |||
IgnoreMetadata: | |||
type: routing | |||
InflectorPath: "./config/initializers/inflections.rb" | |||
UseActiveSupportInflections: false |
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 am not saying that we should support more inflectors, but just in case: Should we rename this to e.g. Inflector: (default|active_support)
? (And a SupportedInflectors
?) Or would that be premature?
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 had tried to explain this choice in a PR comment:
I made the path configurable, but I didn't think an array of possible integrations in config was warranted at this point.
In the event that someone does come along and adds an adapter to a new Inflector library, they could add a new top level config key and use the same InflectorPath key.
I guess I just think it's unlikely we'll reach more than 3 custom inflectors at which point we could always switch to making users pick a style instead of having a boolean option.
Happy to change it if you want!
But to reframe the above, I was on the "premature" side as I was implementing this. I thought it would be more user-friendly to use a boolean than have folks look up two "magic strings" to use this. They need both the key and the value if we abstract it, whereas they just need the key if it's a boolean.
Just let me know if you want it though.
@@ -57,8 +63,67 @@ def on_top_level_example_group(node) | |||
end | |||
end | |||
|
|||
# For testing and debugging | |||
def self.reset_activesupport_cache! |
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 am not crazy about this method. It feels like maybe we are memoizing on the wrong level somehow.
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 kind of thought we should hoist this (the logic is in the module still), so we can use it more transparently.
But open to alternatives...
ActiveSupport::Inflector.underscore(string) | ||
end | ||
|
||
def self.available?(cop_config) |
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 am not sure if this method should memoize without considering cop_config
.
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 I need the config, but I will see what I can 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.
I mean even if I separate the availability from the config check, I still need the config to look up the inflector_path
and only in the case where it is available?
or something like it.
So config is somewhat coupled to availability
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.
Best I can think of would be something like this, but that seems worse to me:
def active_support_available?
return false unless cop_config.fetch(
'UseActiveSupportInflections',
false
)
ActiveSupportInflector.available?(cop_config)
end
def inflector
@inflector ||= if active_support_available?
ActiveSupportInflector
else
DefaultInflector
end
end
Let me know whart you're aiming for here
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.
Thinking this though once more, I happened to ask myself this question: Do we expect people to give different values for InflectorPath
in different configuration files? Would that even work? I think ActiveSupport::Inflector
sets its inflections globally.
So, should InflectorPath
really be a global configuration that cannot be overwritten?
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 would guess the thinking here is that someone using ActiveSupport but not Rails might have a customized directory structure. And monkey patchers could still use this.
ActiveSupport::Inflector
does seem pretty locked down against changes to the base file, and I imagine the same reasoning applies to not moving the config file around, so Rails itself should be pretty stable around which path it scaffolds the file to.
Maybe something else with nested Rails apps or nested Rails Engines?
I do think this is mildly superfluous, but it was one of the comment requests on the original PR, and now that we have it, I'm hesitant to rip it out again since in theory we're making life easier for +1% of users.
IMO, the heavyweight thing is we have to require 2 files and fail gracefully. Path configurability is minor compared that that.
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 think its worth the sacrifice, we could take the path out of config and then make it a constant on the cop class.
I'm imagining somewhere someone will eventually have to type ruby -e
and patch our cop as they run it from the command line to get it working for their use case, but perhaps the tradeoff is worth it?
f123b9e
to
c987a4a
Compare
…n defined and configured Fix rubocop#740
c987a4a
to
82090d3
Compare
Fix #740
Credit to this PR, reviving with performance and configuration comments addressed:
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.Enabled: true
in.rubocop.yml
.VersionAdded: "<<next>>"
indefault/config.yml
.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"
inconfig/default.yml
.