From 7697861dc6a85beed3cce2b2170daac34a47a528 Mon Sep 17 00:00:00 2001 From: hjust Date: Mon, 22 Feb 2010 18:48:35 +0000 Subject: [PATCH] Update cost objects on issue move (depends on http://www.redmine.org/projects/redmine/repository/revisions/3456 and http://www.redmine.org/projects/redmine/repository/revisions/3457) git-svn-id: https://dev.finn.de/svn/cockpit/trunk@520 7926756e-e54e-46e6-9721-ed318f58905e --- app/models/cost_query.rb | 32 +++++++++++++++++++++++++++++--- lib/costs_issue_hook.rb | 24 ++++++++++++++++++++++++ lib/costs_issue_patch.rb | 21 ++++++++++++--------- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/app/models/cost_query.rb b/app/models/cost_query.rb index b5a20b0970..70719abcd0 100644 --- a/app/models/cost_query.rb +++ b/app/models/cost_query.rb @@ -103,6 +103,28 @@ class CostQuery < ActiveRecord::Base self.group_by ||= {} end + def display_time_entries +# read_attribute("display_time_entries") && ( + !self.filters || !self.filters.any? do |f| + f["enabled"] == "1" && f["scope"] == "costs" && f["column_name"] == "cost_type_id" && ( + (f["operator"] == "!" && f["values"].include?("")) || + (f["operator"] == "=" && !f["values"].include?("")) + ) + end +# ) + end + + def display_cost_entries +# read_attribute("display_cost_entries") && ( + !self.filters || !self.filters.any? do |f| + f["enabled"] == "1" && f["scope"] == "costs" && f["column_name"] == "cost_type_id" && ( + (f["operator"] == "=" && f["values"].all? {|v| v == ""}) + ) + end +# ) + end + + def self.operators # These are the operators used by filter types. @@ -157,7 +179,7 @@ class CostQuery < ActiveRecord::Base @available_filters = { :costs => { - "cost_type_id" => { :type => :list_optional, :order => 2, :applies => [:cost_entries], :flags => [], :db_table => CostType.table_name, :db_field => "id", :values => CostType.find(:all, :order => 'name').collect{|s| [s.name, s.id.to_s] }}, + "cost_type_id" => { :type => :list, :order => 2, :applies => [:cost_entries], :flags => [], :db_table => CostType.table_name, :db_field => "id", :values => [[l(:field_labor_costs), ""]] + CostType.find(:all, :order => 'name').collect{|s| [s.name, s.id.to_s] }}, "activity_id" => { :type => :list_optional, :order => 3, :applies => [:time_entries], :flags => [], :db_table => TimeEntryActivity.table_name, :db_field => "id", :values => TimeEntryActivity.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] }}, "created_on" => { :type => :date_exact, :applies => [:time_entries, :cost_entries], :flags => [], :order => 4 }, "updated_on" => { :type => :date_exact, :applies => [:time_entries, :cost_entries], :flags => [], :order => 5 }, @@ -231,6 +253,10 @@ class CostQuery < ActiveRecord::Base return match.blank? ? nil : match[0] end + def clean_filters + + end + MAGIC_GROUP_KEYS = [:block, :time, :display, :db_field, :other_group] def self.grouping_column(*names, &block) @@ -645,9 +671,9 @@ private sql = '' case operator when "=" - sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" unless value.blank? + sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| val.present? ? "'#{connection.quote_string(val)}'" : "NULL"}.join(",") + ")" unless value.blank? when "!" - sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" + sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| val.present? ? "'#{connection.quote_string(val)}'" : "NULL"}.join(",") + "))" when "!*" sql = "#{db_table}.#{db_field} IS NULL" sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter diff --git a/lib/costs_issue_hook.rb b/lib/costs_issue_hook.rb index 036fadadbd..7a1917eef2 100644 --- a/lib/costs_issue_hook.rb +++ b/lib/costs_issue_hook.rb @@ -12,8 +12,32 @@ class CostsIssueHook < Redmine::Hook::ViewListener # Renders a select tag with all the Cost Objects for the bulk edit page render_on :view_issues_bulk_edit_details_bottom, :partial => 'hooks/view_issues_bulk_edit_details_bottom' + render_on :view_issues_move_bottom, :partial => 'hooks/view_issues_move_bottom' + # Updates the cost object after a move + # + # Context: + # * params => Request parameters + # * issue => Issue to move + # * target_project => Target of the move + # * copy => true, if the issues are copied rather than moved + def controller_issues_move_before_save(context={}) + # FIXME: In case of copy==true, this will break stuff if the original issue is saved + + case params[:cost_object_id] + when "" # a.k.a "(No change)" + # cost objects HAVE to be changed if move is performed across project boundaries + # as the are project specific + issue.cost_object_id = nil unless issue.project == context[:target_project] + when "none" + issue.cost_object_id = nil + else + issue.cost_object_id = params[:cost_object_id] + end + end + + # Renders a select tag with all the Cost Objects for the bulk edit page # # Context: diff --git a/lib/costs_issue_patch.rb b/lib/costs_issue_patch.rb index 35c4c51230..02044553c5 100644 --- a/lib/costs_issue_patch.rb +++ b/lib/costs_issue_patch.rb @@ -14,8 +14,8 @@ module CostsIssuePatch has_many :cost_entries, :dependent => :delete_all # disabled for now, implements part of ticket blocking - #alias_method_chain :validate, :cost_object - + alias_method_chain :validate, :cost_object + def spent_hours # overwritten method @spent_hours ||= self.time_entries.visible(User.current).sum(:hours) || 0 @@ -30,13 +30,16 @@ module CostsIssuePatch module InstanceMethods def validate_with_cost_object if cost_object_id_changed? - if cost_object_id_was.nil? - # formerly unassigned ticket - errors.add :cost_object_id, :activerecord_error_invalid if cost_object.blocked? - else - old_cost_object = CostObject.find(cost_object_id_was) - errors.add :cost_object_id, :activerecord_error_invalid if old_cost_object.blocked? - end + errors.add :cost_object_id, :activerecord_error_invalid unless project.cost_object_ids.include? cost_object_id + + ## disabled for now, implements part of ticket blocking + # if cost_object_id_was.nil? + # # formerly unassigned ticket + # errors.add :cost_object_id, :activerecord_error_invalid if cost_object.blocked? + # else + # old_cost_object = CostObject.find(cost_object_id_was) + # errors.add :cost_object_id, :activerecord_error_invalid if old_cost_object.blocked? + # end end validate_without_cost_object