Sha256: 122fb39051c07b5cfd122ec4ad550758dcc4d21455c304bf95db3fad4d2ea9c4

Contents?: true

Size: 1018 Bytes

Versions: 6

Compression:

Stored size: 1018 Bytes

Contents

class AddFieldGroupsKlassName < ActiveRecord::Migration
  def up
    add_column :field_groups, :klass_name, :string, limit: 32

    # Add a default field group for each model
    %w(Account Campaign Contact Lead Opportunity).each do |entity|
      klass = entity.classify.constantize
      field_group = FieldGroup.new
      field_group.label, field_group.klass_name = 'Custom Fields', klass.name
      field_group.save!
      Field.where(field_group_id: nil, klass_name: klass.name).update_all(field_group_id: field_group.id)
    end
    FieldGroup.where(klass_name: nil).update_all('klass_name = (SELECT MAX(klass_name) FROM fields WHERE field_group_id = field_groups.id)')

    remove_column :fields, :klass_name
    Field.reset_column_information
  end

  def down
    add_column :fields, :klass_name, :string, limit: 32
    connection.execute 'UPDATE fields SET klass_name = (SELECT MAX(klass_name) FROM field_groups WHERE field_groups.id = field_group_id)'
    remove_column :field_groups, :klass_name
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
fat_free_crm-0.14.2 db/migrate/20111201030535_add_field_groups_klass_name.rb
fat_free_crm-0.14.1 db/migrate/20111201030535_add_field_groups_klass_name.rb
fat_free_crm-0.15.0.beta db/migrate/20111201030535_add_field_groups_klass_name.rb
fat_free_crm-0.14.0 db/migrate/20111201030535_add_field_groups_klass_name.rb
reduced_fat_crm-0.15.0.beta db/migrate/20111201030535_add_field_groups_klass_name.rb
reduced_fat_crm-0.14.0 db/migrate/20111201030535_add_field_groups_klass_name.rb