lesli 5.1.12 → 5.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -0
  3. data/app/assets/stylesheets/lesli/application.css +43 -3
  4. data/app/controllers/lesli/application_devise_controller.rb +9 -5
  5. data/app/controllers/lesli/application_lesli_controller.rb +31 -0
  6. data/app/controllers/lesli/items/attachments_controller.rb +200 -0
  7. data/app/controllers/lesli/items/discussions_controller.rb +94 -67
  8. data/app/controllers/lesli/items/tasks_controller.rb +132 -62
  9. data/app/controllers/lesli/roles_controller.rb +1 -6
  10. data/app/controllers/lesli/users_controller.rb +1 -5
  11. data/app/helpers/lesli/announcements_helper.rb +14 -0
  12. data/app/helpers/lesli/navigation_helper.rb +1 -1
  13. data/app/helpers/lesli/turbo_helper.rb +0 -18
  14. data/app/interfaces/lesli/responder_interface.rb +65 -38
  15. data/app/mailers/lesli/application_lesli_mailer.rb +34 -8
  16. data/app/models/concerns/lesli/account_initializer.rb +5 -37
  17. data/app/models/concerns/lesli/account_logs.rb +5 -16
  18. data/app/models/concerns/lesli/items/activities.rb +29 -33
  19. data/app/models/concerns/lesli/items/attachments.rb +88 -0
  20. data/app/models/concerns/lesli/items/discussions.rb +25 -11
  21. data/app/models/concerns/lesli/items/ownership.rb +27 -0
  22. data/app/models/concerns/lesli/items/tasks.rb +26 -6
  23. data/app/models/concerns/lesli/user_extensions.rb +8 -10
  24. data/app/models/lesli/account.rb +10 -14
  25. data/app/models/lesli/items/activity.rb +25 -2
  26. data/app/models/lesli/items/attachment.rb +117 -0
  27. data/app/models/lesli/items/discussion.rb +6 -2
  28. data/app/models/lesli/items/task.rb +3 -1
  29. data/app/models/lesli/role.rb +14 -2
  30. data/app/models/lesli/user.rb +19 -21
  31. data/app/operators/lesli/account_operator.rb +84 -0
  32. data/app/operators/lesli/resource_operator.rb +123 -0
  33. data/app/services/lesli/application_lesli_service.rb +33 -116
  34. data/app/services/lesli/resource_service.rb +12 -159
  35. data/app/services/lesli/role_service.rb +11 -229
  36. data/app/services/lesli/user_service.rb +37 -117
  37. data/app/views/lesli/abouts/welcome.html.erb +2 -5
  38. data/app/views/lesli/errors/not_found.html.erb +7 -21
  39. data/app/views/lesli/errors/unauthorized.html.erb +11 -27
  40. data/app/views/lesli/layouts/application-devise.html.erb +10 -9
  41. data/app/views/lesli/layouts/application-lesli.html.erb +2 -2
  42. data/app/views/lesli/partials/_application-lesli-announcements.html.erb +28 -0
  43. data/app/views/lesli/partials/_application-lesli-assets.html.erb +3 -3
  44. data/app/views/lesli/partials/_application-lesli-header.html.erb +8 -11
  45. data/app/views/lesli/partials/_application-lesli-turbo.html.erb +5 -0
  46. data/config/initializers/lesli.rb +10 -2
  47. data/config/locales/translations.en.yml +57 -14
  48. data/config/locales/translations.es.yml +55 -11
  49. data/db/migrate/v1/0000000210_create_lesli_users.rb +1 -0
  50. data/db/seed/accounts.rb +5 -5
  51. data/db/seed/users.rb +8 -16
  52. data/db/seed/xyz.rb +36 -17
  53. data/lib/generators/lesli/base_generator.rb +41 -3
  54. data/lib/generators/lesli/controller/controller_generator.rb +1 -1
  55. data/lib/generators/lesli/controller/templates/controller.rb.tt +12 -7
  56. data/lib/generators/lesli/model/model_generator.rb +1 -1
  57. data/lib/generators/lesli/resource/resource_generator.rb +290 -0
  58. data/lib/generators/lesli/resource/templates/controller.rb.tt +99 -0
  59. data/lib/generators/lesli/resource/templates/locale.en.yml.tt +13 -0
  60. data/lib/generators/lesli/resource/templates/migration.rb.tt +28 -0
  61. data/lib/generators/lesli/resource/templates/model.rb.tt +38 -0
  62. data/lib/generators/lesli/resource/templates/operator.rb.tt +15 -0
  63. data/lib/generators/lesli/resource/templates/service.rb.tt +52 -0
  64. data/lib/generators/lesli/resource/templates/tests/controller_test.rb.tt +14 -0
  65. data/lib/generators/lesli/resource/templates/tests/model_test.rb.tt +24 -0
  66. data/lib/generators/lesli/resource/templates/tests/operator_test.rb.tt +15 -0
  67. data/lib/generators/lesli/resource/templates/tests/service_test.rb.tt +17 -0
  68. data/lib/generators/lesli/resource/templates/views/_form.html.erb.tt +17 -0
  69. data/lib/generators/lesli/resource/templates/views/edit.html.erb.tt +4 -0
  70. data/lib/generators/lesli/resource/templates/views/index.html.erb.tt +22 -0
  71. data/lib/generators/lesli/resource/templates/views/new.html.erb.tt +4 -0
  72. data/lib/generators/lesli/resource/templates/views/show.html.erb.tt +17 -0
  73. data/lib/generators/lesli/scaffold/scaffold_generator.rb +4 -39
  74. data/lib/generators/lesli/service/service_generator.rb +1 -1
  75. data/lib/generators/lesli/service/templates/service.rb.tt +15 -7
  76. data/lib/generators/lesli/views/views_generator.rb +1 -1
  77. data/lib/generators/lesli_generator.rb +11 -0
  78. data/lib/lesli/courier.rb +66 -34
  79. data/lib/lesli/database_tasks.rb +145 -0
  80. data/lib/lesli/engine.rb +0 -4
  81. data/lib/lesli/router.rb +5 -2
  82. data/lib/lesli/version.rb +2 -2
  83. data/lib/migrate/items/activity_structure.rb +8 -8
  84. data/lib/migrate/items/attachment_structure.rb +9 -14
  85. data/lib/migrate/items/discussion_structure.rb +5 -5
  86. data/lib/migrate/items/task_structure.rb +2 -3
  87. data/lib/migrate/shared/account_structure.rb +1 -1
  88. data/lib/tasks/lesli/db.rake +16 -93
  89. data/lib/tasks/lesli/resources.rake +1 -1
  90. data/lib/tasks/lesli_tasks.rake +2 -3
  91. data/readme.md +6 -4
  92. data/test/fixtures/lesli_accounts.yml +18 -0
  93. data/test/fixtures/lesli_users.yml +23 -0
  94. metadata +39 -26
  95. data/app/assets/stylesheets/lesli/application.tailwind.css +0 -33
  96. data/app/views/lesli/partials/_application-lesli-sidebar.html.erb +0 -50
  97. data/config/brakeman.yml +0 -44
  98. data/config/initializers/devise.rb +0 -27
@@ -34,6 +34,8 @@ module Lesli
34
34
  class User < ApplicationLesliRecord
35
35
  include Lesli::UserExtensions
36
36
 
37
+ validates :time_zone, inclusion: { in: TZInfo::Timezone.all_identifiers }, allow_nil: true
38
+
37
39
  # user required configuration
38
40
  validates(:email,
39
41
  format: { with: URI::MailTo::EMAIL_REGEXP },
@@ -41,17 +43,6 @@ module Lesli
41
43
  uniqueness: true
42
44
  );
43
45
 
44
- # devise implementation
45
- devise(
46
- :database_authenticatable,
47
- :registerable,
48
- :rememberable,
49
- :recoverable,
50
- :validatable,
51
- :confirmable,
52
- :trackable
53
- );
54
-
55
46
  # users belongs to an account only...
56
47
  belongs_to :account, optional: true
57
48
 
@@ -64,29 +55,36 @@ module Lesli
64
55
  # users data extensions
65
56
  has_many :tokens
66
57
  has_many :settings
67
- has_many :sessions, class_name: 'LesliShield::User::Session'
68
58
 
69
-
70
- # Users can have many roles and too many privileges through the roles
59
+ # Authentication and authorization records are implemented by
60
+ # LesliShield. These associations remain explicit here because they
61
+ # are part of the public API used throughout the platform.
62
+ has_many :sessions, class_name: "LesliShield::User::Session"
71
63
  has_many :user_roles, class_name: "LesliShield::User::Role"
