Commit 62ed1c53 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Explicitly define ordering in models using default_scope

parent dbca8c97
class Admin::DashboardController < Admin::ApplicationController
def index
@projects = Project.order("created_at DESC").limit(10)
@users = User.order("created_at DESC").limit(10)
@groups = Group.order("created_at DESC").limit(10)
@projects = Project.limit(10)
@users = User.limit(10)
@groups = Group.limit(10)
end
end
......@@ -2,7 +2,7 @@ class Admin::GroupsController < Admin::ApplicationController
before_filter :group, only: [:edit, :show, :update, :destroy, :project_update, :project_teams_update]
def index
@groups = Group.order('name ASC')
@groups = Group.order_name
@groups = @groups.search(params[:name]) if params[:name].present?
@groups = @groups.page(params[:page]).per(20)
end
......
......@@ -5,13 +5,13 @@ class Admin::UsersController < Admin::ApplicationController
@users = User.filter(params[:filter])
@users = @users.search(params[:name]) if params[:name].present?
@users = @users.sort(@sort = params[:sort])
@users = @users.alphabetically.page(params[:page])
@users = @users.order_name.page(params[:page])
end
def show
@personal_projects = user.personal_projects
@joined_projects = user.projects.joined(@user)
@keys = user.keys.order('id DESC')
@keys = user.keys
end
def new
......
......@@ -3,7 +3,7 @@ class Profiles::KeysController < ApplicationController
skip_before_filter :authenticate_user!, only: [:get_keys]
def index
@keys = current_user.keys.order('id DESC')
@keys = current_user.keys
end
def show
......
......@@ -14,6 +14,8 @@
#
class BroadcastMessage < ActiveRecord::Base
include Sortable
validates :message, presence: true
validates :starts_at, presence: true
validates :ends_at, presence: true
......
module InternalId
extend ActiveSupport::Concern
include Sortable
included do
validate :set_iid, on: :create
......
# == 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
include Sortable
belongs_to :user
validates :user_id, presence: true
......
......@@ -15,6 +15,7 @@
#
class Event < ActiveRecord::Base
include Sortable
default_scope { where.not(author_id: nil) }
CREATED = 1
......
......@@ -16,6 +16,7 @@
#
class WebHook < ActiveRecord::Base
include Sortable
include HTTParty
default_value_for :push_events, true
......
......@@ -9,6 +9,7 @@
#
class Identity < ActiveRecord::Base
include Sortable
belongs_to :user
validates :extern_uid, allow_blank: true, uniqueness: { scope: :provider }
......
......@@ -15,6 +15,7 @@
require 'digest/md5'
class Key < ActiveRecord::Base
include Sortable
include Gitlab::Popen
belongs_to :user
......
......@@ -11,6 +11,8 @@
#
class Label < ActiveRecord::Base
include Sortable
DEFAULT_COLOR = '#428BCA'
belongs_to :project
......
......@@ -14,6 +14,7 @@
#
class Member < ActiveRecord::Base
include Sortable
include Notifiable
include Gitlab::Access
......
......@@ -14,6 +14,8 @@
require Rails.root.join("app/models/commit")
class MergeRequestDiff < ActiveRecord::Base
include Sortable
# Prevent store of diff
# if commits amount more then 200
COMMITS_SAFE_SIZE = 200
......
......@@ -14,6 +14,7 @@
#
class Namespace < ActiveRecord::Base
include Sortable
include Gitlab::ShellAdapter
has_many :projects, dependent: :destroy
......
......@@ -23,6 +23,7 @@ require 'file_size_validator'
class Note < ActiveRecord::Base
include Mentionable
default_scope { order(created_at: :asc, id: :asc) }
default_value_for :system, false
attr_mentionable :note
......
......@@ -33,6 +33,7 @@ require 'carrierwave/orm/activerecord'
require 'file_size_validator'
class Project < ActiveRecord::Base
include Sortable
include Gitlab::ShellAdapter
include Gitlab::VisibilityLevel
include Gitlab::ConfigHelper
......@@ -86,7 +87,7 @@ class Project < ActiveRecord::Base
has_many :merge_requests, dependent: :destroy, foreign_key: 'target_project_id'
# 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 :issues, -> { order 'issues.state DESC, issues.created_at DESC' }, dependent: :destroy
has_many :issues, dependent: :destroy
has_many :labels, dependent: :destroy
has_many :services, dependent: :destroy
has_many :events, dependent: :destroy
......@@ -139,14 +140,16 @@ class Project < ActiveRecord::Base
mount_uploader :avatar, AttachmentUploader
# 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_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 :in_team, ->(team) { where('projects.id IN (:ids)', ids: team.projects.map(&:id)) }
scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
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 :joined, ->(user) { where('namespace_id != ?', user.namespace_id) }
scope :public_only, -> { where(visibility_level: Project::PUBLIC) }
......
......@@ -15,6 +15,7 @@
# To add new service you should build a class inherited from Service
# and implement a set of methods
class Service < ActiveRecord::Base
include Sortable
serialize :properties, JSON
default_value_for :active, false
......
......@@ -16,6 +16,7 @@
#
class Snippet < ActiveRecord::Base
include Sortable
include Linguist::BlobHelper
include Gitlab::VisibilityLevel
......
......@@ -49,6 +49,7 @@ require 'carrierwave/orm/activerecord'
require 'file_size_validator'
class User < ActiveRecord::Base
include Sortable
include Gitlab::ConfigHelper
include TokenAuthenticatable
extend Gitlab::ConfigHelper
......@@ -176,7 +177,6 @@ class User < ActiveRecord::Base
scope :admins, -> { where(admin: true) }
scope :blocked, -> { with_state(:blocked) }
scope :active, -> { with_state(:active) }
scope :alphabetically, -> { order('name ASC') }
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_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
@authorized_groups ||= begin
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
......@@ -301,7 +301,7 @@ class User < ActiveRecord::Base
project_ids = personal_projects.pluck(:id)
project_ids.push(*groups_projects.pluck(:id))
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
......
......@@ -39,7 +39,6 @@ module API
issues = current_user.issues
issues = filter_issues_state(issues, params[:state]) unless params[:state].nil?
issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
issues = issues.order('issues.id DESC')
present paginate(issues), with: Entities::Issue
end
......@@ -70,7 +69,6 @@ module API
unless params[:milestone].nil?
issues = filter_issues_milestone(issues, params[:milestone])
end
issues = issues.order('issues.id DESC')
present paginate(issues), with: Entities::Issue
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment