Commit dad53553 authored by Jan Provaznik's avatar Jan Provaznik

Allow to find epics by the given time frame

On the epics roadmap page (see #3559) we need to display epics
in the given time frame. This patch adds an additional filter to
the Epic finder which allows to filter only epics which overlap
with the given time frame, epics which do not have start and end date
set are intentionally ignored.

This filter is not used anywhere yet, but it will be used in the MR for #3559.
This patch is a separate MR to avoid overgrowing and mixing too many
things together in the frontend patch for #3559.

Related #3559
parent 40b6c91f
class AddDateIndexesToEpics < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :epics, :start_date
add_concurrent_index :epics, :end_date
end
def down
remove_concurrent_index :epics, :start_date
remove_concurrent_index :epics, :end_date
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180115201419) do
ActiveRecord::Schema.define(version: 20180131104538) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -810,9 +810,11 @@ ActiveRecord::Schema.define(version: 20180115201419) do
add_index "epics", ["assignee_id"], name: "index_epics_on_assignee_id", using: :btree
add_index "epics", ["author_id"], name: "index_epics_on_author_id", using: :btree
add_index "epics", ["end_date"], name: "index_epics_on_end_date", using: :btree
add_index "epics", ["group_id"], name: "index_epics_on_group_id", using: :btree
add_index "epics", ["iid"], name: "index_epics_on_iid", using: :btree
add_index "epics", ["milestone_id"], name: "index_milestone", using: :btree
add_index "epics", ["start_date"], name: "index_epics_on_start_date", using: :btree
create_table "events", force: :cascade do |t|
t.integer "project_id"
......
......@@ -10,6 +10,7 @@ class EpicsFinder < IssuableFinder
items = by_created_at(items)
items = by_search(items)
items = by_author(items)
items = by_timeframe(items)
sort(items)
end
......@@ -48,4 +49,13 @@ class EpicsFinder < IssuableFinder
groups.select { |g| Ability.allowed?(current_user, :read_epic, g) }
end
end
def by_timeframe(items)
return items unless params[:start_date] && params[:end_date]
items
.where('epics.start_date is not NULL or epics.end_date is not NULL')
.where('epics.start_date is NULL or epics.start_date <= ?', params[:end_date].end_of_day)
.where('epics.end_date is NULL or epics.end_date >= ?', params[:start_date].beginning_of_day)
end
end
......@@ -6,8 +6,8 @@ describe EpicsFinder do
let(:group) { create(:group, :private) }
let(:another_group) { create(:group) }
let!(:epic1) { create(:epic, group: group, title: 'This is awesome epic', created_at: 1.week.ago) }
let!(:epic2) { create(:epic, group: group, created_at: 4.days.ago, author: user) }
let!(:epic3) { create(:epic, group: group, description: 'not so awesome') }
let!(:epic2) { create(:epic, group: group, created_at: 4.days.ago, author: user, start_date: 2.days.ago) }
let!(:epic3) { create(:epic, group: group, description: 'not so awesome', start_date: 5.days.ago, end_date: 3.days.ago) }
let!(:epic4) { create(:epic, group: another_group) }
describe '#execute' do
......@@ -89,6 +89,24 @@ describe EpicsFinder do
expect(epics).to contain_exactly(epic1, epic2, epic3, subepic1, subepic2)
end
end
context 'by timeframe' do
it 'returns epics which start in the timeframe' do
expect(epics(start_date: 2.days.ago, end_date: 1.day.ago)).to contain_exactly(epic2)
end
it 'returns epics which end in the timeframe' do
expect(epics(start_date: 4.days.ago, end_date: 3.days.ago)).to contain_exactly(epic3)
end
it 'returns epics which start before and end after the timeframe' do
expect(epics(start_date: 4.days.ago, end_date: 4.days.ago)).to contain_exactly(epic3)
end
it 'ignores epics which do not have start and end date set' do
expect(epics(start_date: 2.days.ago, end_date: 1.day.ago)).not_to include(epic1)
end
end
end
end
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