Explicitly define ordering in models using default_scope
This commit is contained in:
parent
dbca8c9758
commit
62ed1c537e
22 changed files with 67 additions and 16 deletions
|
@ -1,7 +1,7 @@
|
||||||
class Admin::DashboardController < Admin::ApplicationController
|
class Admin::DashboardController < Admin::ApplicationController
|
||||||
def index
|
def index
|
||||||
@projects = Project.order("created_at DESC").limit(10)
|
@projects = Project.limit(10)
|
||||||
@users = User.order("created_at DESC").limit(10)
|
@users = User.limit(10)
|
||||||
@groups = Group.order("created_at DESC").limit(10)
|
@groups = Group.limit(10)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@ class Admin::GroupsController < Admin::ApplicationController
|
||||||
before_filter :group, only: [:edit, :show, :update, :destroy, :project_update, :project_teams_update]
|
before_filter :group, only: [:edit, :show, :update, :destroy, :project_update, :project_teams_update]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@groups = Group.order('name ASC')
|
@groups = Group.order_name
|
||||||
@groups = @groups.search(params[:name]) if params[:name].present?
|
@groups = @groups.search(params[:name]) if params[:name].present?
|
||||||
@groups = @groups.page(params[:page]).per(20)
|
@groups = @groups.page(params[:page]).per(20)
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,13 +5,13 @@ class Admin::UsersController < Admin::ApplicationController
|
||||||
@users = User.filter(params[:filter])
|
@users = User.filter(params[:filter])
|
||||||
@users = @users.search(params[:name]) if params[:name].present?
|
@users = @users.search(params[:name]) if params[:name].present?
|
||||||
@users = @users.sort(@sort = params[:sort])
|
@users = @users.sort(@sort = params[:sort])
|
||||||
@users = @users.alphabetically.page(params[:page])
|
@users = @users.order_name.page(params[:page])
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@personal_projects = user.personal_projects
|
@personal_projects = user.personal_projects
|
||||||
@joined_projects = user.projects.joined(@user)
|
@joined_projects = user.projects.joined(@user)
|
||||||
@keys = user.keys.order('id DESC')
|
@keys = user.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
|
|
@ -3,7 +3,7 @@ class Profiles::KeysController < ApplicationController
|
||||||
skip_before_filter :authenticate_user!, only: [:get_keys]
|
skip_before_filter :authenticate_user!, only: [:get_keys]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@keys = current_user.keys.order('id DESC')
|
@keys = current_user.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class BroadcastMessage < ActiveRecord::Base
|
class BroadcastMessage < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
|
|
||||||
validates :message, presence: true
|
validates :message, presence: true
|
||||||
validates :starts_at, presence: true
|
validates :starts_at, presence: true
|
||||||
validates :ends_at, presence: true
|
validates :ends_at, presence: true
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
module InternalId
|
module InternalId
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
include Sortable
|
||||||
|
|
||||||
included do
|
included do
|
||||||
validate :set_iid, on: :create
|
validate :set_iid, on: :create
|
||||||
|
|
32
app/models/concerns/sortable.rb
Normal file
32
app/models/concerns/sortable.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# == Sortable concern
|
||||||
|
#
|
||||||
|
# Set default scope for ordering objects
|
||||||
|
#
|
||||||
|
module Sortable
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
# By default all models should be ordered
|
||||||
|
# by created_at field starting from newest
|
||||||
|
default_scope { order(created_at: :desc, id: :desc) }
|
||||||
|
scope :order_name, -> { reorder(name: :asc) }
|
||||||
|
scope :order_recent, -> { reorder(created_at: :desc, id: :desc) }
|
||||||
|
scope :order_oldest, -> { reorder(created_at: :asc, id: :asc) }
|
||||||
|
scope :order_recent_updated, -> { reorder(updated_at: :desc, id: :desc) }
|
||||||
|
scope :order_oldest_updated, -> { reorder(updated_at: :asc, id: :asc) }
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def sort(method)
|
||||||
|
case method.to_s
|
||||||
|
when 'name' then order_name_asc
|
||||||
|
when 'recent' then order_recent
|
||||||
|
when 'oldest' then order_oldest
|
||||||
|
when 'recent_updated' then order_recent_updated
|
||||||
|
when 'oldest_updated' then order_oldest_updated
|
||||||
|
else
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,6 +10,8 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Email < ActiveRecord::Base
|
class Email < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Event < ActiveRecord::Base
|
class Event < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
default_scope { where.not(author_id: nil) }
|
default_scope { where.not(author_id: nil) }
|
||||||
|
|
||||||
CREATED = 1
|
CREATED = 1
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class WebHook < ActiveRecord::Base
|
class WebHook < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include HTTParty
|
include HTTParty
|
||||||
|
|
||||||
default_value_for :push_events, true
|
default_value_for :push_events, true
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Identity < ActiveRecord::Base
|
class Identity < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
validates :extern_uid, allow_blank: true, uniqueness: { scope: :provider }
|
validates :extern_uid, allow_blank: true, uniqueness: { scope: :provider }
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
|
|
||||||
class Key < ActiveRecord::Base
|
class Key < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include Gitlab::Popen
|
include Gitlab::Popen
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Label < ActiveRecord::Base
|
class Label < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
|
|
||||||
DEFAULT_COLOR = '#428BCA'
|
DEFAULT_COLOR = '#428BCA'
|
||||||
|
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Member < ActiveRecord::Base
|
class Member < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include Notifiable
|
include Notifiable
|
||||||
include Gitlab::Access
|
include Gitlab::Access
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
require Rails.root.join("app/models/commit")
|
require Rails.root.join("app/models/commit")
|
||||||
|
|
||||||
class MergeRequestDiff < ActiveRecord::Base
|
class MergeRequestDiff < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
|
|
||||||
# Prevent store of diff
|
# Prevent store of diff
|
||||||
# if commits amount more then 200
|
# if commits amount more then 200
|
||||||
COMMITS_SAFE_SIZE = 200
|
COMMITS_SAFE_SIZE = 200
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Namespace < ActiveRecord::Base
|
class Namespace < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include Gitlab::ShellAdapter
|
include Gitlab::ShellAdapter
|
||||||
|
|
||||||
has_many :projects, dependent: :destroy
|
has_many :projects, dependent: :destroy
|
||||||
|
|
|
@ -23,6 +23,7 @@ require 'file_size_validator'
|
||||||
class Note < ActiveRecord::Base
|
class Note < ActiveRecord::Base
|
||||||
include Mentionable
|
include Mentionable
|
||||||
|
|
||||||
|
default_scope { order(created_at: :asc, id: :asc) }
|
||||||
default_value_for :system, false
|
default_value_for :system, false
|
||||||
|
|
||||||
attr_mentionable :note
|
attr_mentionable :note
|
||||||
|
|
|
@ -33,6 +33,7 @@ require 'carrierwave/orm/activerecord'
|
||||||
require 'file_size_validator'
|
require 'file_size_validator'
|
||||||
|
|
||||||
class Project < ActiveRecord::Base
|
class Project < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include Gitlab::ShellAdapter
|
include Gitlab::ShellAdapter
|
||||||
include Gitlab::VisibilityLevel
|
include Gitlab::VisibilityLevel
|
||||||
include Gitlab::ConfigHelper
|
include Gitlab::ConfigHelper
|
||||||
|
@ -53,7 +54,7 @@ class Project < ActiveRecord::Base
|
||||||
attr_accessor :new_default_branch
|
attr_accessor :new_default_branch
|
||||||
|
|
||||||
# Relations
|
# Relations
|
||||||
belongs_to :creator, foreign_key: 'creator_id', class_name: 'User'
|
belongs_to :creator, foreign_key: 'creator_id', class_name: 'User'
|
||||||
belongs_to :group, -> { where(type: Group) }, foreign_key: 'namespace_id'
|
belongs_to :group, -> { where(type: Group) }, foreign_key: 'namespace_id'
|
||||||
belongs_to :namespace
|
belongs_to :namespace
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ class Project < ActiveRecord::Base
|
||||||
has_many :merge_requests, dependent: :destroy, foreign_key: 'target_project_id'
|
has_many :merge_requests, dependent: :destroy, foreign_key: 'target_project_id'
|
||||||
# Merge requests from source project should be kept when source project was removed
|
# Merge requests from source project should be kept when source project was removed
|
||||||
has_many :fork_merge_requests, foreign_key: 'source_project_id', class_name: MergeRequest
|
has_many :fork_merge_requests, foreign_key: 'source_project_id', class_name: MergeRequest
|
||||||
has_many :issues, -> { order 'issues.state DESC, issues.created_at DESC' }, dependent: :destroy
|
has_many :issues, dependent: :destroy
|
||||||
has_many :labels, dependent: :destroy
|
has_many :labels, dependent: :destroy
|
||||||
has_many :services, dependent: :destroy
|
has_many :services, dependent: :destroy
|
||||||
has_many :events, dependent: :destroy
|
has_many :events, dependent: :destroy
|
||||||
|
@ -139,14 +140,16 @@ class Project < ActiveRecord::Base
|
||||||
mount_uploader :avatar, AttachmentUploader
|
mount_uploader :avatar, AttachmentUploader
|
||||||
|
|
||||||
# Scopes
|
# Scopes
|
||||||
|
scope :sorted_by_activity, -> { reorder('projects.last_activity_at DESC') }
|
||||||
|
scope :sorted_by_stars, -> { reorder('projects.star_count DESC') }
|
||||||
|
scope :sorted_by_names, -> { joins(:namespace).reorder('namespaces.name ASC, projects.name ASC') }
|
||||||
|
|
||||||
scope :without_user, ->(user) { where('projects.id NOT IN (:ids)', ids: user.authorized_projects.map(&:id) ) }
|
scope :without_user, ->(user) { where('projects.id NOT IN (:ids)', ids: user.authorized_projects.map(&:id) ) }
|
||||||
scope :without_team, ->(team) { team.projects.present? ? where('projects.id NOT IN (:ids)', ids: team.projects.map(&:id)) : scoped }
|
scope :without_team, ->(team) { team.projects.present? ? where('projects.id NOT IN (:ids)', ids: team.projects.map(&:id)) : scoped }
|
||||||
scope :not_in_group, ->(group) { where('projects.id NOT IN (:ids)', ids: group.project_ids ) }
|
scope :not_in_group, ->(group) { where('projects.id NOT IN (:ids)', ids: group.project_ids ) }
|
||||||
scope :in_team, ->(team) { where('projects.id IN (:ids)', ids: team.projects.map(&:id)) }
|
scope :in_team, ->(team) { where('projects.id IN (:ids)', ids: team.projects.map(&:id)) }
|
||||||
scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
|
scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
|
||||||
scope :in_group_namespace, -> { joins(:group) }
|
scope :in_group_namespace, -> { joins(:group) }
|
||||||
scope :sorted_by_activity, -> { reorder('projects.last_activity_at DESC') }
|
|
||||||
scope :sorted_by_stars, -> { reorder('projects.star_count DESC') }
|
|
||||||
scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
|
scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
|
||||||
scope :joined, ->(user) { where('namespace_id != ?', user.namespace_id) }
|
scope :joined, ->(user) { where('namespace_id != ?', user.namespace_id) }
|
||||||
scope :public_only, -> { where(visibility_level: Project::PUBLIC) }
|
scope :public_only, -> { where(visibility_level: Project::PUBLIC) }
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
# To add new service you should build a class inherited from Service
|
# To add new service you should build a class inherited from Service
|
||||||
# and implement a set of methods
|
# and implement a set of methods
|
||||||
class Service < ActiveRecord::Base
|
class Service < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
serialize :properties, JSON
|
serialize :properties, JSON
|
||||||
|
|
||||||
default_value_for :active, false
|
default_value_for :active, false
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class Snippet < ActiveRecord::Base
|
class Snippet < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include Linguist::BlobHelper
|
include Linguist::BlobHelper
|
||||||
include Gitlab::VisibilityLevel
|
include Gitlab::VisibilityLevel
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,7 @@ require 'carrierwave/orm/activerecord'
|
||||||
require 'file_size_validator'
|
require 'file_size_validator'
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
|
include Sortable
|
||||||
include Gitlab::ConfigHelper
|
include Gitlab::ConfigHelper
|
||||||
include TokenAuthenticatable
|
include TokenAuthenticatable
|
||||||
extend Gitlab::ConfigHelper
|
extend Gitlab::ConfigHelper
|
||||||
|
@ -176,7 +177,6 @@ class User < ActiveRecord::Base
|
||||||
scope :admins, -> { where(admin: true) }
|
scope :admins, -> { where(admin: true) }
|
||||||
scope :blocked, -> { with_state(:blocked) }
|
scope :blocked, -> { with_state(:blocked) }
|
||||||
scope :active, -> { with_state(:active) }
|
scope :active, -> { with_state(:active) }
|
||||||
scope :alphabetically, -> { order('name ASC') }
|
|
||||||
scope :in_team, ->(team){ where(id: team.member_ids) }
|
scope :in_team, ->(team){ where(id: team.member_ids) }
|
||||||
scope :not_in_team, ->(team){ where('users.id NOT IN (:ids)', ids: team.member_ids) }
|
scope :not_in_team, ->(team){ where('users.id NOT IN (:ids)', ids: team.member_ids) }
|
||||||
scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all }
|
scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all }
|
||||||
|
@ -290,7 +290,7 @@ class User < ActiveRecord::Base
|
||||||
def authorized_groups
|
def authorized_groups
|
||||||
@authorized_groups ||= begin
|
@authorized_groups ||= begin
|
||||||
group_ids = (groups.pluck(:id) + authorized_projects.pluck(:namespace_id))
|
group_ids = (groups.pluck(:id) + authorized_projects.pluck(:namespace_id))
|
||||||
Group.where(id: group_ids).order('namespaces.name ASC')
|
Group.where(id: group_ids)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -301,7 +301,7 @@ class User < ActiveRecord::Base
|
||||||
project_ids = personal_projects.pluck(:id)
|
project_ids = personal_projects.pluck(:id)
|
||||||
project_ids.push(*groups_projects.pluck(:id))
|
project_ids.push(*groups_projects.pluck(:id))
|
||||||
project_ids.push(*projects.pluck(:id).uniq)
|
project_ids.push(*projects.pluck(:id).uniq)
|
||||||
Project.where(id: project_ids).joins(:namespace).order('namespaces.name ASC')
|
Project.where(id: project_ids)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,6 @@ module API
|
||||||
issues = current_user.issues
|
issues = current_user.issues
|
||||||
issues = filter_issues_state(issues, params[:state]) unless params[:state].nil?
|
issues = filter_issues_state(issues, params[:state]) unless params[:state].nil?
|
||||||
issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
|
issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
|
||||||
issues = issues.order('issues.id DESC')
|
|
||||||
|
|
||||||
present paginate(issues), with: Entities::Issue
|
present paginate(issues), with: Entities::Issue
|
||||||
end
|
end
|
||||||
|
@ -70,7 +69,6 @@ module API
|
||||||
unless params[:milestone].nil?
|
unless params[:milestone].nil?
|
||||||
issues = filter_issues_milestone(issues, params[:milestone])
|
issues = filter_issues_milestone(issues, params[:milestone])
|
||||||
end
|
end
|
||||||
issues = issues.order('issues.id DESC')
|
|
||||||
|
|
||||||
present paginate(issues), with: Entities::Issue
|
present paginate(issues), with: Entities::Issue
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue