Class: PermissionGroup

Inherits:
ApplicationRecord show all
Defined in:
app/models/permission_group.rb

Overview

The PermissionGroup model represents a group of permissions in the system. It is used to manage and organize permissions for different roles and users.

Defined Under Namespace

Classes: Codes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Instance Attribute Details

#codeString

Returns the unique code for the permission group.

Returns:

  • (String)

    the unique code for the permission group



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/permission_group.rb', line 12

class PermissionGroup < ApplicationRecord
  # Constants for permission group codes
  class Codes
    PAYROLL_ADMIN = 'PAYROLL-ADMIN'
    EMPLOYEE_PORTAL = 'EMPLOYEE-PORTAL'
    EDI = 'EDI'
    AI_PLATFORM = 'AI-PLATFORM'
  end

  self.table_name = 'PERMISSION_GROUP_HEADER'
  self.primary_key = 'permission_group_header_id'

  class << self
    # Retrieves a list of permission group options.
    #
    # @return [ActiveRecord::Relation] a relation of permission groups with selected attributes
    # @example
    #   PermissionGroup.options
    def options
      select(:permission_group_header_id, :code, :description)
        .order(code: :asc)
    end

    # Finds the payroll admin permission group.
    #
    # @return [PermissionGroup] the payroll admin permission group
    # @raise [ActiveRecord::RecordNotFound] if the payroll admin permission group is not found
    # @example
    #   PermissionGroup.payroll_admin
    def payroll_admin
      find_by!(code: Codes::PAYROLL_ADMIN)
    end

    # Finds the employee portal permission group.
    #
    # @return [PermissionGroup] the employee portal permission group
    # @raise [ActiveRecord::RecordNotFound] if the employee portal permission group is not found
    # @example
    #  PermissionGroup.employee_portal
    def employee_portal
      find_by!(code: Codes::EMPLOYEE_PORTAL)
    end
  end

  # Associations
  has_and_belongs_to_many(
    :permissions,
    join_table: PermissionGroupDetails.table_name,
    foreign_key: primary_key,
    association_foreign_key: Permission.primary_key
  )

  has_many(
    :permission_group_details,
    foreign_key: 'permission_group_header_id',
    class_name: 'PermissionGroupDetails',
    dependent: :destroy
  )

  has_many :user_permission_groups,
           foreign_key: 'permission_group_header_id'

  has_many :erp_users,
           through: :user_permission_groups

  # Validations
  validates :code, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
  validates :description, presence: true, length: { minimum: 1, maximum: 255 }

  # Retrieves the IDs of active permissions in the permission group.
  #
  # @return [Array<Integer>] an array of active permission IDs
  # @example
  #   permission_group.active_permission_ids
  def active_permission_ids
    permission_group_details.includes(:permission).where(active: true).map(&:permission).map(&:permission_id)
  end
end

#descriptionString

Returns the description of the permission group.

Returns:

  • (String)

    the description of the permission group



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/permission_group.rb', line 12

class PermissionGroup < ApplicationRecord
  # Constants for permission group codes
  class Codes
    PAYROLL_ADMIN = 'PAYROLL-ADMIN'
    EMPLOYEE_PORTAL = 'EMPLOYEE-PORTAL'
    EDI = 'EDI'
    AI_PLATFORM = 'AI-PLATFORM'
  end

  self.table_name = 'PERMISSION_GROUP_HEADER'
  self.primary_key = 'permission_group_header_id'

  class << self
    # Retrieves a list of permission group options.
    #
    # @return [ActiveRecord::Relation] a relation of permission groups with selected attributes
    # @example
    #   PermissionGroup.options
    def options
      select(:permission_group_header_id, :code, :description)
        .order(code: :asc)
    end

    # Finds the payroll admin permission group.
    #
    # @return [PermissionGroup] the payroll admin permission group
    # @raise [ActiveRecord::RecordNotFound] if the payroll admin permission group is not found
    # @example
    #   PermissionGroup.payroll_admin
    def payroll_admin
      find_by!(code: Codes::PAYROLL_ADMIN)
    end

    # Finds the employee portal permission group.
    #
    # @return [PermissionGroup] the employee portal permission group
    # @raise [ActiveRecord::RecordNotFound] if the employee portal permission group is not found
    # @example
    #  PermissionGroup.employee_portal
    def employee_portal
      find_by!(code: Codes::EMPLOYEE_PORTAL)
    end
  end

  # Associations
  has_and_belongs_to_many(
    :permissions,
    join_table: PermissionGroupDetails.table_name,
    foreign_key: primary_key,
    association_foreign_key: Permission.primary_key
  )

  has_many(
    :permission_group_details,
    foreign_key: 'permission_group_header_id',
    class_name: 'PermissionGroupDetails',
    dependent: :destroy
  )

  has_many :user_permission_groups,
           foreign_key: 'permission_group_header_id'

  has_many :erp_users,
           through: :user_permission_groups

  # Validations
  validates :code, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
  validates :description, presence: true, length: { minimum: 1, maximum: 255 }

  # Retrieves the IDs of active permissions in the permission group.
  #
  # @return [Array<Integer>] an array of active permission IDs
  # @example
  #   permission_group.active_permission_ids
  def active_permission_ids
    permission_group_details.includes(:permission).where(active: true).map(&:permission).map(&:permission_id)
  end
