lesli 5.1.13 → 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 (63) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/lesli/application_lesli_controller.rb +27 -0
  3. data/app/controllers/lesli/items/attachments_controller.rb +3 -3
  4. data/app/controllers/lesli/items/discussions_controller.rb +2 -2
  5. data/app/controllers/lesli/items/tasks_controller.rb +2 -2
  6. data/app/controllers/lesli/roles_controller.rb +1 -6
  7. data/app/controllers/lesli/users_controller.rb +1 -5
  8. data/app/helpers/lesli/announcements_helper.rb +14 -0
  9. data/app/helpers/lesli/navigation_helper.rb +1 -1
  10. data/app/interfaces/lesli/responder_interface.rb +11 -4
  11. data/app/models/concerns/lesli/account_initializer.rb +5 -45
  12. data/app/models/concerns/lesli/user_extensions.rb +4 -2
  13. data/app/models/lesli/items/attachment.rb +3 -3
  14. data/app/models/lesli/role.rb +14 -2
  15. data/app/models/lesli/user.rb +7 -0
  16. data/app/operators/lesli/account_operator.rb +84 -0
  17. data/app/operators/lesli/resource_operator.rb +123 -0
  18. data/app/services/lesli/application_lesli_service.rb +33 -116
  19. data/app/services/lesli/resource_service.rb +12 -159
  20. data/app/services/lesli/role_service.rb +11 -229
  21. data/app/services/lesli/user_service.rb +37 -117
  22. data/app/views/lesli/layouts/application-lesli.html.erb +1 -1
  23. data/app/views/lesli/partials/_application-lesli-announcements.html.erb +28 -0
  24. data/app/views/lesli/partials/_application-lesli-header.html.erb +3 -2
  25. data/app/views/lesli/partials/_application-lesli-turbo.html.erb +5 -0
  26. data/config/initializers/lesli.rb +7 -2
  27. data/config/locales/translations.en.yml +57 -48
  28. data/config/locales/translations.es.yml +55 -45
  29. data/db/migrate/v1/0000000210_create_lesli_users.rb +1 -0
  30. data/db/seed/xyz.rb +1 -3
  31. data/lib/generators/lesli/base_generator.rb +41 -3
  32. data/lib/generators/lesli/controller/controller_generator.rb +1 -1
  33. data/lib/generators/lesli/controller/templates/controller.rb.tt +12 -7
  34. data/lib/generators/lesli/model/model_generator.rb +1 -1
  35. data/lib/generators/lesli/resource/resource_generator.rb +290 -0
  36. data/lib/generators/lesli/resource/templates/controller.rb.tt +99 -0
  37. data/lib/generators/lesli/resource/templates/locale.en.yml.tt +13 -0
  38. data/lib/generators/lesli/resource/templates/migration.rb.tt +28 -0
  39. data/lib/generators/lesli/resource/templates/model.rb.tt +38 -0
  40. data/lib/generators/lesli/resource/templates/operator.rb.tt +15 -0
  41. data/lib/generators/lesli/resource/templates/service.rb.tt +52 -0
  42. data/lib/generators/lesli/resource/templates/tests/controller_test.rb.tt +14 -0
  43. data/lib/generators/lesli/resource/templates/tests/model_test.rb.tt +24 -0
  44. data/lib/generators/lesli/resource/templates/tests/operator_test.rb.tt +15 -0
  45. data/lib/generators/lesli/resource/templates/tests/service_test.rb.tt +17 -0
  46. data/lib/generators/lesli/resource/templates/views/_form.html.erb.tt +17 -0
  47. data/lib/generators/lesli/resource/templates/views/edit.html.erb.tt +4 -0
  48. data/lib/generators/lesli/resource/templates/views/index.html.erb.tt +22 -0
  49. data/lib/generators/lesli/resource/templates/views/new.html.erb.tt +4 -0
  50. data/lib/generators/lesli/resource/templates/views/show.html.erb.tt +17 -0
  51. data/lib/generators/lesli/scaffold/scaffold_generator.rb +4 -39
  52. data/lib/generators/lesli/service/service_generator.rb +1 -1
  53. data/lib/generators/lesli/service/templates/service.rb.tt +15 -7
  54. data/lib/generators/lesli/views/views_generator.rb +1 -1
  55. data/lib/generators/lesli_generator.rb +11 -0
  56. data/lib/lesli/courier.rb +66 -34
  57. data/lib/lesli/database_tasks.rb +145 -0
  58. data/lib/lesli/version.rb +2 -2
  59. data/lib/tasks/lesli/db.rake +16 -93
  60. data/lib/tasks/lesli/resources.rake +1 -1
  61. data/readme.md +3 -1
  62. metadata +24 -3
  63. data/app/views/lesli/partials/_application-lesli-sidebar.html.erb +0 -50
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e42093c64425af9e68a26e106d438eee2e69c11ead091011ad26eff30b940282
4
- data.tar.gz: 5e56a9246828d3efe4e36354d7fe754a4556af9a0befd4ea9a6169614b94b521
3
+ metadata.gz: 4da34c1a44fb5e4c8c194e2118c171198c4d82dd852e6099c5762d5025e16b5a
4
+ data.tar.gz: a2b47bc59dd2cbb27f870048fdb3c3c660e1c0e57781f611301e6d64ad51493e
5
5
  SHA512:
