Sha256: fe74591af61f4fd412f93cdcd75f6bc025617491c7d544f7c355b9322677c6e8

Contents?: true

Size: 1.17 KB

Versions: 57

Compression:

Stored size: 1.17 KB

Contents

require 'model/flag_from_time_range_concern'

module Cmor::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:  'Cmor::Rbac::UserRole', inverse_of: :role
    has_many :users, through: :user_roles

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

    has_many :enabled_permissions, -> { enabled }, class_name: 'Cmor::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

57 entries across 57 versions & 1 rubygems

Version Path
cmor_rbac-0.0.60.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.59.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.58.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.57.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.56.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.55.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.54.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.53.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.52.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.51.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.50.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.49.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.48.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.45.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.44.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.43.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.42.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.41.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.40.pre app/models/cmor/rbac/role.rb
cmor_rbac-0.0.39.pre app/models/cmor/rbac/role.rb