Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[#3264] Map associations in the JournalDetail properties.
  • Loading branch information
edavis10 committed Nov 12, 2009
1 parent a405715 commit 10c3a37
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
52 changes: 52 additions & 0 deletions app/models/redmine_merge.rb
Expand Up @@ -35,5 +35,57 @@ def self.add_journal(source_id, new_id)
def self.get_new_journal_id(source_id)
Journals[source_id]
end

def self.find_id_by_property(target_klass, source_id)
# Similar to issues_helper.rb#show_detail
source_id = source_id.to_i

case target_klass.to_s
when 'Project'
return Mapper.get_new_journal_id(source_id)
when 'IssueStatus'
target = find_target_record_from_source(SourceIssueStatus, IssueStatus, :name, source_id)
return target.id if target
return nil
when 'Tracker'
target = find_target_record_from_source(SourceTracker, Tracker, :name, source_id)
return target.id if target
return nil
when 'User'
target = find_target_record_from_source(SourceUser, User, :login, source_id)
return target.id if target
return nil
when 'Enumeration'
target = find_target_record_from_source(SourceEnumeration, Enumeration, :name, source_id)
return target.id if target
return nil
when 'IssueCategory'
source = SourceIssueCategory.find(source_id)
return nil unless source
target = IssueCategory.find_by_name_and_project_id(source.name, RedmineMerge::Mapper.get_new_project_id(source.project_id))
return target.id if target
return nil
when 'Version'
source = SourceVersion.find(source_id)
return nil unless source
target = Version.find_by_name_and_project_id(source.name, RedmineMerge::Mapper.get_new_project_id(source.project_id))
return target.id if target
return nil
end

end

private

# Utility method to dynamically find the target records
def self.find_target_record_from_source(source_klass, target_klass, field, source_id)
source = source_klass.find(source_id)
field = field.to_sym
if source
return target_klass.find(:first, :conditions => {field => source.read_attribute(field) })
else
return nil
end
end
end
end
12 changes: 12 additions & 0 deletions app/models/source_journal_detail.rb
Expand Up @@ -10,6 +10,18 @@ def self.migrate
jd = JournalDetail.new
jd.attributes = source_journal_detail.attributes
jd.journal = Journal.find(RedmineMerge::Mapper.get_new_journal_id(source_journal_detail.journal_id))

# Need to remap propery keys to their new ids
if source_journal_detail.prop_key.include?('_id')
property_name = source_journal_detail.prop_key.to_s.gsub(/\_id$/, "").to_sym
association = Issue.reflect_on_all_associations.detect {|a| a.name == property_name }

if association
jd.old_value = RedmineMerge::Mapper.find_id_by_property(association.klass, source_journal_detail.old_value)
jd.value = RedmineMerge::Mapper.find_id_by_property(association.klass, source_journal_detail.value)
end
end

jd.save!

end
Expand Down
27 changes: 20 additions & 7 deletions test/unit/source_journal_detail_test.rb
Expand Up @@ -5,14 +5,16 @@ class SourceJournalDetailTest < Test::Unit::TestCase
setup do
User.anonymous # preload

# Make sure Journals are associating correctly and not that they
# just happen to match ids.
# Make sure objects associating correctly and not that they just
# happen to match ids.
@project = Project.generate!
@tracker = Tracker.generate!
@project.trackers << @tracker
@enumeration = Enumeration.generate!(:opt => 'IPRI')
@issue = Issue.generate!(:tracker => @tracker, :project => @project, :priority => @enumeration)
Journal.generate!(:issue => @issue)
IssueStatus.generate!(:name => 'New')
IssueStatus.generate!(:name => 'Closed')

SourceUser.migrate
SourceTracker.migrate
Expand All @@ -38,11 +40,22 @@ class SourceJournalDetailTest < Test::Unit::TestCase
journal = issue.journals.find_by_notes("Journal notes")
assert journal
assert_equal 2, journal.details.count
journal.details.each do |detail|
assert detail.prop_key == "status_id" || detail.prop_key == "done_ratio"
assert detail.old_value == '1' || detail.old_value == '40'
assert detail.value = '2' || detail.value == '30'
end
end

should "remap the property associations" do
SourceJournalDetail.migrate

issue = Issue.find_by_subject("Can't print recipes")
assert issue

journal = issue.journals.find_by_notes("Journal notes")
assert journal

detail = journal.details.find_by_prop_key('status_id')
assert detail

assert_equal IssueStatus.find_by_name('New').id, detail.old_value.to_i
assert_equal IssueStatus.find_by_name('Assigned').id, detail.value.to_i
end

end
Expand Down

0 comments on commit 10c3a37

Please sign in to comment.