lesli 5.1.13 → 5.1.15

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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/lesli/application.css +527 -10
  3. data/app/controllers/lesli/application_lesli_controller.rb +27 -0
  4. data/app/controllers/lesli/items/attachments_controller.rb +3 -3
  5. data/app/controllers/lesli/items/discussions_controller.rb +2 -2
  6. data/app/controllers/lesli/items/tasks_controller.rb +2 -2
  7. data/app/controllers/lesli/roles_controller.rb +1 -6
  8. data/app/controllers/lesli/users_controller.rb +1 -5
  9. data/app/helpers/lesli/html_helper.rb +5 -5
  10. data/app/helpers/lesli/navigation_helper.rb +5 -5
  11. data/app/interfaces/lesli/customization_interface.rb +11 -31
  12. data/app/interfaces/lesli/responder_interface.rb +11 -4
  13. data/app/models/concerns/lesli/account_initializer.rb +5 -45
  14. data/app/models/concerns/lesli/user_extensions.rb +4 -2
  15. data/app/models/lesli/items/attachment.rb +3 -3
  16. data/app/models/lesli/role.rb +14 -2
  17. data/app/models/lesli/user.rb +7 -0
  18. data/app/operators/lesli/account_operator.rb +84 -0
  19. data/app/operators/lesli/resource_operator.rb +123 -0
  20. data/app/services/lesli/application_lesli_service.rb +33 -116
  21. data/app/services/lesli/resource_service.rb +12 -159
  22. data/app/services/lesli/role_service.rb +11 -229
  23. data/app/services/lesli/user_service.rb +37 -117
  24. data/app/views/lesli/abouts/welcome.html.erb +4 -4
  25. data/app/views/lesli/apps/show.html.erb +1 -1
  26. data/app/views/lesli/errors/not_found.html.erb +9 -16
  27. data/app/views/lesli/errors/unauthorized.html.erb +19 -28
  28. data/app/views/lesli/layouts/application-devise.html.erb +1 -0
  29. data/app/views/lesli/layouts/application-lesli.html.erb +1 -1
  30. data/app/views/lesli/partials/_application-lesli-announcements.html.erb +18 -0
  31. data/app/views/lesli/partials/_application-lesli-assets.html.erb +16 -19
  32. data/app/views/lesli/partials/_application-lesli-header.html.erb +45 -100
  33. data/app/views/lesli/partials/_application-lesli-notifications.html.erb +2 -27
  34. data/app/views/lesli/partials/_application-lesli-turbo.html.erb +5 -0
  35. data/app/views/lesli/shared/dashboards/edit.html.erb +1 -1
  36. data/app/views/lesli/shared/dashboards/show.html.erb +1 -1
  37. data/config/initializers/lesli.rb +7 -2
  38. data/config/locales/translations.en.yml +56 -48
  39. data/config/locales/translations.es.yml +54 -45
  40. data/db/migrate/v1/0000000210_create_lesli_users.rb +1 -0
  41. data/db/seed/xyz.rb +1 -3
  42. data/lib/generators/lesli/base_generator.rb +41 -3
  43. data/lib/generators/lesli/controller/controller_generator.rb +1 -1
  44. data/lib/generators/lesli/controller/templates/controller.rb.tt +12 -7
  45. data/lib/generators/lesli/model/model_generator.rb +1 -1
  46. data/lib/generators/lesli/resource/resource_generator.rb +290 -0
  47. data/lib/generators/lesli/resource/templates/controller.rb.tt +99 -0
  48. data/lib/generators/lesli/resource/templates/locale.en.yml.tt +13 -0
  49. data/lib/generators/lesli/resource/templates/migration.rb.tt +28 -0
  50. data/lib/generators/lesli/resource/templates/model.rb.tt +38 -0
  51. data/lib/generators/lesli/resource/templates/operator.rb.tt +15 -0
  52. data/lib/generators/lesli/resource/templates/service.rb.tt +52 -0
  53. data/lib/generators/lesli/resource/templates/tests/controller_test.rb.tt +14 -0
  54. data/lib/generators/lesli/resource/templates/tests/model_test.rb.tt +24 -0
  55. data/lib/generators/lesli/resource/templates/tests/operator_test.rb.tt +15 -0
  56. data/lib/generators/lesli/resource/templates/tests/service_test.rb.tt +17 -0
  57. data/lib/generators/lesli/resource/templates/views/_form.html.erb.tt +17 -0
  58. data/lib/generators/lesli/resource/templates/views/edit.html.erb.tt +4 -0
  59. data/lib/generators/lesli/resource/templates/views/index.html.erb.tt +22 -0
  60. data/lib/generators/lesli/resource/templates/views/new.html.erb.tt +4 -0
  61. data/lib/generators/lesli/resource/templates/views/show.html.erb.tt +17 -0
  62. data/lib/generators/lesli/scaffold/scaffold_generator.rb +4 -39
  63. data/lib/generators/lesli/service/service_generator.rb +1 -1
  64. data/lib/generators/lesli/service/templates/service.rb.tt +15 -7
  65. data/lib/generators/lesli/views/views_generator.rb +1 -1
  66. data/lib/generators/lesli_generator.rb +11 -0
  67. data/lib/lesli/courier.rb +66 -34
  68. data/lib/lesli/database_tasks.rb +145 -0
  69. data/lib/lesli/version.rb +2 -2
  70. data/lib/tasks/lesli/db.rake +16 -93
  71. data/lib/tasks/lesli/resources.rake +1 -1
  72. data/readme.md +3 -1
  73. metadata +23 -4
  74. data/app/views/lesli/partials/_application-lesli-footer.html.erb +0 -82
  75. data/app/views/lesli/partials/_application-lesli-sidebar.html.erb +0 -50
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= controller_class_name %>Controller < ApplicationController
5
+ before_action :<%= set_resource_method_name %>, only: %i[show edit update destroy]
6
+
7
+ def index
8
+ <%= resource_collection_instance %> = respond_with_pagination(
9
+ <%= service_class_name %>.new(current_user, query).index
10
+ )
11
+ end
12
+
13
+ def show
14
+ <%= resource_instance %> = <%= resource_instance %>_service.show
15
+ end
16
+
17
+ def new
18
+ <%= resource_instance %> = <%= resource_class_name %>.new
19
+ end
20
+
21
+ def edit
22
+ end
23
+
24
+ def create
25
+ service = <%= service_class_name %>.new(current_user, query).create(<%= params_method_name %>)
26
+ <%= resource_instance %> = service.result
27
+
28
+ if service.successful?
29
+ respond_with_lesli(
30
+ turbo: stream_redirection(<%= resource_name %>_path(<%= resource_instance %>)),
31
+ json: <%= resource_instance %>,
32
+ redirect: <%= resource_name %>_path(<%= resource_instance %>)
33
+ )
34
+ else
35
+ respond_with_lesli(
36
+ turbo: stream_notification_danger(service.errors_as_sentence),
37
+ json: { errors: service.errors },
38
+ html: :new,
39
+ status: :unprocessable_entity
40
+ )
41
+ end
42
+ end
43
+
44
+ def update
45
+ service = <%= resource_instance %>_service.update(<%= params_method_name %>)
46
+ <%= resource_instance %> = service.result
47
+
48
+ if service.successful?
49
+ respond_with_lesli(
50
+ turbo: stream_notification_success(t(".<%= resource_name %>_updated")),
51
+ json: <%= resource_instance %>,
52
+ redirect: <%= resource_name %>_path(<%= resource_instance %>)
53
+ )
54
+ else
55
+ respond_with_lesli(
56
+ turbo: stream_notification_danger(service.errors_as_sentence),
57
+ json: { errors: service.errors },
58
+ html: :edit,
59
+ status: :unprocessable_entity
60
+ )
61
+ end
62
+ end
63
+
64
+ def destroy
65
+ service = <%= resource_instance %>_service.destroy
66
+
67
+ if service.successful?
68
+ respond_with_lesli(
69
+ turbo: stream_redirection(<%= resource_collection_name %>_path),
70
+ json: nil,
71
+ redirect: <%= resource_collection_name %>_path
72
+ )
73
+ else
74
+ respond_with_lesli(
75
+ turbo: stream_notification_danger(service.errors_as_sentence),
76
+ json: { errors: service.errors },
77
+ status: :unprocessable_entity
78
+ )
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def <%= set_resource_method_name %>
85
+ <%= resource_instance %>_service = <%= service_class_name %>.new(current_user, query).find(id: params[:id])
86
+ return respond_with_not_found unless <%= resource_instance %>_service.found?
87
+
88
+ <%= resource_instance %> = <%= resource_instance %>_service.result
89
+ end
90
+
91
+ def <%= params_method_name %>
92
+ params.require(:<%= resource_name %>).permit(
93
+ <% permitted_attributes.each_with_index do |attribute, index| -%>
94
+ :<%= attribute %><%= "," unless index == permitted_attributes.length - 1 %>
95
+ <% end -%>
96
+ )
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,13 @@
1
+ en:
2
+ <%= engine_name %>:
3
+ <%= resource_collection_name %>:
4
+ index:
5
+ title: "<%= resource_collection_name.humanize %>"
6
+ show:
7
+ title: "<%= resource_class_name %>"
8
+ new:
9
+ title: "New <%= resource_class_name %>"
10
+ edit:
11
+ title: "Edit <%= resource_class_name %>"
12
+ update:
13
+ <%= resource_name %>_updated: "<%= resource_class_name %> updated successfully"
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= migration_class_name %> < ActiveRecord::Migration[8.1]
4
+ def change
5
+ create_table :<%= table_name %> do |t|
6
+ <% database_attributes.each do |attribute| -%>
7
+ <% if attribute.reference? -%>
8
+ t.references :<%= attribute.name %><%= attribute.inject_options %>
9
+ <% elsif attribute.password_digest? -%>
10
+ t.string :password_digest<%= attribute.inject_options %>
11
+ <% elsif attribute.token? -%>
12
+ t.string :<%= attribute.column_name %><%= attribute.inject_options %>
13
+ <% else -%>
14
+ t.<%= attribute.type %> :<%= attribute.column_name %><%= attribute.inject_options %>
15
+ <% end -%>
16
+ <% end -%>
17
+ t.datetime :deleted_at, index: true
18
+ t.timestamps
19
+ end
20
+
21
+ add_reference(
22
+ :<%= table_name %>,
23
+ :account,
24
+ null: false,
25
+ foreign_key: { to_table: :<%= account_table_name %> }
26
+ )
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= resource_class_name %> < Lesli::ApplicationLesliRecord
5
+ belongs_to :account, class_name: "<%= engine_namespace %>::Account"
6
+ <% attributes.select(&:reference?).each do |attribute| -%>
7
+ belongs_to :<%= attribute.name %>, optional: true
8
+ <% end -%>
9
+ <% attributes.select(&:rich_text?).each do |attribute| -%>
10
+ has_rich_text :<%= attribute.name %>
11
+ <% end -%>
12
+ <% attributes.select(&:attachment?).each do |attribute| -%>
13
+ has_one_attached :<%= attribute.name %>
14
+ <% end -%>
15
+ <% attributes.select(&:attachments?).each do |attribute| -%>
16
+ has_many_attached :<%= attribute.name %>
17
+ <% end -%>
18
+ <% if selected_items.any? -%>
19
+
20
+ # Shared resource items
21
+ <% selected_items.each do |item| -%>
22
+ include Lesli::Items::<%= item.camelize %>
23
+ <% end -%>
24
+
25
+ def lesli_item_account
26
+ account
27
+ end
28
+
29
+ def lesli_item_accessible_by?(user)
30
+ <% if engine_namespace == "Lesli" -%>
31
+ user.present? && account == user.account
32
+ <% else -%>
33
+ user.present? && account.account == user.account
34
+ <% end -%>
35
+ end
36
+ <% end -%>
37
+ end
38
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= engine_namespace %>
4
+ # Add explicit non-CRUD operations for <%= resource_class_name %> here.
5
+ class <%= operator_class_name %>
6
+ def initialize(<%= resource_name %>:, actor:)
7
+ @<%= resource_name %> = <%= resource_name %>
8
+ @actor = actor
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :<%= resource_name %>, :actor
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= service_class_name %> < Lesli::ApplicationLesliService
5
+ def find(id:)
6
+ super(resources.find_by(id: id))
7
+ end
8
+
9
+ def index
10
+ resources
11
+ .order(id: :desc)
12
+ .page(query.dig(:pagination, :page))
13
+ .per(query.dig(:pagination, :perPage))
14
+ end
15
+
16
+ def show
17
+ resource
18
+ end
19
+
20
+ def create(attributes)
21
+ self.resource = resources.new(attributes)
22
+
23
+ unless resource.save
24
+ error(resource.errors.full_messages.to_sentence)
25
+ end
26
+
27
+ self
28
+ end
29
+
30
+ def update(attributes)
31
+ unless resource.update(attributes)
32
+ error(resource.errors.full_messages.to_sentence)
33
+ end
34
+
35
+ self
36
+ end
37
+
38
+ def destroy
39
+ unless resource.destroy
40
+ error(resource.errors.full_messages.to_sentence)
41
+ end
42
+
43
+ self
44
+ end
45
+
46
+ private
47
+
48
+ def resources
49
+ <%= account_resource_scope %>
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ require "test_helper"
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= controller_class_name %>ControllerTest < ActiveSupport::TestCase
5
+ test "defines the complete resource routes" do
6
+ route_names = <%= engine_namespace %>::Engine.routes.routes.filter_map(&:name)
7
+
8
+ assert_includes route_names, "<%= resource_collection_name %>"
9
+ assert_includes route_names, "<%= resource_name %>"
10
+ assert_includes route_names, "new_<%= resource_name %>"
11
+ assert_includes route_names, "edit_<%= resource_name %>"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require "test_helper"
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= resource_class_name %>Test < ActiveSupport::TestCase
5
+ test "inherits from the shared Lesli record" do
6
+ assert_operator <%= resource_class_name %>, :<, Lesli::ApplicationLesliRecord
7
+ end
8
+
9
+ test "belongs to the local engine account" do
10
+ account = <%= resource_class_name %>.reflect_on_association(:account)
11
+
12
+ assert_equal :belongs_to, account&.macro
13
+ assert_equal "<%= engine_namespace %>::Account", account&.class_name
14
+ end
15
+ <% if selected_items.any? -%>
16
+
17
+ test "includes the configured shared items" do
18
+ <% selected_items.each do |item| -%>
19
+ assert_includes <%= resource_class_name %>.ancestors, Lesli::Items::<%= item.camelize %>
20
+ <% end -%>
21
+ end
22
+ <% end -%>
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require "test_helper"
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= operator_class_name %>Test < ActiveSupport::TestCase
5
+ test "is a plain Ruby resource operator" do
6
+ operator = <%= operator_class_name %>.new(
7
+ <%= resource_name %>: Object.new,
8
+ actor: Object.new
9
+ )
10
+
11
+ assert_instance_of <%= operator_class_name %>, operator
12
+ refute_operator <%= operator_class_name %>, :<, Lesli::ApplicationLesliService
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require "test_helper"
2
+
3
+ module <%= engine_namespace %>
4
+ class <%= service_class_name %>Test < ActiveSupport::TestCase
5
+ test "inherits from the Lesli service contract" do
6
+ assert_operator <%= service_class_name %>, :<, Lesli::ApplicationLesliService
7
+ end
8
+
9
+ test "implements the complete resource CRUD contract" do
10
+ operations = %i[find index show create update destroy]
11
+
12
+ operations.each do |operation|
13
+ assert_includes <%= service_class_name %>.instance_methods(false), operation
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ <%%= form_with(model: <%= resource_instance %>, builder: LesliView::Forms::Builder) do |form| %>
2
+ <%%= form.fieldset do %>
3
+ <% form_attributes.each do |attribute| -%>
4
+ <% if attribute.rich_text? -%>
5
+ <%%= form.field_control_textarea_rich(:<%= attribute.name %>, label: false) %>
6
+ <% elsif attribute.type == :text -%>
7
+ <%%= form.field_control_textarea(:<%= attribute.name %>, label: false) %>
8
+ <% elsif attribute.reference? -%>
9
+ <%%= form.field_control_select(:<%= attribute.column_name %>, []) %>
10
+ <% elsif !attribute.attachment? && !attribute.attachments? -%>
11
+ <%%= form.field_control(:<%= attribute.column_name %>) %>
12
+ <% end -%>
13
+ <% end -%>
14
+
15
+ <%%= form.field_control_submit %>
16
+ <%% end %>
17
+ <%% end %>
@@ -0,0 +1,4 @@
1
+ <%%= render(LesliView::Layout::Container.new("edit-<%= resource_name %>")) do %>
2
+ <%%= render(LesliView::Components::Header.new(t("<%= engine_name %>.<%= resource_collection_name %>.edit.title"))) %>
3
+ <%%= render("form") %>
4
+ <%% end %>
@@ -0,0 +1,22 @@
1
+ <%% columns = [
2
+ <% if index_attributes.empty? -%>
3
+ { label: "ID", field: "id" }
4
+ <% else -%>
5
+ <% index_attributes.each_with_index do |attribute, index| -%>
6
+ { label: "<%= attribute.human_name %>", field: "<%= attribute.column_name %>" }<%= "," unless index == index_attributes.length - 1 %>
7
+ <% end -%>
8
+ <% end -%>
9
+ ] %>
10
+
11
+ <%%= render(LesliView::Layout::Container.new("<%= engine_domain_name %>-<%= resource_collection_name %>")) do %>
12
+ <%%= render(LesliView::Components::Header.new(
13
+ t("<%= engine_name %>.<%= resource_collection_name %>.index.title"),
14
+ new_path: new_<%= resource_name %>_path
15
+ )) %>
16
+ <%%= render(LesliView::Components::Toolbar.new) %>
17
+ <%%= render(LesliView::Elements::Table.new(
18
+ columns: columns,
19
+ records: <%= resource_collection_instance %>.dig(:records),
20
+ link: -> (<%= resource_name %>) { <%= resource_name %>_path(<%= resource_name %>) }
21
+ )) %>
22
+ <%% end %>
@@ -0,0 +1,4 @@
1
+ <%%= render(LesliView::Layout::Container.new("new-<%= resource_name %>")) do %>
2
+ <%%= render(LesliView::Components::Header.new(t("<%= engine_name %>.<%= resource_collection_name %>.new.title"))) %>
3
+ <%%= render("form") %>
4
+ <%% end %>
@@ -0,0 +1,17 @@
1
+ <%%= render(LesliView::Layout::Container.new("<%= engine_domain_name %>-<%= resource_collection_name %>-#{<%= resource_instance %>.id}")) do %>
2
+ <%%= render(LesliView::Components::Header.new(t("<%= engine_name %>.<%= resource_collection_name %>.show.title"))) %>
3
+ <% if selected_items.any? -%>
4
+ <%%= render(LesliView::Components::Tabs.new(active_tab: "<%= resource_name %>-information")) do |tabs| %>
5
+ <%% tabs.with_tab(id: "<%= resource_name %>-information", title: "Information", icon: "info") do %>
6
+ <%%= render("form") %>
7
+ <%% end %>
8
+ <% selected_items.each do |item| -%>
9
+ <%% tabs.with_tab(id: "<%= resource_name %>-<%= item %>", title: "<%= item.humanize %>", icon: "<%= { "activities" => "search_activity", "attachments" => "attach_file", "discussions" => "forum", "tasks" => "checklist" }.fetch(item) %>") do %>
10
+ <%%= render(LesliView::Items::<%= item.camelize %>.new(<%= resource_instance %>)) %>
11
+ <%% end %>
12
+ <% end -%>
13
+ <%% end %>
14
+ <% else -%>
15
+ <%%= render("form") %>
16
+ <% end -%>
17
+ <%% end %>
@@ -1,47 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rails/generators"
4
- require_relative "../base_generator"
3
+ require_relative "../resource/resource_generator"
5
4
 
6
5
  module Lesli
7
6
  module Generators
8
- # Main Lesli scaffold generator.
9
- #
10
- # This generator delegates work to the specialized generators.
11
- class ScaffoldGenerator < BaseGenerator
12
- argument :attributes, type: :array, default: [], banner: "field:type field:type"
13
-
14
- def invoke_model_generator
15
- invoke "lesli:model", [name, *attributes]
16
- end
17
-
18
- # Invokes the custom Lesli controller generator.
19
- def invoke_controller_generator
20
- invoke "lesli:controller", [name, *attributes]
21
- end
22
-
23
- # Invokes the custom Lesli service generator.
24
- def invoke_service_generator
25
- invoke "lesli:service", [name, *attributes]
26
- end
27
-
28
- def invoke_views_generator
29
- invoke "lesli:views", [name, *attributes]
30
- end
31
-
32
- def invoke_migration_generator
33
- invoke "active_record:migration", [migration_name, *attributes]
34
- end
35
-
36
- def invoke_route_generator
37
- invoke "resource_route", [resource_collection_name]
38
- end
39
-
40
- private
41
-
42
- def migration_name
43
- "create_#{engine_name}_#{resource_collection_name}"
44
- end
7
+ # Backwards-compatible name for the complete Lesli resource generator.
8
+ class ScaffoldGenerator < ResourceGenerator
9
+ source_root ResourceGenerator.source_root
45
10
  end
