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

change Bullet's feature enable logic #738

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

koya1616
Copy link

@koya1616 koya1616 commented Feb 22, 2025

fix #703

This pull request includes changes to the lib/bullet.rb file to improve the handling of feature enablement flags by introducing default values and refactoring the enablement checks.

Refactoring of feature enablement flags:

  • lib/bullet.rb: Modified the enable= method to set default values for @n_plus_one_query_enable, @unused_eager_loading_enable, and @counter_cache_enable if they are not already set.
  • lib/bullet.rb: Refactored the n_plus_one_query_enable?, unused_eager_loading_enable?, and counter_cache_enable? methods to use new helper methods (default_n_plus_one_query_enable?, default_unused_eager_loading_enable?, and default_counter_cache_enable?) for checking the default values.

Note: When calling Bullet.enable, all other detectors are reset to their defaults (true) and need reconfiguring.

Is there any reason why it is set up this?

If there isn't any special reason, I think @n_plus_one_query_enable, @unused_eager_loading_enable, and @counter_cache_enable don't need to be reset. Since there's an enable? && check in the function below, it seems there wouldn't be any impact.

  • def n_plus_one_query_enable?
  • def unused_eager_loading_enable?
  • def counter_cache_enable?

@flyerhzm
Copy link
Owner

@koya1616 looks like a good solution, but it broke the tests, please take a look.

@koya1616 koya1616 force-pushed the change-enable-sett branch 2 times, most recently from 99817ee to f1c665e Compare February 24, 2025 01:35
@koya1616
Copy link
Author

@flyerhzm
Thanks!
I fixed it - can you take a look?

lib/bullet.rb Outdated

@n_plus_one_query_enable ||= true
@unused_eager_loading_enable ||= true
@counter_cache_enable ||= true
Copy link
Owner

Choose a reason for hiding this comment

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

@koya1616 this confused me, does it always set these three instance variables to true?

Copy link
Author

@koya1616 koya1616 Feb 25, 2025

Choose a reason for hiding this comment

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

@flyerhzm
I’m sorry, I misunderstood. I’ve updated it so that it only returns true when the value is nil.

@n_plus_one_query_enable = true if @n_plus_one_query_enable.nil?
@unused_eager_loading_enable = true if @unused_eager_loading_enable.nil?
@counter_cache_enable = true if @counter_cache_enable.nil?

Copy link
Owner

Choose a reason for hiding this comment

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

I don't like this way, it's confused that we set @n_plus_one_query_enable = true when we call Bullet.enable = false.

what about giving the instance variable a default value, so that we don't need to do it in enable= method.

  class << self
    @n_plus_one_query_enable = true
  end

Copy link
Author

Choose a reason for hiding this comment

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

Thanks!
It seems there are cases, as you suggested in your writing style, where class instance variables such as @n_plus_one_query_enable defined within a class << self block are not properly initialized.
As a solution, I'm thinking of creating an initialization method and calling that method to initialize them.
Where do you think I should call this function from? Or is there another better approach?

def initialize_detectors
  @n_plus_one_query_enable = true
  @unused_eager_loading_enable = true
  @counter_cache_enable = true
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting the Bullet.enable swtich, flips the Bullet.unused_eager_loading_enable value
2 participants