[24955] Reset project context on accessing filters in a project

pull/6827/head
Oliver Günther 7 years ago
parent dc46477c1f
commit 4a953638e6
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 61
      app/controllers/cost_reports_controller.rb
  2. 2
      spec/controllers/cost_reports_controller_spec.rb
  3. 2
      spec/controllers/custom_fields_controller_spec.rb
  4. 24
      spec/features/project_context_spec.rb

@ -112,43 +112,64 @@ class CostReportsController < ApplicationController
##
# Set a default query to cut down initial load time
def default_filter_parameters
{ operators: { user_id: '=', spent_on: '>d' },
{
operators: { user_id: '=', spent_on: '>d' },
values: { user_id: [User.current.id], spent_on: [30.days.ago.strftime('%Y-%m-%d')] }
}.tap do |hash|
if @project
hash[:operators].merge! project_id: '='
hash[:values].merge! project_id: [@project.id]
set_project_filter(hash, @project.id)
end
end
end
##
# Set a default query to cut down initial load time
def default_group_parameters
{ columns: [:week], rows: [] }.tap do |h|
if @project
h[:rows] << :work_package_id
else
h[:rows] << :project_id
# Get the filter params with an optional project context
def filter_params
filters = super
update_project_context!(filters)
filters
end
##
# Clear the query if the project context changed
def update_project_context!(filters)
# Only in project context
return unless @project
# Only if the project context changed
context = filters[:project_context]
# Context is same, don't set project (allow override)
return if context == @project.id
# Reset context if project missing
if context.nil?
filters[:project_context] = @project.id
return
end
# Update the project context and project_id filter
set_project_filter(filters, @project.id)
end
def set_project_filter(filters, project_id)
filters[:project_context] = project_id
filters[:operators].merge! project_id: '='
filters[:values].merge! project_id: [project_id]
end
##
# We apply a project filter, except when we are just applying a brand new query
def ensure_project_scope!(filters)
return unless ensure_project_scope?
# Set a default query to cut down initial load time
def default_group_parameters
{ columns: [:week], rows: [] }.tap do |h|
if @project
filters[:operators].merge! project_id: '='
filters[:values].merge! project_id: @project.id.to_s
h[:rows] << :work_package_id
else
filters[:operators].delete :project_id
filters[:values].delete :project_id
h[:rows] << :project_id
end
end
def ensure_project_scope?
!(set_filter? or set_unit?)
end
##

@ -34,7 +34,7 @@ describe CostReportsController, type: :controller do
describe "WHEN providing invalid units
WHEN having the view_cost_entries permission" do
before do
get :show, id: 1, unit: -1
get :show, params: { id: 1, unit: -1 }
end
it "should respond with a 404 error" do

@ -54,7 +54,7 @@ describe CustomFieldsController do
cost_query.group_by(custom_field_permanent_name)
cost_query.save!
delete :destroy, id: custom_field.id
delete :destroy, params: { id: custom_field.id }
end
end

@ -0,0 +1,24 @@
require 'spec_helper'
require_relative 'support/pages/cost_report_page'
describe 'Cost report project context', type: :feature, js: true do
let(:project1) { FactoryGirl.create :project }
let(:project2) { FactoryGirl.create :project }
let(:admin) { FactoryGirl.create :admin }
let(:report_page) { ::Pages::CostReportPage.new project }
before do
project1
project2
login_as admin
end
it "switches the project context when visiting another project's cost report" do
visit cost_reports_path(project1)
expect(page).to have_selector('#project_id_arg_1_val option', text: project1.name)
visit cost_reports_path(project2)
expect(page).to have_selector('#project_id_arg_1_val option', text: project2.name)
end
end
Loading…
Cancel
Save