Sha256: e9504bfb36f007b9d8c44b959efc908e5275f28f48fa8fd5dc7abc739f35efb7

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

class CreatePermissions < ActiveRecord::Migration[6.0]
  def change
    @values = {
      predicates: %i[can cannot],
      actions: %i[manage create read update destroy],
      targets: ApplicationRecord.subclasses.map {|d| d.to_s.underscore}.to_a.unshift(:all)
    }

    def create_and_fill table
      create_table table do |t|
        t.string :name
        t.bigint :lock_version

        t.timestamps
      end
      add_index table, :name, unique: true
      model = table.to_s.classify.constantize
      model.reset_column_information
      model.upsert_all @values[table].map { |p| {name: p, created_at: Time.now, updated_at: Time.now} }, unique_by: [:name]
    end

    # Predicates
    create_and_fill :predicates
    
    # Actions
    create_and_fill :actions
    
    # Targets
    create_and_fill :targets

    create_table :permissions do |t|
      t.references :predicate, null: false, foreign_key: true
      t.references :action, null: false, foreign_key: true
      t.references :target, null: false, foreign_key: true
      t.bigint :lock_version

      t.timestamps
    end
    # Association table
    create_table :permission_roles do |t|
      t.references :role, null: false, foreign_key: true
      t.references :permission, null: false, foreign_key: true
      t.bigint :lock_version

      t.timestamps
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thecore_auth_commons-2.3.1 db/migrate/20200518082821_create_permissions.rb
thecore_auth_commons-2.3.0 db/migrate/20200518082821_create_permissions.rb
thecore_auth_commons-0 db/migrate/20200518082821_create_permissions.rb
thecore_auth_commons-2.2.9 db/migrate/20200518082821_create_permissions.rb
thecore_auth_commons-2.2.8 db/migrate/20200518082821_create_permissions.rb
thecore_auth_commons-2.2.6 db/migrate/20200518082821_create_permissions.rb
thecore_auth_commons-2.2.5 db/migrate/20200518082821_create_permissions.rb