Sha256: 7a9c80ca51166b9477a7400b2e86e7d0d44568b942b13ba8622a927b52bcabb1

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'model/flag_from_time_range_concern'

module Ecm::Rbac
  class Role < ActiveRecord::Base
    include Model::FlagFromTimeRangeConcern

    DEFAULTS = {
      enabled_from: '01.01.1900 00:00:00',
      enabled_to:   '31.12.9999 23:59:59'
    }

    # associations
    has_many :user_roles, class_name:  'Ecm::Rbac::UserRole', inverse_of: :role
    has_many :users, through: :user_roles

    has_many :role_permissions, class_name: 'Ecm::Rbac::RolePermission'
    has_many :permissions, class_name: 'Ecm::Rbac::Permission', through: :role_permissions

    has_many :enabled_permissions, -> { enabled }, class_name: 'Ecm::Rbac::Permission', through: :role_permissions, source: :role

    flag_from_time_range :enabled?

    # validations
    validates :identifier, presence: true, uniqueness: true
    validates :enabled_from, presence: true
    validates :enabled_to, presence: true

    # callbacks
    after_initialize :set_defaults

    def human
      "#{self.class.model_name.human}: #{identifier}"
    end

    private

    def set_defaults
      return if persisted?
      self.enabled_from = DEFAULTS[:enabled_from]
      self.enabled_to   = DEFAULTS[:enabled_to]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecm_rbac-2.0.0 app/models/ecm/rbac/role.rb
ecm_rbac-1.2.1 app/models/ecm/rbac/role.rb
ecm_rbac-1.2.0 app/models/ecm/rbac/role.rb
ecm_rbac-1.0.1 app/models/ecm/rbac/role.rb