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
@@ -33,93 +33,163 @@ Building a better future, one line of code at a time.
33
33
  module Lesli
34
34
  module Items
35
35
  class TasksController < ApplicationLesliController
36
- before_action :set_task, only: %i[show update]
37
- before_action :set_taskable, only: %i[index create]
36
+ before_action :set_taskable, only: :create
37
+ before_action :set_task, only: :update
38
38
 
39
- def index
40
- @tasks = task_class
41
- .where(account: current_user.account)
42
- .order(created_at: :desc)
43
- respond_with_lesli(json: @tasks)
39
+ def create
40
+ task = @taskable.add_task!(
41
+ creator: current_user,
42
+ title: task_params[:title]
43
+ )
44
+ refresh_tasks
45
+
46
+ message = I18n.t("lesli.items.tasks_created")
47
+ respond_with_lesli(
48
+ turbo: [
49
+ stream_notification_success(message),
50
+ tasks_stream
51
+ ],
52
+ json: {
53
+ message: message,
54
+ task: task_payload(task)
55
+ },
56
+ status: :created
57
+ )
58
+ rescue ActiveRecord::RecordInvalid => error
59
+ respond_with_task_errors(error.record)
44
60
  end
45
61
 
46
- def show
62
+ def update
63
+ @task.update!(done: !@task.done?)
64
+
65
+ message = task_status_message
66
+ respond_with_lesli(
67
+ turbo: [
68
+ stream_notification_success(message),
69
+ turbo_stream.replace(
70
+ helpers.dom_id(@task, scope_key)
71
+ ) do
72
+ render_task(@task)
73
+ end
74
+ ],
75
+ json: {
76
+ message: message,
77
+ task: task_payload(@task)
78
+ }
79
+ )
80
+ rescue ActiveRecord::RecordInvalid => error
81
+ respond_with_task_errors(error.record)
47
82
  end
48
83
 
49
- def create
50
- task = task_class.new(task_params)
84
+ private
51
85
 
52
- task.user = current_user
53
- task.account = current_user.account
54
- task.taskable = @taskable if @taskable
86
+ def scope_key
87
+ ActiveSupport::Inflector
88
+ .underscore(task_class.name)
89
+ .tr("/", "_")
90
+ end
55
91
 
56
- if task.save
57
- respond_with_lesli(:turbo => [
58
- stream_notification_success("Task created"),
59
- turbo_stream.prepend("#{scope_key}-lesli-items-tasks-list") do
60
- LesliView::Items::Task.new(task, scope_key).render_in(view_context)
61
- end
62
- ])
63
- else
64
- respond_with_lesli(
65
- :turbo => stream_notification_danger(task.errors.full_messages)
66
- )
67
- end
92
+ def task_class
93
+ self.class.module_parent.const_get("Task")
68
94
  end
69
95
 
70
- def update
71
- @task.done = true
72
- @task.save
73
- respond_with_lesli(:turbo => [
74
- stream_notification_success("Task updated #{@taskable}"),
75
- turbo_stream.replace(helpers.dom_id(@task, scope_key)) do
76
- LesliView::Items::Task.new(@task, scope_key).render_in(view_context)
77
- end
78
- ])
96
+ def taskable_class
97
+ allowed_taskable_classes.find do |klass|
98
+ klass.name == params.require(:taskable_type)
99
+ end || raise(
100
+ ActiveRecord::RecordNotFound,
101
+ "Unauthorized taskable type"
102
+ )
79
103
  end
80
104
 
81
- private
105
+ def allowed_taskable_classes
106
+ []
107
+ end
82
108
 
83
- # return a key to identify the engine where the tasks are running
84
- def scope_key = ActiveSupport::Inflector.underscore(task_class.name).tr("/", "_")
109
+ def set_task
110
+ @task = task_class.find(params[:id])
111
+ ensure_taskable_access!(@task.taskable)
112
+ end
85
113
 
86
- # return the task class including the engine where the tasks are running
87
- def task_class = self.class.module_parent.const_get("Task")
114
+ def set_taskable
115
+ @taskable = taskable_class.find(
116
+ params.require(:taskable_id)
117
+ )
118
+ ensure_taskable_access!(@taskable)
119
+ end
88
120
 
89
- # validate we invoke only models that implements Lesli::Item::Tasks concern
90
- def validate_taskable taskable
121
+ def ensure_taskable_access!(taskable)
122
+ allowed = taskable.respond_to?(:lesli_item_accessible_by?) &&
123
+ taskable.lesli_item_accessible_by?(current_user)
91
124
 
92
- # 1. Attempt to resolve the class safely
93
- klass = taskable.safe_constantize
125
+ raise ActiveRecord::RecordNotFound unless allowed
126
+ end
94
127
 