72
64
  has_many :roles, through: :user_roles, source: :role
73
65
  has_many :privileges, through: :roles
74
66
 
75
-
76
- # allow save duplicated users to execute callbacks
77
- def save(*args)
78
- super()
79
- rescue ActiveRecord::RecordNotUnique => error
80
- end
67
+ # Audit records are implemented by LesliAudit. The associations are
68
+ # declared here because requests, journals, and logs form part of the
69
+ # user API consumed by the platform.
70
+ has_many :requests, class_name: "LesliAudit::UserRequest", foreign_key: :user_id, inverse_of: :user
71
+ has_many :journals, class_name: "LesliAudit::UserJournal", foreign_key: :user_id, inverse_of: :user
72
+ has_many :logs, class_name: "LesliAudit::UserLog", foreign_key: :user_id, inverse_of: :user
73
+ has_many :account_logs, class_name: "LesliAudit::AccountLog", foreign_key: :user_id, inverse_of: :user
81
74
 
82
75
  before_create :before_create_user
83
76
 
77
+ # Resolve the display zone without storing the fallback as a user preference.
78
+ def effective_time_zone
79
+ Time.find_zone!(time_zone || Lesli.config.datetime&.dig(:time_zone).presence || Rails.application.config.time_zone)
80
+ end
81
+
84
82
  private
85
83
 
86
84
  def before_create_user
87
85
  self.uid ||= loop do
88
86
  candidate = generate_resource_uid(prefix:'LID')
89
- break candidate unless self.account&.users&.exists?(uid: candidate)
87
+ break candidate unless self.class.exists?(uid: candidate)
90
88
  end
91
89
  end
92
90
 
@@ -0,0 +1,84 @@
1
+ =begin
2
+
3
+ Lesli
4
+
5
+ Copyright (c) 2025, Lesli Technologies, S. A.
6
+
7
+ This program is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program. If not, see http://www.gnu.org/licenses/.
19
+
20
+ Lesli · Ruby on Rails SaaS Development Framework.
21
+
22
+ Made with ♥ by LesliTech
23
+ Building a better future, one line of code at a time.
24
+
25
+ @contact hello@lesli.tech
26
+ @website https://www.lesli.tech
27
+ @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
+
29
+ // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
+ // ·
31
+ =end
32
+
33
+ module Lesli
34
+ class AccountOperator
35
+ def initialize(account:)
36
+ @account = account
37
+ end
38
+
39
+ def initialize_roles!
40
+ Account.transaction do
41
+ owner = account.roles.create_with(permission_level: 2147483647, active: true)
42
+ .find_or_create_by!(name: "owner")
43
+ admin = account.roles.create_with(permission_level: 100000, active: true)
44
+ .find_or_create_by!(name: "admin")
45
+ guest = account.roles.create_with(
46
+ permission_level: 10, active: true, path_default: "/administration/profile"
47
+ ).find_or_create_by!(name: "guest")
48
+
49
+ if defined?(LesliShield)
50
+ actions = LesliShield::RoleActionService.new(nil)
51
+ actions.add_owner_actions(owner)
52
+ actions.add_owner_actions(admin)
53
+ actions.add_guest_actions(guest)
54
+ end
55
+ end
56
+
57
+ account
58
+ end
59
+
60
+ def initialize_engines!
61
+ Account.transaction do
62
+ initialized_engines = []
63
+ LesliSystem.engines.each_key do |engine|
64
+ next if %w[Lesli LesliBabel Root].include?(engine)
65
+
66
+ engine_account = engine.constantize::Account.find_or_create_by!(account: account)
67
+ initialized_engines << engine if engine_account.previously_new_record?
68
+ end
69
+
70
+ # Audit must exist before recording initialization, regardless
71
+ # of registry order. Repeated runs do not repeat these events.
72
+ initialized_engines.each do |engine|
73
+ account.log(:account_initialization, "Engine: #{engine} initialized successfully")
74
+ end
75
+ end
76
+
77
+ account
78
+ end
79
+
80
+ private
81
+
82
+ attr_reader :account
83
+ end
84
+ end
@@ -0,0 +1,123 @@
1
+ =begin
2
+
3
+ Lesli
4
+
5
+ Copyright (c) 2026, Lesli Technologies, S. A.
6
+
7
+ This program is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program. If not, see http://www.gnu.org/licenses/.
19
+
20
+ Lesli · Ruby on Rails SaaS Development Framework.
21
+
22
+ Made with ♥ by LesliTech
23
+ Building a better future, one line of code at a time.
24
+
25
+ @contact hello@lesli.tech
26
+ @website https://www.lesli.tech
27
+ @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
+
29
+ // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
+ // ·
31
+ =end
32
+
33
+ module Lesli
34
+ class ResourceOperator
35
+ DEVISE_CONTROLLERS = %w[
36
+ users/registrations users/sessions users/passwords users/confirmations
37
+ ].freeze
38
+
39
+ HOST_INTERNAL_CONTROLLERS = %w[
40
+ rails action_mailbox active_storage view_components
41
+ turbo/native/navigation lesli/abouts
42
+ ].freeze
43
+
44
+ # Scan routes from the main application and installed engines,
45
+ # then register their controllers and actions as Lesli resources.
46
+ # Repeated runs retain existing resource IDs.
47
+ def register!
48
+ # Register the complete catalog in one transaction so a failed
49
+ # action does not leave a partially updated resource catalog.
50
+ Resource.transaction do
51
+ routes_by_engine.each do |engine, controllers|
52
+ controllers.each do |route, actions|
53
+ # Build the standard controller name from its route.
54
+ # Examples:
55
+ # users -> Users
56
+ # lesli_bell/notifications -> LesliBell::Notifications
57
+ # account/currency/exchange_rates -> Account::Currency::ExchangeRates
58
+ identifier = route.camelize
59
+ # Create the controller descriptor or update its existing metadata.
60
+ controller = Resource.find_or_initialize_by(route: route)
61
+ controller.update!(
62
+ label: identifier.demodulize.humanize,
63
+ engine: engine,
64
+ identifier: identifier
65
+ )
66
+
67
+ # Register each action only once. Different HTTP verbs can
68
+ # point to the same controller action.
69
+ actions.uniq.each do |action|
70
+ controller.children.find_or_create_by!(
71
+ action: action,
72
+ identifier: action,
73
+ label: action.humanize
74
+ )
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def routes_by_engine
84
+ # Group the available actions by engine and controller.
85
+ # Example: { "lesli_bell" => { "lesli_bell/notifications" => ["index"] } }
86
+ controllers = { "rails_app" => {}, "lesli" => {} }
87
+
88
+ # Get controllers and actions from the main Rails application.
89
+ Rails.application.routes.routes.each do |route|
90
+ controller = route.defaults[:controller]
91
+ action = route.defaults[:action]
92
+ next if controller.blank? || action.blank?
93
+ # Exclude internal Rails routes and the default welcome page.
94
+ next if HOST_INTERNAL_CONTROLLERS.any? { |name| controller.include?(name) }
95
+
96
+ # Devise routes belong to Lesli's access-control catalog,
97
+ # even though they are mounted in the main application.
98
+ engine = DEVISE_CONTROLLERS.include?(controller) ? "lesli" : "rails_app"
99
+ (controllers[engine][controller] ||= []) << action
100
+ end
101
+
102
+ # Get controllers and actions from each installed engine.
103
+ LesliSystem.engines.each do |engine, info|
104
+ # The main application was already processed above.
105
+ next if engine == "Root"
106
+
107
+ engine.constantize::Engine.routes.routes.each do |route|
108
+ controller = route.defaults[:controller]
109
+ action = route.defaults[:action]
110
+ # Mounted apps and redirects do not necessarily name an action.
111
+ next if controller.blank? || action.blank?
112
+ next if controller.include?("rails/health")
113
+
114
+ # Create the engine/controller containers before adding the action.
115
+ controllers[info[:code]] ||= {}
116
+ (controllers[info[:code]][controller] ||= []) << action
117
+ end
118
+ end
119
+
120
+ controllers
121
+ end
122
+ end
123
+ end
@@ -33,137 +33,67 @@ Building a better future, one line of code at a time.
33
33
  # ·
34
34
  module Lesli
35
35
  class ApplicationLesliService
36
+ attr_reader :current_user, :query, :errors
36
37
 
37
-
38
- # Set container attributes to manage the life-cycle of the service
39
- @query;
40
- @resource;
41
- @failures;
42
- @current_user;
43
-
44
-
45
- # Service constructor
46
- # current_user is always required to initialize a service object
47
- # current user is used to get the data only from the account context
48
- def initialize current_user=nil, query={}
49
-
50
- # make the current user globaly available in the service object
38
+ # Services receive their request context explicitly. A system operation
39
+ # without an authenticated user must pass nil intentionally.
40
+ def initialize(current_user, query = {})
51
41
  @current_user = current_user
52
-
53
- # stores the resources from the database as result of the active record queries
54
- @resource = nil
55
-
56
- # stores any error found during the life-cycle of the service object
57
- @failures = []
58
-
59
- # standard conditions to query the database
60
42
  @query = query
43
+ @resource = nil
44
+ @errors = []
61
45
  end
62
46
 
63
-
64
- # Find an specific resource through the main id
65
- def find resource = nil
66
- # Look for the resource in the database
67
- # self.resource = current_user.account.users.find_by_id(id) # example
68
-
69
- # Should always return self
70
- self.resource = resource if resource
71
- self
72
- end
73
-
74
-
75
- # Standard method to list data from the database
76
- def list params=nil
77
- end
78
-
79
-
80
- # Standard method to index data from the database
81
- def index params=nil
82
- end
83
-
84
-
85
- # Standard method to show data from the database
86
- def show
87
- end
88
-
89
-
90
- # Standard method to create new resource into the database
91
- def create params
92
- # Example:
93
- # user = current_user.account.users.new(params)
94
-
95
- # if user.save
96
- # self.resource = user
97
- # else
98
- # self.error(user.errors.full_messages.to_sentence)
99
- # end
100
- self.resource = params
47
+ # Store the result of an account-scoped lookup and return the service so
48
+ # controllers can inspect found? and result through one contract.
49
+ def find(resource)
50
+ self.resource = resource
101
51
  self
102
52
  end
103
53
 
104
-
105
- # Standard method to update resources into the database
106
- def update params
107
- end
108
-
109
-
110
- # Standard method to delete data from the database
111
- def delete
112
- end
113
-
114
-
115
- # Method to check if the service object has data available
116
- # this method ment to be used together and after the find method
117
54
  def found?
118
- !self.resource.blank?
55
+ !resource.nil?
119
56
  end
120
57
 
121
-
122
- # Check if the service object has errors
123
58
  def successful?
124
- self.failures.empty?
125
- end
126
-
127
-
128
- # Get the list of erros
129
- def errors
130
- self.failures
59
+ errors.empty?
131
60
  end
132
61
 
133
-
134
- # Get the list of erros as single string
135
62
  def errors_as_sentence
136
- self.failures.to_sentence
137
- end
138
-
139
-
140
- # Register a new error for the current service object
141
- def error error
142
- self.failures.push(error)
63
+ errors.to_sentence
143
64
  end
144
65
 
145
-
146
- # Return the resource as a result
147
66
  def result
148
- self.resource
67
+ resource
149
68
  end
150
69
 
151
- def cache_key_for_account(method=nil)
152
- cache_key_builder('account',current_user.account.id,method)
70
+ def cache_key_for_account(method = nil)
71
+ cache_key_builder("account", current_user.account.id, method)
153
72
  end
154
73
 
155
- def cache_key_for_user(method=nil)
156
- cache_key_builder('user',current_user.id,method)
74
+ def cache_key_for_user(method = nil)
75
+ cache_key_builder("user", current_user.id, method)
157
76
  end
158
77
 
159
- private
78
+ protected
160
79
 
161
- def response resource = nil
80
+ attr_reader :resource
81
+ attr_writer :resource
82
+
83
+ # Store the result of a mutation and preserve the service response
84
+ # contract used by controllers.
85
+ def response(resource)
162
86
  self.resource = resource
163
87
  self
164
88
  end
165
89
 
166
- def cache_key_builder(namespace,namespace_id,method)
90
+ def error(message)
91
+ errors << message
92
+ end
93
+
94
+ private
95
+
96
+ def cache_key_builder(namespace, namespace_id, method)
167
97
  [
168
98
  namespace,
169
99
  namespace_id,
@@ -171,19 +101,6 @@ module Lesli
171
101
  method,
172
102
  query[:cacheKey]
173
103
  ].compact.join(":").tr("/_", "-")
174
- end
175
-
176
- attr_reader :current_user
177
-
178
- attr_reader :resource
179
- attr_writer :resource
180
-
181
- attr_reader :successful
182
- attr_writer :successful
183
-
184
- attr_reader :failures
185
- attr_writer :failures
186
-
187
- attr_reader :query
104
+ end
188
105
  end
189
106
  end
@@ -32,21 +32,9 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module Lesli
34
34
  class ResourceService < Lesli::ApplicationLesliService
35
+ def index(matrix: false)
35
36
 
