Sha256: 0bc7af72f51e270e83707572b994a6069b5ffb4ad684e06e0ce0a65fc666d114

Contents?: true

Size: 948 Bytes

Versions: 18

Compression:

Stored size: 948 Bytes

Contents

class AddOneRoleStrategy < ActiveRecord::Migration
  class << self

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

    def down      
      drop_roles
      remove_user_role
      
      remove_index :roles, :name
    end

    protected

    def add_user_role
      change_table :users do |t|
        t.integer :role_id
      end
    end

    def remove_user_role
      change_table :users do |t|
        t.remove :role_id
      end
    end

    def create_roles
      create_table :roles do |t|
        t.string  :name, :null => false
        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}") # if !Role.where(:name => name.to_s).first
        rescue
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

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