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

Some more refactoring and testing of BulletTrain::LoadsAndAuthorizesResource #516

Merged

Conversation

aaricpittman
Copy link
Contributor

This PR does the following:

  • BulletTrain::LoadsAndAuthorizesResource#regex_to_remove_controller_namespace to a class method to match expectations in BulletTrain::LoadsAndAuthorizesResource.account_load_and_authorize_resource.
  • Simplifies BulletTrain::LoadsAndAuthorizesResource.model_namespace_from_controller_namespace.
  • Simplifies and adds some checks against undefined dependencies BulletTrain::LoadsAndAuthorizesResource#load_team.
  • Adds some tests around these methods.

Copy link
Contributor

@jagthedrummer jagthedrummer left a comment

Choose a reason for hiding this comment

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

Hey @aaricpittman, this seems to break the starter repo tests that we run in CI. Looks like it breaks things very early in the init process and we don't ever get around to actually running tests. https://app.circleci.com/pipelines/github/bullet-train-co/bullet_train-core/1588/workflows/dcd633a8-a835-4e84-922a-4fe46a30867a

@aaricpittman
Copy link
Contributor Author

@jagthedrummer Sorry, I saw the failures but hadn't had a chance to look into them. I was wondering if this would happen. It is failing because the abstract regex_to_remove_controller_namespace method is getting invoked now because it is defined at the appropriate level.

My guess is that it is an include order issue. I'll look into why and get it fixed.

Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

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

Hey thanks for working on this! I had some questions and suggestions for the changes here.


if defined?(Current) && Current.respond_to?(:team=)
Current.team = @team
end

# If the currently loaded team is saved to the database, make that the user's new current team.
if @team.try(:persisted?)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we've inserted the return unless @team above, we can remove the try here:

Suggested change
if @team.try(:persisted?)
if @team.persisted?

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 not calling try for null safety. I'm calling it because there is nothing in this gem to guarantee that the value of @team responds to persisted?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like it'd be better to raise the NoMethodError exception with our expected interface to ease debugging, but this code path was already wrapped in try so we can keep it.

current_class = Class.new(ActiveSupport::CurrentAttributes) do
attribute :team
end
Object.const_set(:Current, current_class)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this override an actual Current constant in apps? I wonder if this is related to why starter repo tests fail.

I wonder if we'd be better off with something like this in test_helper.rb:

# Define a fallback `Current` when we're running our tests isolated from the starter repo's `Current`.
unless defined?(::Current)
  class Current < ActiveSupport::CurrentAttributes
    attribute :team
  end
end

Copy link
Contributor

Choose a reason for hiding this comment

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

This may be one of those things where writing tests directly in the gem is somewhat lacking compared to writing tests in the starter repo where we have a more robust BT implementation that we can rely 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.

I've updated the test as @kaspth suggested.

I think having to write tests in another application or library because they won't work in the library where the code under-test is written is an indication of one or more software design problems.

bullet_train-super_load_and_authorize_resource has some dependency issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example, the code of this gem has an undeclared dependency on cancancan or, at a minimum, that the class calling account_load_and_authorize_resource defines a method load_and_authorize_resource.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah there are definitely some undeclared dependencies and circular dependencies through a few of the BT packages. I have it on my list to try to sort that out soon.

@aaricpittman aaricpittman force-pushed the load-and-auth-refactor branch 2 times, most recently from 5e18901 to 0cc4481 Compare September 15, 2023 16:40
@jagthedrummer
Copy link
Contributor

@aaricpittman, if possible it might be nice to split this into some smaller PRs that each do one specific bit of refactoring. That way we could merge the bits that we can, and then deal with the tricky bits separately.

@aaricpittman aaricpittman force-pushed the load-and-auth-refactor branch 2 times, most recently from 3237cf2 to ba577d3 Compare September 26, 2023 22:13
@aaricpittman
Copy link
Contributor Author

@jagthedrummer / @kaspth made all the suggested changes and fixed the code so the tests pass.

@jagthedrummer jagthedrummer merged commit 06ffffd into bullet-train-co:main Oct 3, 2023
1 check passed
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.

3 participants