Sha256: 824bca28e8492d53d30bd0fddf5b883fbde9d8685e0b00db3b267a0e268369a7

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

# This migration comes from spotlight (originally 20141118233735)
class ChangeContactDetails < ActiveRecord::Migration
  def up
    add_column :spotlight_contacts, :contact_info, :text

    Spotlight::Contact.find_each do |contact|
      migrated_contact_info = {}
      attributes.each do |attribute|
        if (value = contact.send(attribute)).present?
          migrated_contact_info[attribute] = value
        end
      end
      contact.contact_info = migrated_contact_info
      contact.save!
    end

    attributes.each do |col|
      remove_column :spotlight_contacts, col, :string if Spotlight::Contact.column_names.include? col
    end
  end
  def down
    attributes.each do |attribute|
      add_column :spotlight_contacts, col, :string
    end

    Spotlight::Contact.find_each do |contact|
      attributes.each do |attribute|
        if (value = contact.contact_info[attribute]).present?
          contact.send("#{attribute}=".to_sym, value)
        end
      end
      contact.save!
    end

    remove_column :spotlight_contacts, :contact_info, :text
  end
  private
  def attributes
    [:email, :title, :location, :telephone]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blacklight-spotlight-0.3.1 spec/internal/db/migrate/20150303163375_change_contact_details.spotlight.rb
blacklight-spotlight-0.3.0 spec/internal/db/migrate/20150302231336_change_contact_details.spotlight.rb
blacklight-spotlight-0.2.0 spec/internal/db/migrate/20150120185734_change_contact_details.spotlight.rb