-
Notifications
You must be signed in to change notification settings - Fork 46
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
Some more refactoring and testing of BulletTrain::LoadsAndAuthorizesResource #516
Conversation
c26b985
to
7162f65
Compare
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.
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
@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 |
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.
Hey thanks for working on this! I had some questions and suggestions for the changes here.
...nd_authorize_resource/app/controllers/concerns/bullet_train/loads_and_authorizes_resource.rb
Outdated
Show resolved
Hide resolved
...horize_resource/test/controllers/concerns/bullet_train/loads_and_authorizes_resource_test.rb
Outdated
Show resolved
Hide resolved
...horize_resource/test/controllers/concerns/bullet_train/loads_and_authorizes_resource_test.rb
Outdated
Show resolved
Hide resolved
|
||
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?) |
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.
Since we've inserted the return unless @team
above, we can remove the try
here:
if @team.try(:persisted?) | |
if @team.persisted? |
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'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?
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.
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.
...horize_resource/test/controllers/concerns/bullet_train/loads_and_authorizes_resource_test.rb
Show resolved
Hide resolved
current_class = Class.new(ActiveSupport::CurrentAttributes) do | ||
attribute :team | ||
end | ||
Object.const_set(:Current, current_class) |
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.
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
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.
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.
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'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.
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.
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
.
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.
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.
5e18901
to
0cc4481
Compare
@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. |
0cc4481
to
a5b2dca
Compare
…than an instance method
…m_controller_namespace and add a test
3237cf2
to
ba577d3
Compare
ba577d3
to
2ba24d2
Compare
@jagthedrummer / @kaspth made all the suggested changes and fixed the code so the tests pass. |
This PR does the following:
BulletTrain::LoadsAndAuthorizesResource#regex_to_remove_controller_namespace
to a class method to match expectations inBulletTrain::LoadsAndAuthorizesResource.account_load_and_authorize_resource
.BulletTrain::LoadsAndAuthorizesResource.model_namespace_from_controller_namespace
.BulletTrain::LoadsAndAuthorizesResource#load_team
.