Sha256: 78f677cc43c8af7e1125067c5143b2d134435b41e6f9889cca6c1d3ca3bdc055

Contents?: true

Size: 917 Bytes

Versions: 17

Compression:

Stored size: 917 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 :user_roles do |t|
        t.integer :user_id
        t.integer :role_id
        t.timestamps
      end
    end

    def drop_user_roles
      drop_table :user_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

17 entries across 17 versions & 1 rubygems

Version Path
roles_active_record-0.5.1 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.5.0 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.9 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.8 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.7 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.6.4 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.6.3 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.6.2 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.6.1 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.6 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.5 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.4 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.3 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.2 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.1 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.4.0 spec/migrations/many_roles/002_add_many_roles_strategy.rb
roles_active_record-0.3.6 spec/migrations/many_roles/002_add_many_roles_strategy.rb