Skip to content
Snippets Groups Projects
Commit 62ed1c53 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Explicitly define ordering in models using default_scope

parent dbca8c97
No related branches found
No related tags found
1 merge request!8686add "Uplaod" and "Replace" functionality
Showing
with 64 additions and 11 deletions
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
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -14,6 +14,8 @@
#
 
class BroadcastMessage < ActiveRecord::Base
include Sortable
validates :message, presence: true
validates :starts_at, presence: true
validates :ends_at, presence: true
Loading
Loading
module InternalId
extend ActiveSupport::Concern
include Sortable
 
included do
validate :set_iid, on: :create
Loading
Loading
# == 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
Loading
Loading
@@ -10,6 +10,8 @@
#
 
class Email < ActiveRecord::Base
include Sortable
belongs_to :user
 
validates :user_id, presence: true
Loading
Loading
Loading
Loading
@@ -15,6 +15,7 @@
#
 
class Event < ActiveRecord::Base
include Sortable
default_scope { where.not(author_id: nil) }
 
CREATED = 1
Loading
Loading
Loading
Loading
@@ -16,6 +16,7 @@
#
 
class WebHook < ActiveRecord::Base
include Sortable
include HTTParty
 
default_value_for :push_events, true
Loading
Loading
Loading
Loading
@@ -9,6 +9,7 @@
#
 
class Identity < ActiveRecord::Base
include Sortable
belongs_to :user
 
validates :extern_uid, allow_blank: true, uniqueness: { scope: :provider }
Loading
Loading
Loading
Loading
@@ -15,6 +15,7 @@
require 'digest/md5'
 
class Key < ActiveRecord::Base
include Sortable
include Gitlab::Popen
 
belongs_to :user
Loading
Loading
Loading
Loading
@@ -11,6 +11,8 @@
#
 
class Label < ActiveRecord::Base
include Sortable
DEFAULT_COLOR = '#428BCA'
 
belongs_to :project
Loading
Loading
Loading
Loading
@@ -14,6 +14,7 @@
#
 
class Member < ActiveRecord::Base
include Sortable
include Notifiable
include Gitlab::Access
 
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -14,6 +14,7 @@
#
 
class Namespace < ActiveRecord::Base
include Sortable
include Gitlab::ShellAdapter
 
has_many :projects, dependent: :destroy
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
@@ -53,7 +54,7 @@ class Project < ActiveRecord::Base
attr_accessor :new_default_branch
 
# 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 :namespace
 
Loading
Loading
@@ -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
Loading
Loading
@@ -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) }
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -16,6 +16,7 @@
#
 
class Snippet < ActiveRecord::Base
include Sortable
include Linguist::BlobHelper
include Gitlab::VisibilityLevel
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment