Class: NavigationConfiguration

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

Constant Summary collapse

ADMIN =
1
PAYROLL =
14
PAYROLL_SYSTEM_RECORD =
NavigationConfiguration.find_by(name: 'payroll_system_record')&.id
EDI =
NavigationConfiguration.find_by(name: 'edi')&.id
EDI_SYSTEM_RECORD =
NavigationConfiguration.find_by(name: 'edi_system_record')&.id
EMPLOYEE_PORTAL =
NavigationConfiguration.find_by(name: 'employee_portal')&.id
AI_PLATFORM_SYSTEM_RECORD =
NavigationConfiguration.find_by(name: 'ai_platform_system_record')&.id
BLANK_PATH =
'/app/blank'

Instance Method Summary collapse

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Instance Method Details

#allows?(controller, action) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/navigation_configuration.rb', line 110

def allows?(controller, action)
  return true
  return false unless active
  return false unless controller && action

  # navigation_record_permission = NavigationRecordPermission.find_by(controller: controller, action: action)
  # return false unless navigation_record_permission

  (navigation_group_ids & navigation_record_permission.navigation_group_ids).any? ||
    (navigation_header_ids & navigation_record_permission.navigation_header_ids).any? ||
    (navigation_item_ids & navigation_record_permission.navigation_item_ids).any?
end

#associate_from_checkbox_tree_data(modules, allow_system_records = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/navigation_configuration.rb', line 67

def associate_from_checkbox_tree_data(modules, allow_system_records = false)
  process_node = lambda do |node, depth|
    case depth
    when 1
      return unless node['checked']

      group = NavigationGroup.find_by(name: node['name'])
      associate_groups([group]) if allow_system_records || !group.system_record?
    when 2
      return unless node['checked']

      header = NavigationHeader.find_by(name: node['name'])
      associate_headers([header]) if allow_system_records || !header.system_record?
    when 3
      return unless node['checked']

      item = NavigationItem.find_by(name: node['name'])
      associate_items([item]) if allow_system_records || !item.system_record?
    end
  end

  traverse_and_process = lambda do |nodes, depth|
    nodes.each do |node|
      if node['children']
        traverse_and_process.call(node['children'], depth + 1)
      else
        process_node.call(node, depth)
      end
    end
  end

  clear_associations
  traverse_and_process.call(modules['children'], 1) if modules['children']
end

#associate_groups(groups) ⇒ Object



59
60
61
62
63
64
65
# File 'app/models/navigation_configuration.rb', line 59

def associate_groups(groups)
  groups.each do |group|
    next if navigation_groups.include?(group)

    navigation_groups << group
  end
end

#associate_headers(headers) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/models/navigation_configuration.rb', line 50

def associate_headers(headers)
  headers.each do |header|
    next if navigation_headers.include?(header)

    navigation_headers << header
    associate_groups([header.navigation_group])
  end
end

#associate_items(items) ⇒ Object



41
42
43
44
45
46
47
48
# File 'app/models/navigation_configuration.rb', line 41

def associate_items(items)
  items.each do |item|
    next if navigation_items.include?(item)

    navigation_items << item
    associate_headers([item.navigation_header])
  end
end

#authorizes_navigation?(name) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'app/models/navigation_configuration.rb', line 102

def authorizes_navigation?(name)
  return false unless name

  navigation_items.exists?(name: name) ||
    navigation_headers.exists?(name: name) ||
    navigation_groups.exists?(name: name)
end

#clear_associationsObject



35
36
37
38
39
# File 'app/models/navigation_configuration.rb', line 35

def clear_associations
  navigation_items.clear
  navigation_headers.clear
  navigation_groups.clear
end

#home_pathObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/navigation_configuration.rb', line 124

def home_path
  return BLANK_PATH unless active

  navigation_groups.where(system_record: false).order(:order).each do |group|
    return group.path if group.path

    group.headers_for_configuration(self).each do |header|
      return header.path if header.path

      header.items_for_configuration(self).each do |item|
        return item.path if item.path
      end
    end
  end

  BLANK_PATH
end


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/navigation_configuration.rb', line 18

def navigation_data
  navigation_groups.order(:order).map do |group|
    group_data = group.attributes
    group_data['label'] = I18n.t("navigation.group.#{group.name}")
    group_data['navigation_headers'] = group.headers_for_configuration(self).map do |header|
      header_data = header.attributes
      header_data['label'] = I18n.t("navigation.header.#{header.name}")
      header_data['navigation_items'] = header.items_for_configuration(self).map(&:attributes)
      header_data['navigation_items'].each do |item|
        item['label'] = I18n.t("navigation.item.#{item['name']}")
      end
      header_data
    end
    group_data
  end
end