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

Remove default scope in coder model #195

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/controllers/coders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ class CodersController < ApplicationController
def index
@sort_column = params[:order_by]
@sort_column = 'score' unless %w[score commit_count additions deletions repository_count].include?(@sort_column)
@coders = Coder.extending(CommitStats).with_commit_stats.with_repository_count.order({ @sort_column => :desc })
@coders = Coder.in_organisation.extending(CommitStats).with_commit_stats.with_repository_count.order({ @sort_column => :desc })
end

def show
@coder = Coder.extending(CommitStats)
@coder = Coder.in_organisation
.extending(CommitStats)
.with_commit_stats
.with_repository_count
.find(params[:id])
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class PagesController < ApplicationController
def top4
@coders = Coder.extending(CommitStats).with_commit_stats
@coders = Coder.in_organisation
.extending(CommitStats).with_commit_stats
.where(commits: { committed_at: 1.week.ago.. })
.order(score: :desc)
.limit(4)
Expand All @@ -11,7 +12,8 @@ def top4
.take(4).map do |repo|
[
repo,
Coder.extending(CommitStats).with_commit_stats
Coder.in_organisation
.extending(CommitStats).with_commit_stats
.where(repositories: { id: repo.id }, commits: { committed_at: 1.week.ago.. })
.order(score: :desc)
]
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def show

@sort_column = params[:order_by]
@sort_column = 'score' unless %w[score commit_count additions deletions].include?(@sort_column)
@coders = Coder.extending(CommitStats)
@coders = Coder.in_organisation
.extending(CommitStats)
.with_commit_stats
.where(commits: { repository_id: @repository.id })
.order({ @sort_column => :desc })
Expand Down
4 changes: 2 additions & 2 deletions app/models/coder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class Coder < ApplicationRecord

validates :github_name, uniqueness: true

default_scope -> { where(github_name: OrganisationMember.select(:github_name)) }
scope :in_organisation, -> { where(github_name: OrganisationMember.select(:github_name)) }

def self.from_github(rc, repo)
commit = Rails.application.config.github.repos.commits.get(repo.organisation, repo.name, rc.oid)
return nil if commit.author&.login.blank?

Coder.unscoped.find_or_create_by(github_name: commit.author.login) do |c|
Coder.find_or_create_by(github_name: commit.author.login) do |c|
Rails.application.config.github.users.get(user: commit.author.login).tap do |data|
c.full_name = data.try(:name) || ''
c.avatar_url = data.avatar_url
Expand Down