46
11
  end
47
12
  end
@@ -25,7 +25,7 @@ module Lesli
25
25
  # app/services/my_engine
26
26
  # app/services/store
27
27
  def service_directory
28
- File.join(ENGINE_ROOT, "app/services", engine_name)
28
+ engine_root.join("app/services", engine_name)
29
29
  end
30
30
 
31
31
  # Returns the final service file path.
@@ -9,8 +9,9 @@ module <%= engine_namespace %>
9
9
  # Build the search hash using only present values.
10
10
  params = { id: id, uid: uid }.compact
11
11
 
12
- # Only perform the lookup when at least one filter is present.
13
- super(scope.find_by(params)) if params.any?
12
+ # An empty search must not return the first record in the account.
13
+ resource = scope.find_by(params) if params.any?
14
+ super(resource)
14
15
  end
15
16
 
16
17
  def index
@@ -19,10 +20,9 @@ module <%= engine_namespace %>
19
20
 
20
21
  def create(<%= params_method_name %>)
21
22
  <%= resource_name %> = current_user.account.<%= engine_domain_name %>.<%= resource_collection_name %>.new(<%= params_method_name %>)
23
+ self.resource = <%= resource_name %>
22
24
 
23
- if <%= resource_name %>.save
24
- self.resource = <%= resource_name %>
25
- else
25
+ unless <%= resource_name %>.save
26
26
  self.error(<%= resource_name %>.errors.full_messages.to_sentence)