6
- metadata.gz: a7f90e00184b49ffac1f366897ae89ddbd0dbe6e488aa4952d018f432a6e73f2399f9c8fd3f9b670dbb6d1bff2b6a1660f73a79eb146f5fb08ceed7e5efb2a5b
7
- data.tar.gz: 69a092763f5a8e0c55c03bd7d68aa68b511cc8a5f6bd60aedfdef9fbc601501319f12555f3c95d908e354292d3285d3289c91c01e803d72ed09669775b2f7446
6
+ metadata.gz: 50e1cdb2ed1ac0c89148eae0f040ddd9887e299637f183d05579f4bda2a1737b5915ac16b8c82b1e4229ec6a4e2b620db0ecbf5728d3f3c3681494f2eb1dddf6
7
+ data.tar.gz: eded5fd73abcb17f93d043c9eaf1111ad8b104e090c9e89b635e84aaffc541478d047583096312a11fbe454403fffb3d3412f4f2abea55315196e8be6868a246
@@ -46,11 +46,13 @@ module Lesli
46
46
 
47
47
  protect_from_forgery with: :exception
48
48
 
49
+ around_action :use_user_timezone
49
50
  before_action :set_locale
50
51
  before_action :authenticate_request if defined?(LesliShield)
51
52
  before_action :authorize_request if defined?(LesliShield)
52
53
  before_action :set_customizer
53
54
  before_action :set_requester
55
+ before_action :load_announcements
54
56
  after_action :log_requests if defined?(LesliAudit)
55
57
 
56
58
  layout "lesli/layouts/application-lesli"
@@ -67,5 +69,30 @@ module Lesli
67
69
  payload[:session_id] = session[:user_session_id]
68
70
  current_user.log(**payload)
69
71
  end
72
+
73
+ private
74
+
75
+ def use_user_timezone(&action)
76
+ # Keep the profile zone scoped to this request, including services and views.
77
+ user = current_user if respond_to?(:current_user, true)
78
+ zone = user ? user.effective_time_zone :
79
+ Time.find_zone!(Lesli.config.datetime&.dig(:time_zone).presence || Rails.application.config.time_zone)
80
+ Time.use_zone(zone, &action)
81
+ end
82
+
83
+ def load_announcements
84
+ @lesli_announcements = []
85
+ @lesli_announcements_enabled = false
86
+ return unless request.format.html? && !turbo_frame_request?
87
+ return unless respond_to?(:current_user, true) && current_user
88
+
89
+ announcements = Lesli::Courier.new(:LesliBell)
90
+ .from(:announcement_service, current_user)
91
+ .call(:list, path: request.path)
92
+ return unless announcements
93
+
94
+ @lesli_announcements_enabled = true
95
+ @lesli_announcements = announcements.to_a
96
+ end
70
97
  end
