From 4a953638e682765b1313ba5dcde586c4a02a8d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Tue, 5 Dec 2017 10:02:52 +0100 Subject: [PATCH] [24955] Reset project context on accessing filters in a project --- app/controllers/cost_reports_controller.rb | 61 +++++++++++++------ .../cost_reports_controller_spec.rb | 2 +- .../custom_fields_controller_spec.rb | 2 +- spec/features/project_context_spec.rb | 24 ++++++++ 4 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 spec/features/project_context_spec.rb diff --git a/app/controllers/cost_reports_controller.rb b/app/controllers/cost_reports_controller.rb index 7089cec1f0..fd8bff09dd 100644 --- a/app/controllers/cost_reports_controller.rb +++ b/app/controllers/cost_reports_controller.rb @@ -112,16 +112,54 @@ 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 + ## + # 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 + ## # Set a default query to cut down initial load time def default_group_parameters @@ -134,23 +172,6 @@ class CostReportsController < ApplicationController end 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? - if @project - filters[:operators].merge! project_id: '=' - filters[:values].merge! project_id: @project.id.to_s - else - filters[:operators].delete :project_id - filters[:values].delete :project_id - end - end - - def ensure_project_scope? - !(set_filter? or set_unit?) - end - ## # Determine active cost types, the currently selected unit and corresponding cost type def set_cost_types diff --git a/spec/controllers/cost_reports_controller_spec.rb b/spec/controllers/cost_reports_controller_spec.rb index a563f3b237..83c0a2f576 100644 --- a/spec/controllers/cost_reports_controller_spec.rb +++ b/spec/controllers/cost_reports_controller_spec.rb @@ -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 diff --git a/spec/controllers/custom_fields_controller_spec.rb b/spec/controllers/custom_fields_controller_spec.rb index 0c93181493..ad7d2e771f 100644 --- a/spec/controllers/custom_fields_controller_spec.rb +++ b/spec/controllers/custom_fields_controller_spec.rb @@ -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 diff --git a/spec/features/project_context_spec.rb b/spec/features/project_context_spec.rb new file mode 100644 index 0000000000..85c0965338 --- /dev/null +++ b/spec/features/project_context_spec.rb @@ -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 \ No newline at end of file