27
27
  end
28
28
 
@@ -34,11 +34,19 @@ module <%= engine_namespace %>
34
34
  end
35
35
 
36
36
  def update(params)
37
- self.resource.update(params)
37
+ unless resource.update(params)
38
+ error(resource.errors.full_messages.to_sentence)
39
+ end
40
+
41
+ self
38
42
  end
39
43
 
40
44
  def destroy
41
- self.resource.destroy
45
+ unless resource.destroy
46
+ error(resource.errors.full_messages.to_sentence)
47
+ end
48
+
49
+ self
42
50
  end
43
51
  end
44
52
  end
@@ -54,7 +54,7 @@ module Lesli
54
54
  # Example:
55
55
  # app/views/lesli_support/tickets
56
56
  def views_directory
57
- File.join(ENGINE_ROOT, "app/views", engine_name, resource_collection_name)
57
+ engine_root.join("app/views", engine_name, resource_collection_name)
58
58
  end
59
59
 
60
60
  # Returns only scalar-like attributes that can be rendered
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lesli/resource/resource_generator"
4
+
5
+ # Provides the concise `rails generate lesli RESOURCE` command while the
6
+ # implementation remains available as `lesli:resource`.
7
+ class LesliGenerator < Lesli::Generators::ResourceGenerator
8
+ namespace "lesli"
9
+ source_root Lesli::Generators::ResourceGenerator.source_root
10
+ desc "Generate a complete account-scoped resource inside a Lesli engine"
11
+ end
data/lib/lesli/courier.rb CHANGED
@@ -33,55 +33,87 @@ Building a better future, one line of code at a time.
33
33
  module Lesli
