20100714111654_add_changes_from_journal_details_for_acts_as_journalized.rb 1.63 KB
Newer Older
1
#-- encoding: UTF-8
Holger Just's avatar
Holger Just committed
2 3 4
#-- copyright
# ChiliProject is a project management system.
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2012 the ChiliProject Team
Holger Just's avatar
Holger Just committed
6 7 8 9 10 11 12 13 14
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# See doc/COPYRIGHT.rdoc for more details.
#++

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class AddChangesFromJournalDetailsForActsAsJournalized < ActiveRecord::Migration
  def self.up
    # This is provided here for migrating up after the JournalDetails has been removed
    unless Object.const_defined?("JournalDetails")
      Object.const_set("JournalDetails", Class.new(ActiveRecord::Base))
    end

    say_with_time("Adding changes from JournalDetails") do
      JournalDetails.all.each do |detail|
        journal = Journal.find(detail.journal_id)
        changes = journal.changes || {}
        if detail.property == 'attr' # Standard attributes
          changes[detail.prop_key.to_s] = [detail.old_value, detail.value]
        elsif detail.property == 'cf' # Custom fields
          changes["custom_values_" + detail.prop_key.to_s] = [detail.old_value, detail.value]
        elsif detail.property == 'attachment' # Attachment
          changes["attachments_" + detail.prop_key.to_s] = [detail.old_value, detail.value]
        end
33 34 35 36 37
        begin
          journal.update_attribute(:changes, changes.to_yaml)
        rescue ActiveRecord::RecordInvalid => ex
          puts "Error saving: #{journal.class.to_s}##{journal.id} - #{ex.message}"
        end
Holger Just's avatar
Holger Just committed
38

39 40 41
      end

    end
Holger Just's avatar
Holger Just committed
42

43 44 45 46 47
  end

  def self.down
    # No-op
  end
Holger Just's avatar
Holger Just committed
48

49
end