Skip to content

Defining rules for unloaded dynamic class and subtarget

Adam Pahlevi Baihaqi edited this page Mar 1, 2016 · 1 revision

Bali allows you to pass in a String, rather than the class name. It makes it possible to define rules for yet unloaded class, or class that may get created on the fly.

Bali.map_rules do
  roles_for User, :roles
  rules_for 'BankAccount' do
    role(Role::USER_ULTIMATE) { can_all }
    role(Role::USER_BASIC, Role::USER_ADMIN) do
      can :view, :create
    end
    role(Role::USER_ADMIN) do
      can :create, :update, :delete
    end
    role(Role::USER_BASIC) do
      can :create, :update, if: proc { |record, user| record.owned_by?(user) }
    end
  end
end

Similarly, roles_for can as well accept a String, instead of a constant, for the intended subtarget class:

Bali.map_rules do
  roles_for 'User', :roles
  rules_for BankAccount do
    role(Role::USER_ULTIMATE) { can_all }

  end
end