34
34
  class Courier
35
35
 
36
- # Store the module_name (e.g., :lesli_support as LesliSupport)
37
- def initialize(module_name, on_error=nil)
36
+ # Configure the optional engine and its error behavior.
37
+ #
38
+ # module_name: engine module name as a String or Symbol.
39
+ # fallback: value returned when the engine is unavailable or an error is handled.
40
+ # fallback_on_error: return the fallback instead of raising execution errors.
41
+ def initialize(module_name, fallback=nil, fallback_on_error: false)
38
42
  @module_name = module_name.to_s.camelize
39
- @on_error = on_error
43
+ @fallback = fallback
44
+ @fallback_on_error = fallback_on_error
40
45
  end
41
46
 
42
- # Dynamically determine the class to call (e.g., :ticket_service as TicketService)
43
- def from(service_name, *args)
44
- @service_params = args
47
+ # Select the class to instantiate (e.g., :ticket_service as TicketService),
48
+ # store its constructor arguments, and return self to keep the call chainable.
49
+ #
50
+ # service_name: class name as a String or Symbol.
51
+ # *args: positional arguments passed to the class constructor.
52
+ # **kwargs: keyword arguments passed to the class constructor.
53
+ def from(service_name, *args, **kwargs)
54
+ @service_args = args
55
+ @service_kwargs = kwargs
45
56
  @service_name = service_name.to_s.camelize
