Sha256: 78d39fe37e036ce49c689fab92e9f71620812a81c6fe671b8b60191d30cdc85d

Contents?: true

Size: 1010 Bytes

Versions: 15

Compression:

Stored size: 1010 Bytes

Contents

class AddManyRolesStrategy < ActiveRecord::Migration
  class << self

    def up          
      create_roles
      create_user_roles
      add_index :roles, :name, :unique => true      
      
      insert_roles
    end

    def down      
      drop_roles
      drop_user_roles
      
      remove_index :roles, :name      
    end

    protected

    def create_user_roles
      create_table :<%= table_name.singularize %>_roles do |t|
        t.integer :<%= table_name.singularize %>_id
        t.integer :role_id
        t.timestamps
      end
    end

    def drop_user_roles
      drop_table :<%= table_name.singularize %>_roles
    end


    def create_roles
      create_table :roles do |t|
        t.string  :name
        t.timestamps
      end
    end

    def drop_roles
      drop_table :roles
    end
    
    def insert_roles
      <%- roles_to_add.each do |role| -%>
        begin
          Role.create(:name => '<%= role %>')
        rescue
        end
      <%- end -%>
    end    
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
roles_active_record-0.4.9 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.8 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.7 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.6.4 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.6.3 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.6.2 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.6.1 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.6 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.5 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.4 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.3 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.2 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.1 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.4.0 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
roles_active_record-0.3.6 lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb