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

Refactor loads and authorize resource #525

Closed
wants to merge 17 commits into from

Conversation

kaspth
Copy link
Contributor

@kaspth kaspth commented Sep 6, 2023

I'm trying to see if there's some fairly simple and safe refactoring we could do here. Just because this method is 140 lines long.

through_as_symbols.map { |tas| "@#{tas}" }.join(" || ")
eval "@#{options[:polymorphic]} ||= #{possible_sources_of_parent}"
unless instance_variable_defined?("@#{options[:polymorphic]}")
parent = through_as_symbols.lazy.filter_map { instance_variable_get "@#{_1}" }.first
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to demonstrate how lazy.filter_map {}.first works. It's so we only loop through just the amount of calls we need to find and return a match:

irb(main):004> [1, 2, 3, 4].filter_map { p _1; _1 if _1.even? }.first
1
2
3
4
=> 2
irb(main):005> [1, 2, 3, 4].lazy.filter_map { p _1; _1 if _1.even? }.first
1
2
=> 2

@kaspth kaspth requested a review from jagthedrummer September 6, 2023 14:43
@kaspth
Copy link
Contributor Author

kaspth commented Sep 6, 2023

@jagthedrummer I've tried cut down on some the incidental complexity in account_load_and_authorize_resource while also removing the eval calls. What do you think? Are there certain commits in here that moves too much, that we shouldn't ship?

@jagthedrummer
Copy link
Contributor

@kaspth I don't know a whole lot about this class so it may take me a little time to give this a thorough review.

@jagthedrummer
Copy link
Contributor

@kaspth, I'm having a hard time making sense of this PR. Partly because I don't know much about this class, but also partly because it does several refactorings all at once and it's not clear to me which changes are related to which other changes. Could we break it down into a few smaller ones? Like could we do one PR for replacing eval, and one for changing the call signature of account_load_and_authorize_resource (is it safe to change that?), etc...

@kaspth
Copy link
Contributor Author

kaspth commented Oct 2, 2023

@jagthedrummer hey yeah, good point. I'll try to get back to this, this week.

@jagthedrummer jagthedrummer marked this pull request as draft October 3, 2023 15:53
@jagthedrummer
Copy link
Contributor

@kaspth, just wanted to check in on your thoughts on this one. I've been reading the current version of that method, and I think I have at least a little more context as to what all is going on this PR, and I don't see anything that's obviously concerning. But I do still think it would be nice to split each logical change into smaller PRs. I can take a stab at that if you're not able to get to it.

@kaspth
Copy link
Contributor Author

kaspth commented Dec 13, 2023

@jagthedrummer oh, I forgot about this! I'll take a look at splitting this up tomorrow.

```
account_load_and_authorize_resource :project, :team # New
account_load_and_authorize_resource :project, through: :team # Old
```

The deprecate notice TODO was added over 2 years ago, and Bullet Train is still using the old form. Don't think its happening.
c5ba33b
In the previous commit I removed `.constantize` from `begin`, but `.klass` above is what loads the model and is why we can call `name` on it.

So I don't think this exception is ever hit.
I'm grouping it with the exception so it's easier to see why `through_as_symbols.first` works.
`eval` just evaluates in the same scope, so if we can assign instance variables in there, we can both get and assign them without eval.
@kaspth
Copy link
Contributor Author

kaspth commented Dec 14, 2023

@jagthedrummer ok, I've split all of these out into 5 different PRs. I've stacked them so part-2 merges into part-1 etc. So if you merge them in order, GitHub should point the PR next-in-line branch back to main (I can't remember if GitHub can detect a squashed merge and swap the branch so you may need to do that manually, or delete the part-n branch from the previous PR).

This should let you sequence and ship these in several different releases if you want to slow the rollout down a bit.

@kaspth kaspth deleted the refactor-loads-and-authorize-resource branch December 14, 2023 21:20
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.

2 participants