]> source.dussan.org Git - redmine.git/commitdiff
Fixes tracker_id and custom_field_values assignment order for issues (#4353).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 9 Dec 2009 09:12:29 +0000 (09:12 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 9 Dec 2009 09:12:29 +0000 (09:12 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3139 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
test/unit/issue_test.rb

index e98369d060cff51e3899c78e5caac9e0e9bc4994..a0f47fdb866befce14a23385534950d2095b1bb6 100644 (file)
@@ -142,8 +142,22 @@ class Issue < ActiveRecord::Base
   def tracker_id=(tid)
     self.tracker = nil
     write_attribute(:tracker_id, tid)
+    result = write_attribute(:tracker_id, tid)
+    @custom_field_values = nil
+    result
   end
   
+  # Overrides attributes= so that tracker_id gets assigned first
+  def attributes_with_tracker_first=(new_attributes, *args)
+    return if new_attributes.nil?
+    new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id]
+    if new_tracker_id
+      self.tracker_id = new_tracker_id
+    end
+    self.attributes_without_tracker_first = new_attributes, *args
+  end
+  alias_method_chain :attributes=, :tracker_first
+  
   def estimated_hours=(h)
     write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
   end
index dd732f97f5c4bf0b45594c35a23794e17860ac7c..e91880c08dc1f0487f91bbee1d0ad7725cf234b9 100644 (file)
@@ -164,6 +164,23 @@ class IssueTest < ActiveSupport::TestCase
     assert_equal custom_value.id, issue.custom_value_for(field).id
   end
   
+  def test_assigning_tracker_id_should_reload_custom_fields_values
+    issue = Issue.new(:project => Project.find(1))
+    assert issue.custom_field_values.empty?
+    issue.tracker_id = 1
+    assert issue.custom_field_values.any?
+  end
+  
+  def test_assigning_attributes_should_assign_tracker_id_first
+    attributes = ActiveSupport::OrderedHash.new
+    attributes['custom_field_values'] = { '1' => 'MySQL' }
+    attributes['tracker_id'] = '1'
+    issue = Issue.new(:project => Project.find(1))
+    issue.attributes = attributes
+    assert_not_nil issue.custom_value_for(1)
+    assert_equal 'MySQL', issue.custom_value_for(1).value
+  end
+  
   def test_should_update_issue_with_disabled_tracker
     p = Project.find(1)
     issue = Issue.find(1)