Sha256: 15c1dd5891024fc4e86251edbe5095ffeb45f8caf6807f592ebbd722d3b7a4ac

Contents?: true

Size: 855 Bytes

Versions: 2

Compression:

Stored size: 855 Bytes

Contents

# encoding: utf-8

class UpdatePageRedirectTo < ActiveRecord::Migration
  def self.up
    include Rails.application.routes.url_helpers

    pages = Page.where('redirect_to IS NOT NULL AND redirect_to != ""')
    pages.each do |page|
      if page.redirect_to == "0" || page.redirect_to == "--- \"\"\n" || page.redirect_to == "--- ''\n"
        page.update(redirect_to: nil)
      elsif page.redirect_to.start_with?("---")
        options = YAML.load(page.redirect_to)
        path = url_for(options.merge(locale: ":locale", only_path: true))
        page.update(redirect_to: path)
      elsif !(page.redirect_to =~ /\A(\/|https?:\/\/)/)
        fail "Not a valid Page redirect_to: #{page.redirect_to.inspect}"
      end
    end
    change_column :pages, :redirect_to, :string
  end

  def self.down
    change_column :pages, :redirect_to, :text
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_core-3.4.3 db/migrate/20130823133208_update_page_redirect_to.rb
pages_core-3.4.2 db/migrate/20130823133208_update_page_redirect_to.rb