Skip to content

Commit

Permalink
Merge pull request #1664 from 24pullrequests/faster-homepage
Browse files Browse the repository at this point in the history
Make sql do the work to find 24 random users with pull requests
  • Loading branch information
nodunayo committed Dec 2, 2016
2 parents a8b2b71 + 40d70ae commit 5986dfe
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/static_controller.rb
Expand Up @@ -2,8 +2,8 @@ class StaticController < ApplicationController
def homepage
@projects = Project.includes(:labels).active.order("RANDOM()").limit(6)
@featured_projects = Project.includes(:labels).featured.order("RANDOM()").limit(6)
@users = User.order('pull_requests_count desc').limit(200).sample(24)
@orgs = Organisation.order_by_pull_requests.limit(200).sample(24)
@users = User.with_any_pull_requests.random.limit(24)
@orgs = Organisation.with_any_pull_requests.random.limit(24)
@pull_requests = PullRequest.year(current_year).order('created_at desc').limit(5)
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/organisation.rb
Expand Up @@ -2,6 +2,8 @@ class Organisation < ApplicationRecord
has_and_belongs_to_many :users
has_many :pull_requests, -> { order('created_at desc').for_aggregation }, through: :users

scope :with_any_pull_requests, -> { where('organisations.pull_request_count > 0') }
scope :random, -> { order("RANDOM()") }
scope :order_by_pull_requests, -> do
order('organisations.pull_request_count desc, organisations.login asc')
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Expand Up @@ -17,6 +17,8 @@ class User < ApplicationRecord
has_many :archived_pull_requests

scope :by_language, ->(language) { joins(:skills).where('lower(language) = ?', language.downcase) }
scope :with_any_pull_requests, -> { where('users.pull_requests_count > 0') }
scope :random, -> { order("RANDOM()") }

paginates_per 99

Expand Down

0 comments on commit 5986dfe

Please sign in to comment.