Sha256: 2636f229b904570df35c95b8a43e4af1b5ee84cb494bd257f7d22bfcb1131443

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# This is mostly how the attachments looked at the time of the migration. They
# have later changed names, which would cause errors when running this
# migration. Hopefully, this is enough to run this through properly.
module KnowledgeBase::Sectionables
  class Attachment < Sectionable
    belongs_to :attachment_list
  end

  class AttachmentList < Sectionable
    has_many :sections, as: :sectionable, dependent: :destroy

    has_many :attachments, dependent: :destroy
    accepts_nested_attributes_for :attachments, allow_destroy: true
  end
end

class MigrateAttachmentsOverToLists < ActiveRecord::Migration
  def up
    KnowledgeBase::Sectionables::Attachment.all.each do |attachment|
      sections = KnowledgeBase::Section
        .where(sectionable_type: 'KnowledgeBase::Sectionables::Attachment')
        .where(sectionable_id: attachment.id)

      list = KnowledgeBase::Sectionables::AttachmentList.new
      list.attachments << attachment
      list.sections = sections

      list.save!
    end
  rescue StandardError
    # Do nothing
  end

  def down
    KnowledgeBase::Sectionables::AttachmentList.all.each do |list|
      sections = KnowledgeBase::Section
        .where(sectionable_type: 'KnowledgeBase::Sectionables::AttachmentList')
        .where(sectionable_id: list.id)

      list.attachments.all.each do |attachment|
        attachment.sections = sections

        attachment.save!
      end
    end
  rescue StandardError
    # Do nothing
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
knowledge_base-0.2.0 db/migrate/20140410191611_migrate_attachments_over_to_lists.rb