36
- DEVISE_CONTROLLERS = [
37
- "users/registrations",
38
- "users/sessions",
39
- "users/passwords",
40
- "users/confirmations"
41
- ]
42
-
43
- def initialize
44
- end
45
-
46
- def index matrix:false
47
-
48
- # get a matrix of controllers and actions
49
- c = Resource.actions.joins(:parent).select(
37
+ actions = Resource.actions.joins(:parent).select(
50
38
  "parents_lesli_resources.engine as engine",
51
39
  "parents_lesli_resources.route as route",
52
40
  "parents_lesli_resources.identifier as controller",
@@ -70,153 +58,18 @@ module Lesli
70
58
  .where("parents_lesli_resources.deleted_at is NULL")
71
59
  .order("importance DESC")
72
60
 
73
- return c unless matrix
74
-
75
- cc = {}
76
-
77
- # convert the matrix to a hash of engines with controllers and available actions as values
78
- # example:
79
- # my_engine: { my_controller: [ my list of actions ]}
80
- c.each do |c|
81
-
82
- engine = c[:engine]
83
- controller = c[:controller]
84
-
85
- # create a uniq container for every action that belongs to a specific controller
86
- if cc[engine].blank?
87
- cc[engine] = {}
88
- end
89
-
90
- # create a uniq container for every action that belongs to a specific controller
91
- if cc[engine][controller].blank?
92
- cc[engine][controller] = {
93
- id: c[:controller_id],
94
- name: c[:controller_name],
95
- route: c[:route],
96
- actions: []
97
- }
98
- end
99
-
100
- # push every action to his specic controller
101
- cc[engine][controller][:actions].push({
102
- id: c[:action_id],
103
- action: c[:action]
61
+ return actions unless matrix
62
+
63
+ actions.each_with_object({}) do |action, engines|
64
+ controllers = (engines[action[:engine]] ||= {})
65
+ controller = (controllers[action[:controller]] ||= {
66
+ id: action[:controller_id],
67
+ name: action[:controller_name],
68
+ route: action[:route],
69
+ actions: []
104
70
  })
71
+ controller[:actions] << { id: action[:action_id], action: action[:action] }
105
72
  end
106
-
107
- return cc
108
-
109
- end
110
-
111
- # Scan new routes added and create role privileges
112
- def build
113
-
114
- # get all the engines, controllers and actions
115
- engines = scan_for_engine_controllers
116
-
117
- # Register descriptors and privileges for all the accounts
118
- engines.each do |engine, controllers|
119
-
120
- controllers.each do |controller_route, controller_actions|
121
-
122
- # Build a strig with the standard name of a Rails controller from the standard routes
123
- # Examples:
124
- # users converts to Users
125
- # cloud_bell/notifications converts to CloudBell::Notifications
126
- # sometimes we need a second split to deal with third level deep of controllers
127
- # Example: "Account::Currency::ExchangeRatesController" from "account/currency/exchange_rates"
128
- identifier = controller_route
129
- .camelize
130
- .gsub('/', '::')
131
-
132
- name = identifier.demodulize.humanize
133
-
134
- controller = Lesli::Resource.find_or_initialize_by(route: controller_route)
135
- controller.assign_attributes(
136
- label: name,
137
- engine: engine,
138
- identifier: identifier
139
- )
140
- controller.save!
141
-
142
- controller_actions.each do |action_name|
143
- controller.children.find_or_create_by!(
144
- action: action_name,
145
- identifier: action_name,
146
- label: action_name.humanize
147
- )
148
- end
149
- end
150
- end
151
- end
152
-
153
- private
154
-
155
- def scan_for_engine_controllers
156
-
157
- # Global container
158
- controller_list = {
159
- "rails_app" => {},
160
- "lesli" => {}
161
- }
162
-
163
- # Get the list of controllers and actions of the main rails app
164
- Rails.application.routes.routes.each do |route|
165
-
166
- list = "rails_app"
167
- route = route.defaults
168
-
169
- # filter the non-used main app routes
170
- next if route[:controller].blank?
171
- next if route[:controller].include? "rails"
172
- next if route[:controller].include? "action_mailbox"
173
- next if route[:controller].include? "active_storage"
174
- next if route[:controller].include? "view_components"
175
- next if route[:controller].include? "turbo/native/navigation"
176
- next if route[:controller].include? "lesli/abouts" #default welcome page
177
-
178
- if DEVISE_CONTROLLERS.include?(route[:controller])
179
- list = "lesli"
180
- end
181
-
182
- # create a container for the actions related to a controller
183
- controller_list[list][route[:controller]] = [] unless controller_list[list][route[:controller]]
184
-
185
- # assign and group all the actions related to the controller
186
- controller_list[list][route[:controller]].push(route[:action])
187
-
188
- end
189
-
190
- # Get the list of controllers and actions from engines
191
- LesliSystem.engines.each do |engine, engine_info|
192
-
193
- # Do not process main Rails app
194
- next if engine == "Root"
195
-
196
- # load and retrieve the list of controllers and actions from an engine
197
- routes = "#{engine}::Engine".constantize.routes.routes.each do |route|
198
- route = route.defaults
199
-
200
- # validate if route has information, some special routes like redirects
201
- # can generate an empty entry in the route hash
202
- next if route.empty?
203
- next if route[:controller].include? "rails/health"
204
-
205
- # get the engine code
206
- engine_code = engine_info[:code]
207
-
208
- # create a container for the controllers related to the engine
209
- controller_list[engine_code] = {} if controller_list[engine_code].blank?
210
-
211
- # assign and group all the actions related to the controller
212
- controller_list[engine_code][route[:controller]] = [] if controller_list[engine_code][route[:controller]].blank?
213
-
214
- # assign and group all the actions related to the controller
215
- controller_list[engine_code][route[:controller]].push(route[:action])
216
- end
217
- end
218
-
219
- return controller_list
220
73
  end
221
74
  end
222
75
  end