Commit cfd475a4 authored by Tiago Botelho's avatar Tiago Botelho
Browse files

Removes default scope from sortable

parent d1b60cbc
......@@ -2,7 +2,7 @@ class Admin::BroadcastMessagesController < Admin::ApplicationController
before_action :finder, only: [:edit, :update, :destroy]
def index
@broadcast_messages = BroadcastMessage.reorder("ends_at DESC").page(params[:page])
@broadcast_messages = BroadcastMessage.order(ends_at: :desc).page(params[:page])
@broadcast_message = BroadcastMessage.new
end
......
class Admin::DashboardController < Admin::ApplicationController
def index
@projects = Project.without_deleted.with_route.limit(10)
@users = User.limit(10)
@groups = Group.with_route.limit(10)
@projects = Project.order_id_desc.without_deleted.with_route.limit(10)
@users = User.order_id_desc.limit(10)
@groups = Group.order_id_desc.with_route.limit(10)
end
end
......@@ -17,7 +17,7 @@ def projects
end
def keys
@keys = user.keys
@keys = user.keys.order_id_desc
end
def new
......
class Dashboard::GroupsController < Dashboard::ApplicationController
def index
@sort = params[:sort] || 'id_desc'
@groups =
if params[:parent_id] && Group.supports_nested_groups?
parent = Group.find_by(id: params[:parent_id])
......@@ -15,7 +17,7 @@ def index
@groups = @groups.search(params[:filter_groups]) if params[:filter_groups].present?
@groups = @groups.includes(:route)
@groups = @groups.sort(@sort = params[:sort])
@groups = @groups.sort(@sort)
@groups = @groups.page(params[:page])
respond_to do |format|
......
class Profiles::EmailsController < Profiles::ApplicationController
def index
@primary = current_user.email
@emails = current_user.emails
@emails = current_user.emails.order_id_desc
end
def create
......
......@@ -2,7 +2,7 @@ class Profiles::KeysController < Profiles::ApplicationController
skip_before_action :authenticate_user!, only: [:get_keys]
def index
@keys = current_user.keys
@keys = current_user.keys.order_id_desc
@key = Key.new
end
......
......@@ -27,7 +27,7 @@ def define_diff_vars
@merge_request.merge_request_diff
end
@merge_request_diffs = @merge_request.merge_request_diffs.viewable.select_without_diff
@merge_request_diffs = @merge_request.merge_request_diffs.viewable.select_without_diff.order_id_desc
@comparable_diffs = @merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }
if params[:start_sha].present?
......
......@@ -47,6 +47,10 @@ def resend_invite
end
end
def import
@projects = current_user.authorized_projects.order_id_desc
end
def apply_import
source_project = Project.find(params[:source_project_id])
......
......@@ -9,6 +9,7 @@ def execute(from_project, search: nil, offset_id: nil)
projects = @user.projects_where_can_admin_issues
projects = projects.search(search) if search.present?
projects = projects.excluding_project(from_project)
projects = projects.order_id_desc
# infinite scroll using offset
projects = projects.where('projects.id < ?', offset_id) if offset_id.present?
......
......@@ -121,7 +121,7 @@ def by_search(items)
end
def sort(items)
params[:sort].present? ? items.sort(params[:sort]) : items
params[:sort].present? ? items.sort(params[:sort]) : items.order_id_desc
end
def by_archived(projects)
......
......@@ -118,7 +118,7 @@ def type
end
def sort(items)
params[:sort] ? items.sort(params[:sort]) : items.reorder(id: :desc)
params[:sort] ? items.sort(params[:sort]) : items.order_id_desc
end
def by_action(items)
......
......@@ -56,7 +56,7 @@ def milestone_options(object)
end
def project_options(issuable, current_user, ability: :read_project)
projects = current_user.authorized_projects
projects = current_user.authorized_projects.order_id_desc
projects = projects.select do |project|
current_user.can?(ability, project)
end
......
......@@ -85,7 +85,7 @@ def project_autocomplete
# Autocomplete results for the current user's groups
def groups_autocomplete(term, limit = 5)
current_user.authorized_groups.search(term).limit(limit).map do |group|
current_user.authorized_groups.order_id_desc.search(term).limit(limit).map do |group|
{
category: "Groups",
id: group.id,
......@@ -97,7 +97,7 @@ def groups_autocomplete(term, limit = 5)
# Autocomplete results for the current user's projects
def projects_autocomplete(term, limit = 5)
current_user.authorized_projects.search_by_title(term)
current_user.authorized_projects.order_id_desc.search_by_title(term)
.sorted_by_stars.non_archived.limit(limit).map do |p|
{
category: "Projects",
......
......@@ -33,7 +33,7 @@ def self.current
end
def self.current_and_future_messages
where('ends_at > :now', now: Time.zone.now).reorder(id: :asc)
where('ends_at > :now', now: Time.zone.now).order_id_asc
end
def active?
......
......@@ -6,10 +6,6 @@ module Sortable
extend ActiveSupport::Concern
included do
# By default all models should be ordered
# by created_at field starting from newest
default_scope { order_id_desc }
scope :order_id_desc, -> { reorder(id: :desc) }
scope :order_id_asc, -> { reorder(id: :asc) }
scope :order_created_desc, -> { reorder(created_at: :desc) }
......
......@@ -70,7 +70,7 @@ def update_tracked_fields!(request)
#
# Namespace for personal projects
has_one :namespace, -> { where type: nil }, dependent: :destroy, foreign_key: :owner_id, autosave: true # rubocop:disable Cop/ActiveRecordDependent
has_one :namespace, -> { where(type: nil) }, dependent: :destroy, foreign_key: :owner_id, autosave: true # rubocop:disable Cop/ActiveRecordDependent
# Profile
has_many :keys, -> do
......@@ -255,11 +255,13 @@ def find_for_database_authentication(warden_conditions)
end
def sort(method)
case method.to_s
order_method = method || 'id_desc'
case order_method.to_s
when 'recent_sign_in' then order_recent_sign_in
when 'oldest_sign_in' then order_oldest_sign_in
else
order_by(method)
order_by(order_method)
end
end
......@@ -367,7 +369,7 @@ def find_by_personal_access_token(token_string)
# Returns a user for the given SSH key.
def find_by_ssh_key_id(key_id)
find_by(id: Key.unscoped.select(:user_id).where(id: key_id))
Key.find_by(id: key_id)&.user
end
def find_by_full_path(path, follow_redirects: false)
......
......@@ -115,7 +115,7 @@ def extractor
if issuable.allows_multiple_assignees?
issuable.assignees.pluck(:id) + users.map(&:id)
else
[users.last.id]
[users.first.id]
end
end
......
......@@ -8,7 +8,7 @@
= form_tag apply_import_project_project_members_path(@project), method: 'post', class: 'form-horizontal' do
.form-group
= label_tag :source_project_id, "Project", class: 'control-label'
.col-sm-10= select_tag(:source_project_id, options_from_collection_for_select(current_user.authorized_projects, :id, :name_with_namespace), prompt: "Select project", class: "select2 lg", required: true)
.col-sm-10= select_tag(:source_project_id, options_from_collection_for_select(@projects, :id, :name_with_namespace), prompt: "Select project", class: "select2 lg", required: true)
.form-actions
= button_tag 'Import project members', class: "btn btn-create"
......
---
title: Removes Sortable default scope.
merge_request: 13558
author:
type: fixed
......@@ -52,7 +52,7 @@ class Spinach::Features::ProjectFork < Spinach::FeatureSteps
end
step 'I visit the forks page of the "Shop" project' do
@project = Project.where(name: 'Shop').last
@project = Project.where(name: 'Shop').first
visit project_forks_path(@project)
end
......
......@@ -20,7 +20,7 @@ def find_message
use :pagination
end
get do
messages = BroadcastMessage.all
messages = BroadcastMessage.all.order_id_desc
present paginate(messages), with: Entities::BroadcastMessage
end
......
......@@ -21,7 +21,7 @@ class MergeRequestDiffs < Grape::API
get ":id/merge_requests/:merge_request_iid/versions" do
merge_request = find_merge_request_with_access(params[:merge_request_iid])
present paginate(merge_request.merge_request_diffs), with: Entities::MergeRequestDiff
present paginate(merge_request.merge_request_diffs.order_id_desc), with: Entities::MergeRequestDiff
end
desc 'Get a single merge request diff version' do
......
......@@ -28,7 +28,7 @@ module MilestoneResponses
end
def list_milestones_for(parent)
milestones = parent.milestones
milestones = parent.milestones.order_id_desc
milestones = Milestone.filter_by_state(milestones, params[:state])
milestones = filter_by_iid(milestones, params[:iids]) if params[:iids].present?
milestones = filter_by_search(milestones, params[:search]) if params[:search]
......
......@@ -20,7 +20,7 @@ class MergeRequestDiffs < Grape::API
get ":id/merge_requests/:merge_request_id/versions" do
merge_request = find_merge_request_with_access(params[:merge_request_id])
present merge_request.merge_request_diffs, with: ::API::Entities::MergeRequestDiff
present merge_request.merge_request_diffs.order_id_desc, with: ::API::Entities::MergeRequestDiff
end
desc 'Get a single merge request diff version' do
......
......@@ -34,6 +34,7 @@ def filter_milestones_state(milestones, state)
milestones = user_project.milestones
milestones = filter_milestones_state(milestones, params[:state])
milestones = filter_by_iid(milestones, params[:iid]) if params[:iid].present?
milestones = milestones.order_id_desc
present paginate(milestones), with: ::API::Entities::Milestone
end
......
......@@ -119,7 +119,7 @@ def present_projects(projects, options = {})
get do
authenticate!
present_projects current_user.authorized_projects,
present_projects current_user.authorized_projects.order_id_desc,
with: ::API::V3::Entities::ProjectWithAccess
end
......
......@@ -18,7 +18,7 @@
context 'when selecting the namespace' do
let(:user) { create(:admin) }
let!(:namespace) { create(:namespace, name: 'asd', owner: user) }
let!(:namespace) { user.namespace }
let(:project_path) { 'test-project-path' + SecureRandom.hex }
context 'prefilled the path' do
......@@ -66,12 +66,11 @@
end
scenario 'invalid project' do
namespace = create(:namespace, name: 'asdf', owner: user)
project = create(:project, namespace: namespace)
project = create(:project, namespace: user.namespace)
visit new_project_path
select2(namespace.id, from: '#project_namespace_id')
select2(user.namespace.id, from: '#project_namespace_id')
fill_in :project_path, with: project.name, visible: true
click_link 'GitLab export'
attach_file('file', file)
......
......@@ -15,7 +15,7 @@
result = described_class.new(group).execute
expect(result.to_a).to eq([member3, member2, member1])
expect(result.to_a).to match_array([member3, member2, member1])
end
it 'returns members for nested group', :nested_groups do
......@@ -27,6 +27,6 @@
result = described_class.new(nested_group).execute
expect(result.to_a).to eq([member4, member3, member1])
expect(result.to_a).to match_array([member1, member3, member4])
end
end
......@@ -17,6 +17,6 @@
result = described_class.new(project, user2).execute
expect(result.to_a).to eq([member3, member2, member1])
expect(result.to_a).to match_array([member1, member2, member3])
end
end
......@@ -404,6 +404,6 @@
let!(:project1) { create(:project_empty_repo, namespace: group) }
let!(:project2) { create(:project_empty_repo, namespace: child) }
it { expect(group.all_projects.to_a).to eq([project2, project1]) }
it { expect(group.all_projects.to_a).to match_array([project2, project1]) }
end
end
......@@ -1517,7 +1517,7 @@
developer_project = create(:project) { |p| p.add_developer(user) }
master_project = create(:project) { |p| p.add_master(user) }
expect(user.projects_where_can_admin_issues.to_a).to eq([master_project, developer_project, reporter_project])
expect(user.projects_where_can_admin_issues.to_a).to match_array([master_project, developer_project, reporter_project])
expect(user.can?(:admin_issue, master_project)).to eq(true)
expect(user.can?(:admin_issue, developer_project)).to eq(true)
expect(user.can?(:admin_issue, reporter_project)).to eq(true)
......
......@@ -14,7 +14,7 @@
describe 'GET /projects/:id/merge_requests/:merge_request_iid/versions' do
it 'returns 200 for a valid merge request' do
get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/versions", user)
merge_request_diff = merge_request.merge_request_diffs.first
merge_request_diff = merge_request.merge_request_diffs.last
expect(response.status).to eq 200
expect(response).to include_pagination_headers
......
......@@ -574,7 +574,7 @@
expect { post api("/projects/user/#{user.id}", admin), name: 'Foo Project' }.to change {Project.count}.by(1)
expect(response).to have_http_status(201)
project = Project.first
project = Project.last
expect(project.name).to eq('Foo Project')
expect(project.path).to eq('foo-project')
......@@ -585,7 +585,7 @@
.to change { Project.count }.by(1)
expect(response).to have_http_status(201)
project = Project.first
project = Project.last
expect(project.name).to eq('Foo Project')
expect(project.path).to eq('path-project-Foo')
......
......@@ -14,7 +14,7 @@
describe 'GET /projects/:id/merge_requests/:merge_request_id/versions' do
it 'returns 200 for a valid merge request' do
get v3_api("/projects/#{project.id}/merge_requests/#{merge_request.id}/versions", user)
merge_request_diff = merge_request.merge_request_diffs.first
merge_request_diff = merge_request.merge_request_diffs.last
expect(response.status).to eq 200
expect(json_response.size).to eq(merge_request.merge_request_diffs.size)
......
......@@ -93,7 +93,7 @@
context 'with private project and builds' do
before do
project.members.first.update(access_level: Gitlab::Access::GUEST)
project.members.last.update(access_level: Gitlab::Access::GUEST)
end
it 'does not list the test events' do
......
......@@ -1237,7 +1237,7 @@ def send_notifications(*new_mentions)
end
it do
group_member = group.members.first
group_member = group.members.last
expect do
notification.decline_group_invite(group_member)
......@@ -1285,7 +1285,7 @@ def create_member!
end
it do
project_member = project.members.first
project_member = project.members.last
expect do
notification.decline_project_invite(project_member)
......
......@@ -140,9 +140,14 @@ def assign_session_token(provider)
end
context "when a namespace with the provider user's username already exists" do
let!(:existing_namespace) { create(:namespace, name: other_username, owner: user) }
let!(:existing_namespace) { user.namespace }
context "when the namespace is owned by the GitLab user" do
before do
user.username = other_username
user.save
end
it "takes the existing namespace" do
expect(Gitlab::GithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, existing_namespace, user, access_params, type: provider)
......@@ -153,12 +158,9 @@ def assign_session_token(provider)
end
context "when the namespace is not owned by the GitLab user" do
before do
existing_namespace.owner = create(:user)
existing_namespace.save
end
it "creates a project using user's namespace" do
create(:user, username: other_username)
expect(Gitlab::GithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: true))
......
......@@ -127,6 +127,7 @@
it "asks the project to trigger all hooks" do
allow(Project).to receive(:find_by).and_return(project)
expect(project).to receive(:execute_hooks).twice
expect(project).to receive(:execute_services).twice
......@@ -135,6 +136,7 @@
it "enqueues a UpdateMergeRequestsWorker job" do
allow(Project).to receive(:find_by).and_return(project)
expect(UpdateMergeRequestsWorker).to receive(:perform_async).with(project.id, project.owner.id, any_args)
described_class.new.perform(gl_repository, key_id, base64_changes)
......
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