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

Extract AccountFollowable concern for Account.followable_by scope #26179

Closed

Conversation

mjankowski
Copy link
Contributor

Pulled out from #26093

This scope was sort of complicated and hard to follow and parse visually.

This change pulls the whole thing out into a concern which does nothing but build the scope back up from better-named private methods. From my local testing, a) the generated sql hasn't changed, b) the specs which exercise this method seem fine with the change.

@renchap renchap added refactoring Improving code quality ruby Pull requests that update Ruby code labels Jul 25, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

This pull request has resolved merge conflicts and is ready for review.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

This pull request has resolved merge conflicts and is ready for review.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 9, 2023

This pull request has resolved merge conflicts and is ready for review.

@mjankowski mjankowski force-pushed the line-length-account-followable branch from c84e615 to 875a8cd Compare October 20, 2023 17:15
@mjankowski
Copy link
Contributor Author

Updated this to move the whole query generation to a new class, and remove the scope from Account since its only ever used in the suggestion area. I think the goal of making it less dense and more readable is better with this approach than just the extraction to the concern.

The suggestions classes, and maybe the new query class could probably be further simplified. I suspect that at least part of the query generation does not need the full low level arel treatment it's currently using, but haven't verified in detail.

Copy link

codecov bot commented Dec 1, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (3f3df9a) 84.83% compared to head (9673d43) 84.84%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26179      +/-   ##
==========================================
+ Coverage   84.83%   84.84%   +0.01%     
==========================================
  Files        1038     1039       +1     
  Lines       28142    28161      +19     
  Branches     4536     4536              
==========================================
+ Hits        23874    23893      +19     
  Misses       3103     3103              
  Partials     1165     1165              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mjankowski mjankowski force-pushed the line-length-account-followable branch from 286b679 to bd2b858 Compare December 3, 2023 15:48
@mjankowski
Copy link
Contributor Author

Rebased and moved to Account:: concerns module.

@mjankowski mjankowski force-pushed the line-length-account-followable branch from 6e85032 to 82fd844 Compare January 11, 2024 11:43
@mjankowski
Copy link
Contributor Author

Rebased this again after the introduction of spec coverage in #28689

Current version of this is pull out the scope creation to a concern module, building two smaller scopes with the conditions, and then combining them. I think this is a net-improvement as-is, but would like feedback. If this is not good to go, the other idea I have is to move (and expand) the coverage to be directly on AccountSuggestions::Source (the only place this is used), and then shift the scope generation into that class and out of Account.

@ClearlyClaire
Copy link
Contributor

Taking a step back from this query, it heavily uses arel to build queries of the form LEFT OUTER JOIN … ON … WHERE id = NULL , which can be expressed in a somewhat simpler (and sometimes more efficient) way with WHERE NOT EXISTS (SELECT 1 FROM … WHERE …). Unfortunately, neither can be easily written with ActiveRecord itself and would require either raw SQL or extensive use of arel.

Given that this query is only ever used in AccountSuggestions::Source, I would move it there and use raw SQL, as this file already does for another part of the query, and in a similar way AccountSuggestions::FriendsOfFriendsSource already does.

Something like:

  def followable_by(account)
    Account.where.not('EXISTS (SELECT 1 FROM follows f WHERE f.target_account_id = accounts.id AND f.account_id = :id)', id: account.id)
           .where.not('EXISTS (SELECT 1 FROM follow_requests f WHERE f.target_account_id = accounts.id AND f.account_id = :id)', id: account.id)
  end

@mjankowski
Copy link
Contributor Author

Great. I'll move the coversage over to account suggesstion spec, expand it a bit, and then explore something along those lines.

@mjankowski
Copy link
Contributor Author

Closing this, goal accomplished by linked PR.

@mjankowski mjankowski closed this Jan 12, 2024
@mjankowski mjankowski deleted the line-length-account-followable branch January 12, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Improving code quality ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants