Sha256: d57a1a35489cfc2586cf27ccee32b494bc0f159f6738fb28c811cf58e0047d7a

Contents?: true

Size: 1.07 KB

Versions: 103

Compression:

Stored size: 1.07 KB

Contents

class ChangeContactDetails < ActiveRecord::Migration[4.2]
  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

103 entries across 103 versions & 1 rubygems

Version Path
blacklight-spotlight-3.0.3 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.2 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.1 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.rc6 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.rc5 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.rc4 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.rc3 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.rc2 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.rc1 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.10 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.9 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.8 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.7 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.6 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.5 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.4 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.3 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.2 db/migrate/20141118233735_change_contact_details.rb
blacklight-spotlight-3.0.0.alpha.1 db/migrate/20141118233735_change_contact_details.rb