46
57
  self
47
58
  end
48
59
 
49
- # Store the method name to call on the service (e.g., :index_with_deadline)
50
- def call(method_name, *args)
51
- @method_params = args
60
+ # Select and execute a public method on the configured class instance.
61
+ #
62
+ # method_name: method name as a String or Symbol.
63
+ # *args: positional arguments passed to the method.
64
+ # **kwargs: keyword arguments passed to the method.
65
+ # Returns the value returned by the invoked method.
66
+ def call(method_name, *args, **kwargs)
67
+ @method_args = args
68
+ @method_kwargs = kwargs
52
69
  @method_name = method_name
53
- self.execute
70
+ execute
54
71
  end
55
72
 
56
- private
73
+ private
57
74
 
58
- # Execute the final call to the service, either with or without params
75
+ # The engine is always optional. Errors from an installed engine remain
76
+ # visible unless the caller explicitly enables fallback_on_error.
59
77
  def execute
78
+ # Resolve the configured engine name as a top-level Ruby module.
79
+ engine_module = resolve_constant(Object, @module_name)
80
+
81
+ # Return the configured fallback when the optional engine is not installed.
82
+ return fallback unless engine_module
83
+
84
+ # Resolve the configured class directly inside the engine module.
85
+ service_class = engine_module.const_get(@service_name, false)
86
+
87
+ # Instantiate the class with the arguments previously stored by `from`.
88
+ service = service_class.new(*@service_args, **@service_kwargs)
89
+
90
+ # Invoke the public method configured by `call` and return its result.
91
+ service.public_send(@method_name, *@method_args, **@method_kwargs)
92
+ rescue StandardError => error
93
+ # Re-raise by default so integration and implementation errors remain visible.
94
+ raise unless @fallback_on_error
95
+
96
+ # Record handled errors without interrupting the caller's execution.
97
+ Rails.error.report(
98
+ error,
99
+ handled: true,
100
+ context: {
101
+ module_name: @module_name,
102
+ service_name: @service_name,
103
+ method_name: @method_name
104
+ }
105
+ )
106
+
107
+ # Allow optional integrations to fail without breaking the calling feature.
108
+ fallback
109
+ end
60
110
 
61
- module_name = "#{@module_name}"
62
-
63
- # Look up the module and class dynamically
64
- raise unless Object.const_defined?(module_name)
65
-
66
- module_service_name = "#{@module_name}::#{@service_name}"
67
-
68
- raise unless Object.const_defined?(module_service_name)
69
-
70
- # Create a reference of the module service object
71
- module_service_class = module_service_name.constantize
72
-
73
- # Instantiate the service class with or without arguments
74
- module_service_instance = module_service_class.new(*@service_params.to_a)
75
-
76
- # Check if the method is defined on the service instance
77
- raise unless module_service_instance.respond_to?(@method_name)
111
+ attr_reader :fallback
78
112
 
79
- # Dynamically call the method on the service instance
80
- module_service_instance.send(@method_name, *@method_params)
113
+ def resolve_constant(namespace, constant_name)
114
+ return unless namespace.const_defined?(constant_name, false)
81
115
 
82
- rescue => e
83
- ## pp e add a debug message
84
- return @on_error
116
+ namespace.const_get(constant_name, false)
85
117
  end
86
118
  end
87
119
  end