95
- # 2. VALIDATION: Check if the class is allowed to be taskable
96
- # We check if the class exists AND if it includes your specific taskable trait
97
- unless klass && klass.respond_to?(:is_lesli_taskable?) && klass.is_lesli_taskable?
98
- render json: { error: "Unauthorized taskable type" }, status: :forbidden and return
99
- end
128
+ def task_params
129
+ params.require(scope_key.to_sym).permit(:title)
130
+ end
100
131
 
101
- klass
132
+ def render_task(task)
133
+ LesliView::Items::Task
134
+ .new(task, scope_key)
135
+ .render_in(view_context)
102
136
  end
103
137
 
104
- def set_task
105
- @task = task_class.where(account: current_user.account).find(params[:id])
138
+ def task_payload(task)
139
+ {
140
+ id: task.id,
141
+ title: task.title,
142
+ done: task.done?,
143
+ taskable_type: task.taskable_type,
144
+ taskable_id: task.taskable_id,
145
+ creator: {
146
+ id: task.user_id,
147
+ name: task.user.name
148
+ },
149
+ created_at: task.created_at,
150
+ updated_at: task.updated_at
151
+ }
106
152
  end
107
153
 
108
- # Permite filtrar/crear para un actionable específico:
109
- # params: actionable_type=LesliSupport::Ticket&actionable_id=123
110
- def set_taskable
111
- type = params[:taskable_type].presence
112
- id = params[:taskable_id].presence
113
- return if type.blank? || id.blank?
154
+ def refresh_tasks
155
+ association_name =
156
+ @taskable.class.lesli_tasks_association_name
114
157
 
115
- taskable = validate_taskable(type)
158
+ @taskable.public_send(association_name).reset
159
+ end
116
160
 
117
- # 3. Execution: Now it is safe to query
118
- @taskable = taskable.where(account: current_user.account).find_by(id: id)
161
+ def task_status_message
162
+ status = @task.done? ? :completed : :reopened
163
+ I18n.t("lesli.items.tasks_#{status}")
119
164
  end
120
165
 
121
- def task_params
122
- params.require(scope_key.to_sym).permit(:title)
166
+ def tasks_stream(task: nil)
167
+ turbo_stream.replace(
168
+ helpers.dom_id(@taskable, :tasks)
169
+ ) do
170
+ LesliView::Items::Tasks
171
+ .new(@taskable, task: task)
172
+ .render_in(view_context)
173
+ end
174
+ end
175
+
176
+ def respond_with_task_errors(task)
177
+ turbo = [
178
+ stream_notification_danger(
179
+ task.errors.full_messages.to_sentence
180
+ )
181
+ ]
182
+
183
+ if task.new_record?
184
+ refresh_tasks
185
+ turbo << tasks_stream(task: task)
186
+ end
187
+
188
+ respond_with_lesli(
189
+ turbo: turbo,
190
+ json: { errors: task.errors },
191
+ status: :unprocessable_content
192
+ )
123
193
  end
124
194
  end
125
195
  end
@@ -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(
@@ -37,24 +37,6 @@ module Lesli
37
37
  link_to(body, url, class: "link", data: { turbo_frame: '_top' })
38
38
  end
39
39
 
40
- # Meta-programming to define flash setter methods dynamically
41
- # success("Everything worked!")
42
- # danger("Oops, there was an error.")
43
- # info("Just an informational message.")
44
- # warning("This is a warning.")
45
- [:info, :success, :warning, :danger].each do |flash_type|
46
- define_method(flash_type) do |message|
47
- flash[flash_type] = message
48
- end
49
- end
50
-
51
- def stream_notification_success(message)
52
- success(message)
53
- turbo_stream.replace("application-lesli-notifications") do
54
- render("lesli/partials/application-lesli-notifications")
55
- end
56
- end
57
-
58
40
  def render_form_stream(partial_path, locals = {})
59
41
 
60
42
  # Detect the current engine's namespace
@@ -32,6 +32,11 @@ 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
36
+
37
+ # Lesli-specific response status used to tell JSON clients that they
38
+ # must execute the action included in the response body.
39
+ ACTION_REQUIRED_STATUS = 490
35
40
 
36
41
  # Standard json structure for paginated data,
37
42
  # this is compatible for json, html and turbo responses
@@ -58,25 +63,43 @@ module Lesli
58
63
  }
59
64
  end
60
65
 
