Sha256: 6d9d15137b42f2535bbe5eafb0d2fe6b62af3bd76125b232204f1cdbc5504729

Contents?: true

Size: 1.47 KB

Versions: 57

Compression:

Stored size: 1.47 KB

Contents

require 'model/flag_from_time_range_concern'

module Cmor::Rbac
  class Permission < 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 :role_permissions, class_name: 'Cmor::Rbac::RolePermission', dependent: :destroy
    has_many :roles, class_name: 'Cmor::Rbac::Role', through: :role_permissions
    has_many :user_roles, through: :roles, class_name: 'Cmor::Rbac::UserRole'
    has_many :users, through: :user_roles, class_name: Cmor::Rbac::Configuration.user_class_name

    has_many :enabled_roles, -> { enabled }, class_name: 'Cmor::Rbac::Role', through: :role_permissions, source: :permission
    has_many :enabled_user_roles, class_name: 'Cmor::Rbac::UserRole', through: :enabled_roles, source: :role_permissions

    has_many :enabled_users, through: :enabled_user_roles, class_name: Cmor::Rbac::Configuration.user_class_name, source: :user

    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

57 entries across 57 versions & 1 rubygems

Version Path
cmor_rbac-0.0.17.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.16.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.15.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.14.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.13.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.12.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.11.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.10.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.9.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.8.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.7.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.6.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.5.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.4.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.3.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.2.pre app/models/cmor/rbac/permission.rb
cmor_rbac-0.0.1.pre app/models/cmor/rbac/permission.rb