Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0a3bc8479f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 83 lines (65 sloc) 2.249 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
class CreateSeoMeta < ActiveRecord::Migration

  def self.up
    create_table :seo_meta do |t|
      t.integer :seo_meta_id
      t.string :seo_meta_type

      t.string :browser_title
      t.string :meta_keywords
      t.text :meta_description

      t.timestamps
    end

    add_index :seo_meta, :id
    add_index :seo_meta, [:seo_meta_id, :seo_meta_type]

    # Grab the attributes of the records that currently exist
    existing_translations = ::Page::Translation.all.map(&:attributes)

    # Remove columns
    ::SeoMeta.attributes.keys.each do |field|
      if ::Page::Translation.column_names.map(&:to_sym).include?(field)
        remove_column ::Page::Translation.table_name, field
      end
    end

    # Reset column information because otherwise the old columns will still exist.
    ::Page::Translation.reset_column_information

    # Re-attach seo_meta
    ::Page::Translation.module_eval do
      is_seo_meta
    end

    # Migrate data
    existing_translations.each do |translation|
      ::Page::Translation.find(translation['id']).update_attributes(
        ::SeoMeta.attributes.keys.inject({}) {|attributes, name|
          attributes.merge(name => translation[name.to_s])
        }
      )
    end
  end

  def self.down
    # Grab the attributes of the records that currently exist
    existing_translations = ::Page::Translation.all.map(&:attributes)

    # Add columns back to your model
    ::SeoMeta.attributes.each do |field, field_type|
      unless ::Page::Translation.column_names.map(&:to_sym).include?(field)
        add_column ::Page::Translation.table_name, field, field_type
      end
    end

    # Reset column information because otherwise the new columns won't exist yet.
    ::Page::Translation.reset_column_information

    # Migrate data
    existing_translations.each do |translation|
      ::Page::Translation.update_all(
        ::SeoMeta.attributes.keys.inject({}) {|attributes, name|
          attributes.merge(name => translation[name.to_s])
        }, :id => translation['id']
      )
    end

    ::SeoMeta.attributes.keys.each do |k|
      ::Page::Translation.module_eval %{
def #{k}
end

def #{k}=(*args)
end
}
    end

    drop_table :seo_meta
  end

end
Something went wrong with that request. Please try again.