71
98
  end
@@ -66,7 +66,7 @@ module Lesli
66
66
  respond_with_lesli(
67
67
  turbo: [
68
68
  stream_notification_success(
69
- I18n.t("lesli.items.attachments.created")
69
+ I18n.t("lesli.items.attachments_created")
70
70
  ),
71
71
  attachments_stream
72
72
  ],
@@ -97,11 +97,11 @@ module Lesli
97
97
  respond_with_lesli(
98
98
  turbo: [
99
99
  stream_notification_success(
100
- I18n.t("lesli.items.attachments.deleted")
100
+ I18n.t("lesli.items.attachments_deleted")
101
101
  ),
102
102
  attachments_stream
103
103
  ],
104
- json: { message: I18n.t("lesli.items.attachments.deleted") },
104
+ json: { message: I18n.t("lesli.items.attachments_deleted") },
105
105
  redirect: return_path
106
106
  )
107
107
  else
@@ -42,7 +42,7 @@ module Lesli
42
42
  )
43
43
  refresh_discussions
44
44
 
45
- message = I18n.t("lesli.items.discussions.created")
45
+ message = I18n.t("lesli.items.discussions_created")
46
46
  respond_with_lesli(
47
47
  turbo: [
48
48
  stream_notification_success(message),
@@ -135,7 +135,7 @@ module Lesli
135
135
 
136
136
  def respond_with_comment_errors(comment)
137
137
  refresh_discussions
138
- message = I18n.t("lesli.items.discussions.invalid")
138
+ message = I18n.t("lesli.items.discussions_invalid")
139
139
  respond_with_lesli(
140
140
  turbo: [
141
141
  stream_notification_danger(message),
@@ -43,7 +43,7 @@ module Lesli
43
43
  )
44
44
  refresh_tasks
45
45
 
46
- message = I18n.t("lesli.items.tasks.created")
46
+ message = I18n.t("lesli.items.tasks_created")
47
47
  respond_with_lesli(
48
48
  turbo: [
49
49
  stream_notification_success(message),
@@ -160,7 +160,7 @@ module Lesli
160
160
 
161
161
  def task_status_message
162
162
  status = @task.done? ? :completed : :reopened
163
- I18n.t("lesli.items.tasks.#{status}")
163
+ I18n.t("lesli.items.tasks_#{status}")
164
164
  end
165
165
 
166
166
  def tasks_stream(task: nil)
@@ -32,14 +32,9 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module Lesli
34
34
  class RolesController < ApplicationLesliController
35
- before_action :set_role, only: []
36
35
 
37
36
  def list
38
- respond_to do |format|
39
- format.json {
40
- respond_with_successful(RoleService.new(current_user, query).list(params))
41
- }
42
- end
37
+ respond_with_lesli(json: RoleService.new(current_user, query).list)
43
38
  end
44
39
  end
45
40
  end
@@ -35,11 +35,7 @@ module Lesli
35
35
 
36
36
  # GET /users/list
37
37
  def list
38
- respond_to do |format|
39
- format.json {
40
- respond_with_successful(UserService.new(current_user, query).list(params))
41
- }
42
- end
38
+ respond_with_lesli(json: UserService.new(current_user, query).list(role: params[:role]))
43
39
  end
44
40
  end
45
41
  end
@@ -0,0 +1,14 @@
1
+ module Lesli
2
+ module AnnouncementsHelper
3
+ ANNOUNCEMENT_STYLES = {
4
+ "info" => { icon: "info", classes: "border-info-300 bg-info-50 text-info-900" },
5
+ "success" => { icon: "check_circle", classes: "border-success-300 bg-success-50 text-success-900" },
6
+ "warning" => { icon: "warning", classes: "border-warning-300 bg-warning-50 text-warning-900" },
7
+ "danger" => { icon: "error", classes: "border-danger-300 bg-danger-50 text-danger-900" }
8
+ }.freeze
9
+
10
+ def announcement_style(category)
11
+ ANNOUNCEMENT_STYLES.fetch(category, ANNOUNCEMENT_STYLES.fetch("info"))
12
+ end
13
+ end
14
+ end
@@ -75,7 +75,7 @@ module Lesli
75
75
  item_class = class_names(
76
76
  "group flex items-center gap-4 rounded-2xl px-4 py-3 text-base font-medium transition focus:outline-none focus:ring-2 focus:ring-sky-500/30",
77
77
  "lg:gap-2 lg:rounded-xl lg:px-4 lg:py-2 lg:text-md",
78
- active ? "bg-sky-50 text-sky-700" : "text-slate-600 hover:bg-slate-50 hover:text-slate-950 lg:text-slate-500"
78
+ active ? "text-sky-700" : "text-slate-600 hover:bg-slate-50 hover:text-slate-950 lg:text-slate-500"
79
79
  )
80
80
 
81
81
  icon_class = class_names(
@@ -32,6 +32,7 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module Lesli
34
34
  module ResponderInterface
35
+ NATIVE_TURBO_REDIRECTS = true
35
36
 
36
37
  # Lesli-specific response status used to tell JSON clients that they
37
38
  # must execute the action included in the response body.
@@ -65,9 +66,9 @@ module Lesli
65
66
  # Build a response for HTML, JSON, or Turbo Stream requests.
66
67
  #
67
68
  # `html` is passed to `render`. When omitted, Rails renders the current
68
- # action's template. Use `redirect` for HTML redirects instead of
69
- # invoking `redirect_to` before calling this method. `status` applies to
70
- # rendered responses; redirects use `redirect_status`.
69
+ # action's template. Use `redirect` for HTML and Turbo redirects instead
70
+ # of invoking `redirect_to` before calling this method. `status` applies
71
+ # to rendered responses; redirects use `redirect_status`.
71
72
  def respond_with_lesli(
72
73
  turbo: nil,
73
74
  json: nil,
@@ -92,7 +93,13 @@ module Lesli
92
93
  end
93
94
 
94
95
  format.json { respond_with_http(status, json) }
95
- format.turbo_stream { render(turbo_stream: turbo, status: status) }
96
+ format.turbo_stream do
97
+ if redirect && turbo.nil?
98
+ redirect_to(redirect, status: redirect_status)
99
+ else
100
+ render(turbo_stream: turbo, status: status)
101
+ end
102
+ end
96
103
  end
97
104
  end
98
105
 
@@ -34,54 +34,14 @@ module Lesli
34
34
  module AccountInitializer
35
35
  extend ActiveSupport::Concern
36
36
 
37
- # initialize minimum resources needed for the account
37
+ # Keep automatic account provisioning for every creation path,
38
+ # including seeders and console usage.
38
39
  def initialize_account
39
-
40
- # platform role for owners
41
- owner = self.roles
42
- .create_with({ permission_level: 2147483647, active: true })
43
- .find_or_create_by(:name => "owner")
44
-
45
- # platform role for administrators
46
- admin = self.roles
47
- .create_with({ permission_level: 100000, active: true })
48
- .find_or_create_by(name: "admin")
49
-
50
- # platform role for guests
51
- guest = self.roles
52
- .create_with({ permission_level: 10, active: true , path_default: "/administration/profile" })
53
- .find_or_create_by(name: "guest")
54
-
55
-
56
- # Add base privileges to roles
57
- if defined?(LesliShield)
58
- LesliShield::RoleActionService.new(nil).add_owner_actions(owner)
59
- LesliShield::RoleActionService.new(nil).add_owner_actions(admin)
60
- LesliShield::RoleActionService.new(nil).add_guest_actions(guest)
61
- end
40
+ AccountOperator.new(account: self).initialize_roles!
62
41
  end
63
42
 
64
-
65
- # initialize engines for new accounts
66
43
  def initialize_engines
67
- initialized_engines = []
68
-
69
- LesliSystem.engines.each do |engine, data|
70
-
71
- next if ["Lesli", "LesliBabel", "Root"].include?(engine)
72
-
73
- # Create an associated account if the attribute is blank
74
- engine.constantize::Account.find_or_create_by!(account: self)
75
-
76
- initialized_engines << engine
77
- end
78
-
79
- # Emit initialization events only after every engine account exists.
80
- # In particular, this makes the result independent from the position
81
- # of LesliAudit in the engine registry.
82
- initialized_engines.each do |engine|
83
- self.log(:account_initialization, "Engine: #{engine} initialized successfully")
84
- end
85
- end
44
+ AccountOperator.new(account: self).initialize_engines!
45
+ end
86
46
  end
87
47
  end
@@ -91,7 +91,8 @@ module Lesli
91
91
  operation:nil, # two word action description
92
92
  description:nil, # human readable description
93
93
  session_id:nil, # must come from server session
94
- subject:nil # resource related to the log
94
+ subject:nil, # resource related to the log
95
+ details:{} # explicit domain data, never raw request parameters
95
96
  )
96
97
 
97
98
  return unless defined?(LesliAudit::ActivityAudit)
@@ -104,7 +105,8 @@ module Lesli
104
105
  operation: operation,
105
106
  description: description,
106
107
  session_id: session_id,
107
- subject: subject
108
+ subject: subject,
109
+ details: details
108
110
  )
109
111
  end
110
112
  end
@@ -82,7 +82,7 @@ module Lesli
82
82
 
83
83
  errors.add(
84
84
  :file,
85
- I18n.t("lesli.items.attachments.errors.required")
85
+ I18n.t("lesli.items.attachments_errors_required")
86
86
  )
87
87
  end
88
88
 
@@ -93,7 +93,7 @@ module Lesli
93
93
  errors.add(
94
94
  :file,
95
95
  I18n.t(
96
- "lesli.items.attachments.errors.too_large",
96
+ "lesli.items.attachments_errors_too_large",
97
97
  size: ActiveSupport::NumberHelper.number_to_human_size(
98
98
  self.class.lesli_attachment_max_file_size
99
99
  )
@@ -109,7 +109,7 @@ module Lesli
109
109
 
110
110
  errors.add(
111
111
  :file,
112
- I18n.t("lesli.items.attachments.errors.unsupported_type")
112
+ I18n.t("lesli.items.attachments_errors_unsupported_type")
113
113
  )
114
114
  end
115
115
  end
@@ -40,8 +40,20 @@ module Lesli
40
40
  has_many :privileges, dependent: :delete_all, class_name: 'LesliShield::Role::Privilege'
41
41
 
42
42
  # validations
43
- validates :name, presence: :true
44
- validates :permission_level, presence: :true
43
+ validates :name,
44
+ presence: true,
45
+ uniqueness: {
46
+ scope: :account_id,
47
+ case_sensitive: false,
48
+ conditions: -> { where(deleted_at: nil) }
49
+ }
50
+ validates :permission_level,
51
+ presence: true,
52
+ numericality: {
53
+ only_integer: true,
54
+ greater_than_or_equal_to: 0
55
+ }
56
+ validates :active, inclusion: { in: [true, false] }
45
57
 
46
58
  # initializers for new roles
47
59
  after_create :after_create_role
@@ -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 },
@@ -72,6 +74,11 @@ module Lesli
72
74
 
73
75
  before_create :before_create_user
74
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
+
75
82
  private
76
83
 
77
84
  def before_create_user
@@ -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