61
- # General response builder
62
- # Support for: turbo, json and html
63
- # Note: Turbo respond support single stream or array of streams
64
- # Usage:
65
- # respond_with(
66
- # turbo: [
67
- # stream_notification_success('ticket creado de forma exitosass')
68
- # stream_notification_warning('ticket creado de forma exitosass')
69
- # ],
70
- # json: {
71
- # message: "it works"
72
- # },
73
- # html: 'lesli/abouts/up'
74
- # )
75
- def respond_with_lesli(turbo:nil, json:nil, html:nil)
66
+ # Build a response for HTML, JSON, or Turbo Stream requests.
67
+ #
68
+ # `html` is passed to `render`. When omitted, Rails renders the current
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`.
72
+ def respond_with_lesli(
73
+ turbo: nil,
74
+ json: nil,
75
+ html: nil,
76
+ redirect: nil,
77
+ status: :ok,
78
+ redirect_status: :see_other
79
+ )
80
+ if html && redirect
81
+ raise ArgumentError, "Use either html: or redirect:, not both"
82
+ end
83
+
76
84
  respond_to do |format|
77
- format.html { render(html) }
78
- format.json { respond_with_json(json) }
79
- format.turbo_stream { render(turbo_stream: turbo) }
85
+ format.html do
86
+ if redirect
87
+ redirect_to(redirect, status: redirect_status)
88
+ elsif html
89
+ render(html, status: status)
90
+ else
91
+ render(status: status)
92
+ end
93
+ end
94
+
95
+ format.json { respond_with_http(status, json) }
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
80
103
  end
81
104
  end
82
105
 
@@ -95,12 +118,13 @@ module Lesli
95
118
  end
96
119
  end
97
120
 
98
- # General error for unauthorized request
99
- # Usage:
100
- # This method is automatically triggered by the
101
- # LesliShield authentication interface, however
102
- # you can use it anywhere by just invoking this method
103
- def respond_with_unauthorized(details = {})
121
+ # Respond with HTTP 403 when an authenticated user does not have the
122
+ # privileges required to perform the requested action.
123
+ #
124
+ # This method is used by LesliShield's authorization interface after
125
+ # authentication has succeeded. `respond_with_unauthorized` remains
126
+ # available as a backwards-compatible alias for existing engines.
127
+ def respond_with_forbidden(details = {})
104
128
 
105
129
  @error_object = {
106
130
  error_role: nil,
@@ -117,11 +141,13 @@ module Lesli
117
141
 
118
142
  respond_to do |format|
119
143
  format.json { respond_with_json_unauthorized(@error_object) }
120
- format.html { render('lesli/errors/unauthorized', status: :unauthorized) }
121
- format.turbo_stream { render('lesli/errors/unauthorized', status: :unauthorized) }
144
+ format.html { render('lesli/errors/unauthorized', status: :forbidden) }
145
+ format.turbo_stream { render('lesli/errors/unauthorized', status: :forbidden) }
122
146
  end
123
147
  end
124
148
 
149
+ alias_method :respond_with_unauthorized, :respond_with_forbidden
150
+
125
151
 
126
152
  # ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
127
153
  # Auxiliar methods for responders
@@ -143,7 +169,7 @@ module Lesli
143
169
 
144
170
  # Return the stream object so you can call:
145
171
  # render turbo_stream: stream_notification_success("Done!")
146
- turbo_stream.update(
172
+ turbo_stream.replace(
147
173
  "application-lesli-notifications",
148
174
  partial: "lesli/partials/application-lesli-notifications"
149
175
  )
@@ -162,7 +188,7 @@ module Lesli
162
188
 
163
189
  # Just a simple json success response for JSON
164
190
  def respond_with_json(payload = nil)
165
- respond_with_http(200, payload)
191
+ respond_with_http(:ok, payload)
166
192
  end
167
193
 
168
194
  # Just a simple json not found response for JSON
@@ -185,19 +211,20 @@ module Lesli
185
211
  end
186
212
 
187
213
  # Just a simple http response for JSON
188
- def respond_with_http(status, payload)
189
- render(
190
- :status => status,
191
- :content_type => 'application/json',
192
- :json => payload.nil? ? "" : payload.to_json
193
- )
214
+ def respond_with_http(status, payload = nil)
215
+ return head(status) if payload.nil?
216
+
217
+ render(:status => status, :json => payload)
194
218
  end
195
219
 
196
- # General method to respond with and order for an action
197
- # This method exists just for compatibility poruposes,
198
- # a refactor is needed for this method
220
+ # Tell a JSON client to execute an action before it can continue.
221
+ # Clients using the Lesli API must handle ACTION_REQUIRED_STATUS and
222
+ # dispatch the action included in the response body.
199
223
  def respond_with_action(action, message = "Action Required")
200
- respond_with_http(490, { :message => message, :action => action })
224
+ respond_with_http(
225
+ ACTION_REQUIRED_STATUS,
226
+ { :message => message, :action => action }
227
+ )
201
228
  end
202
229
  end
203
230
  end
@@ -34,7 +34,12 @@ module Lesli
34
34
  class ApplicationLesliMailer < ActionMailer::Base
35
35
 
36
36
  # Set a dynamic template according to the engine that is sending the email
37
- default(template_path: Lesli.config.mailer.dig(:templates))
37
+ default(
38
+ template_path: (Lesli.config.mailer || {}).fetch(
39
+ :templates,
40
+ "lesli_assets/emails"
41
+ )
42
+ )
38
43
 
39
44
  #after_action :log_mail_requests
40
45
 
@@ -48,9 +53,16 @@ module Lesli
48
53
  end
49
54
 
50
55
  def build_url(path, params = {})
51
- default_url = Rails.application.config.action_mailer.default_url_options
56
+ default_url = mailer_url_options
52
57
  host = default_url[:host].to_s.sub(/\Ahttps?:\/\//, "")
53
- scheme = default_url[:protocol]&.delete(":") || "http"
58
+ scheme = default_url[:protocol].presence&.to_s&.delete(":") || "http"
59
+
60
+ if host.blank?
61
+ raise ArgumentError, <<~MESSAGE.squish
62
+ Action Mailer requires default_url_options[:host] to build
63
+ absolute links in Lesli emails
64
+ MESSAGE
65
+ end
54
66
 
55
67
  URI::Generic.build(
56
68
  scheme: scheme,
@@ -63,19 +75,32 @@ module Lesli
63
75
 
64
76
  protected
65
77
 
66
- def email(params={}, user:nil, to:, subject:, template_name:)
78
+ def email(
79
+ params = {},
80
+ user: nil,
81
+ to:,
82
+ subject:,
83
+ template_name:,
84
+ **headers
85
+ )
67
86
 
68
87
  @params = @params.merge(params)
69
88
 
70
89
  build_app_from_params(params)
71
90
 
72
91
  mail(
73
- to: to,
92
+ to: to,
74
93
  subject: subject,
94
+ **headers,
75
95
  template_name: template_name
76
96
  )
77
97
  end
78
98
 
99
+ def mailer_url_options
100
+ options = Rails.application.config.action_mailer.default_url_options
101
+ (options || {}).to_h.symbolize_keys
102
+ end
103
+
79
104
 
80
105
  private
81
106
 
@@ -88,11 +113,12 @@ module Lesli
88
113
  tag_line: "",
89
114
  }
90
115
 
91
- return if params[:user].blank? || params[:user].class.name != "Lesli::User"
116
+ account = params[:user]&.account
117
+ return if account.blank?
92
118
 
93
119
  @app[:company] = {
94
- id: params[:user].account.id,
95
- name: params[:user].account.name,
120
+ id: account.id,
121
+ name: account.name,
96
122
  #tag_line: params[:user].account.company_tag_line,
97
123
  }
98
124
  end
@@ -34,46 +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 })
43
- .find_or_create_by(:name => "owner")
44
-
45
- # platform role for administrators
46
- admin = self.roles
47
- .create_with({ permission_level: 100000})
48
- .find_or_create_by(name: "admin")
49
-
50
- # platform role for guests
51
- guest = self.roles
52
- .create_with({ permission_level: 10, 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
-
68
- LesliSystem.engines.each do |engine, data|
69
-
70
- next if ["Lesli", "LesliBabel", "Root"].include?(engine)
71
-
72
- # Create an associated account if the attribute is blank
73
- engine.constantize::Account.find_or_create_by!(account: self)
74
-
75
- self.log(:account_initialization, "Engine: #{engine} initialized successfully")
76
- end
77
- end
44
+ AccountOperator.new(account: self).initialize_engines!
45
+ end
78
46
  end
79
47
  end
@@ -35,25 +35,14 @@ module Lesli
35
35
  extend ActiveSupport::Concern
36
36
 
37
37
  def log operation, description=nil, user:nil
38
- return unless defined?(LesliAudit)
39
- log_anonimous(operation, description) unless self.audit
40
- log_account(operation, description) if self.audit
41
- end
42
-
43
- private
44
-
45
- def log_anonimous operation, description
46
- LesliAudit::AccountLog.create({
47
- operation: operation,
48
- description: description
49
- })
50
- end
38
+ return unless defined?(LesliAudit::ActivityAudit)
51
39
 
52
- def log_account operation, description
53
- self.logs.create!({
40
+ LesliAudit::ActivityAudit.record_account!(
41
+ account: self,
42
+ actor: user,
54
43
  operation: operation,
55
44
  description: description
56
- })
45
+ )
57
46
  end
58
47
  end
59
48
  end