Sha256: eb674a1088be9b2bd228ec3c15ac77319e1f7b1e45d79658abfe8ced03177865

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Authz
  class ScopingRule < ApplicationRecord
    # Validations
    # ==========================================================================
    validates :scopable, presence: true
    validate :scopable_exists
    validates_uniqueness_of :scopable, scope: [:authz_role_id]
    validates :role, presence: true
    validates :keyword, presence: true
    validate :valid_keyword_for_scopable

    # Associations
    # ==========================================================================
    belongs_to :role, class_name: 'Authz::Role',
                      foreign_key: 'authz_role_id'
    has_many :role_grants, through: :role

    scope :for_scopables, ->(scopables) { where(scopable: scopables.map(&:to_s)) }

    def to_s
      "#{scopable}: #{keyword}##{id}"
    end

    private

    def scopable_exists
      unless scopable_exists?(scopable)
        errors.add(:scopable, "#{scopable} does not exists.")
      end
    end

    def valid_keyword_for_scopable
      if  scopable_exists?(scopable) && !scopable.constantize.valid_keyword?(keyword)
        errors.add(:keyword, "#{keyword} is not a valid keyword for #{scopable}")
      end
    end

    # Used to reduce the impact of the external dependency Scopable::Base
    def scopable_exists?(scopable)
      Scopables::Base.scopable_exists?(scopable)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
authz-0.0.1.alpha4 app/models/authz/scoping_rule.rb
authz-0.0.1.alpha3 app/models/authz/scoping_rule.rb
authz-0.0.1.alpha2 app/models/authz/scoping_rule.rb
authz-0.0.1.alpha app/models/authz/scoping_rule.rb