class Object

Public Instance Methods

change() click to toggle source
# File lib/generators/simple_localizer/templates/create_translations.rb, line 5
def change
  create_table <%= table_name -%> do |t|
    t.string :locale, :null => false
    t.integer <%=  foreign_key -%>
    <% fields.each_pair do |column, type| %>
    t.<%= type %> <%= ":#{column}" -%>
    <% end %>

    t.timestamps
  end

  <%- random_string = proc { (0...20).map{ ('a'..'z').to_a[rand(26)] }.join } -%>
  <%- options = ActiveRecord::Migration.index_name(table_name, foreign_key).size > 64 ? { :name => random_string.call } : {} -%>
  add_index <%= table_name -%>, <%= foreign_key %>, <%= p(options) %>
  <%- options = ActiveRecord::Migration.index_name(table_name, :locale).size > 64 ? { :name => random_string.call } : {} -%>
  add_index <%= table_name -%>, :locale, <%= p(options) %>
  <%- options = ActiveRecord::Migration.index_name(table_name, [:locale, foreign_key]).size > 64 ? { :name => random_string.call } : {} -%>
  add_index <%= table_name -%>, [:locale, <%= foreign_key -%>], <%= p(options.merge(:unique => true)) %>
end