end

#permission_group_header_idInteger

Returns the primary key of the permission group.

Returns:

  • (Integer)

    the primary key of the permission group



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/permission_group.rb', line 12

class PermissionGroup < ApplicationRecord
  # Constants for permission group codes
  class Codes
    PAYROLL_ADMIN = 'PAYROLL-ADMIN'
    EMPLOYEE_PORTAL = 'EMPLOYEE-PORTAL'
    EDI = 'EDI'
    AI_PLATFORM = 'AI-PLATFORM'
  end

  self.table_name = 'PERMISSION_GROUP_HEADER'
  self.primary_key = 'permission_group_header_id'

  class << self
    # Retrieves a list of permission group options.
    #
    # @return [ActiveRecord::Relation] a relation of permission groups with selected attributes
    # @example
    #   PermissionGroup.options
    def options
      select(:permission_group_header_id, :code, :description)
        .order(code: :asc)
    end

    # Finds the payroll admin permission group.
    #
    # @return [PermissionGroup] the payroll admin permission group
    # @raise [ActiveRecord::RecordNotFound] if the payroll admin permission group is not found
    # @example
    #   PermissionGroup.payroll_admin
    def payroll_admin
      find_by!(code: Codes::PAYROLL_ADMIN)
    end

    # Finds the employee portal permission group.
    #
    # @return [PermissionGroup] the employee portal permission group
    # @raise [ActiveRecord::RecordNotFound] if the employee portal permission group is not found
    # @example
    #  PermissionGroup.employee_portal
    def employee_portal
      find_by!(code: Codes::EMPLOYEE_PORTAL)
    end
  end

  # Associations
  has_and_belongs_to_many(
    :permissions,
    join_table: PermissionGroupDetails.table_name,
    foreign_key: primary_key,
    association_foreign_key: Permission.primary_key
  )

  has_many(
    :permission_group_details,
    foreign_key: 'permission_group_header_id',
    class_name: 'PermissionGroupDetails',
    dependent: :destroy
  )

  has_many :user_permission_groups,
           foreign_key: 'permission_group_header_id'

  has_many :erp_users,
           through: :user_permission_groups

  # Validations
  validates :code, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
  validates :description, presence: true, length: { minimum: 1, maximum: 255 }

  # Retrieves the IDs of active permissions in the permission group.
  #
  # @return [Array<Integer>] an array of active permission IDs
  # @example
  #   permission_group.active_permission_ids
  def active_permission_ids
    permission_group_details.includes(:permission).where(active: true).map(&:permission).map(&:permission_id)
  end
end

Class Method Details

.employee_portalPermissionGroup

Finds the employee portal permission group.

Examples:

PermissionGroup.employee_portal

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    if the employee portal permission group is not found



51
52
53
# File 'app/models/permission_group.rb', line 51

def employee_portal
  find_by!(code: Codes::EMPLOYEE_PORTAL)
end

.optionsActiveRecord::Relation

Retrieves a list of permission group options.

Examples:

PermissionGroup.options

Returns:

  • (ActiveRecord::Relation)

    a relation of permission groups with selected attributes



30
31
32
33
# File 'app/models/permission_group.rb', line 30

def options
  select(:permission_group_header_id, :code, :description)
    .order(code: :asc)
end

.payroll_adminPermissionGroup

Finds the payroll admin permission group.

Examples:

PermissionGroup.payroll_admin

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    if the payroll admin permission group is not found



41
42
43
# File 'app/models/permission_group.rb', line 41

def payroll_admin
  find_by!(code: Codes::PAYROLL_ADMIN)
end

Instance Method Details

#active_permission_idsArray<Integer>

Retrieves the IDs of active permissions in the permission group.

Examples:

permission_group.active_permission_ids

Returns:

  • (Array<Integer>)

    an array of active permission IDs



86
87
88
# File 'app/models/permission_group.rb', line 86

def active_permission_ids
  permission_group_details.includes(:permission).where(active: true).map(&:permission).map(&:permission_id)
end