From e352ce4088a40dfc193c6a67299e7b5d5aa26571 Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Fri, 30 May 2014 16:30:04 +0200 Subject: [PATCH] Remove custom field from query in session --- .../patches/custom_fields_controller_patch.rb | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/open_project/reporting/patches/custom_fields_controller_patch.rb b/lib/open_project/reporting/patches/custom_fields_controller_patch.rb index 3989201190..64ce7751af 100644 --- a/lib/open_project/reporting/patches/custom_fields_controller_patch.rb +++ b/lib/open_project/reporting/patches/custom_fields_controller_patch.rb @@ -36,29 +36,52 @@ module OpenProject::Reporting::Patches def destroy_with_custom_fields id = @custom_field.id - if @custom_field.destroyed? - custom_field_name = "CustomField#{id}" - reports = CostQuery.where("serialized LIKE '%CustomField#{id}%'") - - reports.each do |report| - filters = report.serialized[:filters].reject { |f| f[0] == custom_field_name } - group_bys = report.serialized[:group_bys].reject { |f| f[0] == custom_field_name } - updated_report = build_query(report.engine, filters, group_bys) - report.migrate(updated_report) - report.save! - end - end + reports = CostQuery.where("serialized LIKE '%CustomField#{id}%'") + + remove_custom_field_from_cost_report(reports, id) + remove_custom_field_from_session(id) destroy_without_custom_fields end private + def remove_custom_field_from_cost_report(affected_reports, id) + custom_field_name = "CustomField#{id}" + + affected_reports.each do |report| + filters = reject_from_query_properties(report, :filters, custom_field_name) + group_bys = reject_from_query_properties(report, :group_bys, custom_field_name) + updated_report = build_query(report.engine, filters, group_bys) + report.migrate(updated_report) + report.save! + end + end + + def reject_from_query_properties(report, property, custom_field_name) + report.serialized[property].reject { |f| f[0] == custom_field_name } + end + def build_query(report_engine, filters, groups = {}) query = report_engine.deserialize({ filters: filters, group_bys: groups }) query.serialize query end + + def remove_custom_field_from_session(id) + custom_field_name = "custom_field#{id}".to_sym + report_engine_name = CostQuery.name.underscore.to_sym + cookie = session[report_engine_name] + + if cookie + cookie[:filters][:operators].delete(custom_field_name) + cookie[:filters][:values].delete(custom_field_name) + cookie[:groups][:rows].delete(custom_field_name.to_s) + cookie[:groups][:columns].delete(custom_field_name.to_s) + + session[report_engine_name] = cookie + end + end end end end