fix: organization recommend api

This commit is contained in:
yystopf 2021-10-28 15:28:33 +08:00
parent 81ab0b01c0
commit 8698829678
5 changed files with 30 additions and 8 deletions

View File

@ -69,8 +69,7 @@ class Organizations::OrganizationsController < Organizations::BaseController
def recommend
recommend = %W(xuos Huawei_Technology openatom_foundation pkecosystem TensorLayer)
@organizations = Organization.with_visibility(%w(common))
.where(login: recommend).select(:id, :login, :firstname, :lastname, :nickname)
@organizations = Organization.includes(:organization_extension).where(organization_extensions: {recommend: true}).to_a.each_slice(group_size).to_a
end
private
@ -81,6 +80,10 @@ class Organizations::OrganizationsController < Organizations::BaseController
:max_repo_creation, :nickname)
end
def group_size
params.fetch(:group_size, 4)
end
def password
params.fetch(:password, "")
end

View File

@ -76,7 +76,7 @@ class Organization < Owner
validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, case_sensitive: false
validates :login, format: { with: NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" }
delegate :description, :website, :location, :repo_admin_change_team_access,
delegate :description, :website, :location, :repo_admin_change_team_access, :recommend,
:visibility, :max_repo_creation, :num_projects, :num_users, :num_teams, to: :organization_extension, allow_nil: true
scope :with_visibility, ->(visibility) { joins(:organization_extension).where(organization_extensions: {visibility: visibility}) if visibility.present? }

View File

@ -15,6 +15,7 @@
# num_projects :integer default("0")
# num_users :integer default("0")
# num_teams :integer default("0")
# recommend :boolean default("0")
#
# Indexes
#
@ -30,6 +31,8 @@ class OrganizationExtension < ApplicationRecord
enum visibility: {common: 0, limited: 1, privacy: 2}
before_save :set_recommend
def self.build(organization_id, description, website, location, repo_admin_change_team_access, visibility, max_repo_creation)
self.create!(organization_id: organization_id,
description: description,
@ -39,4 +42,9 @@ class OrganizationExtension < ApplicationRecord
visibility: visibility,
max_repo_creation: max_repo_creation)
end
private
def set_recommend
self.recommend = false unless self.common?
end
end

View File

@ -1,6 +1,12 @@
json.organizations @organizations do |organization|
json.id organization.id
json.name organization.login
json.nickname organization.nickname.blank? ? organization.name : organization.nickname
json.avatar_url url_to_avatar(organization)
json.organizations do
json.array! @organizations.each do |group|
json.array! group.each do |organization|
json.id organization.id
json.name organization.login
json.nickname organization.real_name
json.avatar_url url_to_avatar(organization)
json.website organization.website
end
end
end

View File

@ -0,0 +1,5 @@
class AddRecommendToOrganizationExtensions < ActiveRecord::Migration[5.2]
def change
add_column :organization_extensions, :recommend, :boolean, default: false
end
end