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
@@ -1,52 +1,60 @@
1
1
  ---
2
- :en:
2
+ en:
3
3
  lesli:
4
- items:
5
- activities:
6
- empty: No activities have been recorded yet.
7
- unknown_actor: Unknown user
8
- discussions:
9
- add: Add comment
10
- created: Comment added successfully
11
- empty: No comments have been added yet.
12
- invalid: Comment could not be added
13
- unknown_author: Unknown user
14
- tasks:
15
- add: Add task
16
- complete_action: Mark task as complete
17
- completed: Task completed successfully
18
- created: Task created successfully
19
- empty: No tasks have been added yet.
20
- reopen_action: Mark task as incomplete
21
- reopened: Task reopened successfully
22
- attachments:
23
- add: Add file
24
- category: Category
25
- choose_file: Choose a file
26
- created: File uploaded successfully
27
- deleted: File removed successfully
28
- download: Download
29
- empty: No files have been attached yet.
30
- label: Label
31
- remove: Remove
32
- confirm_remove: Remove this file?
33
- uploading: Uploading...
34
- errors:
35
- required: must be attached
36
- too_large: must be smaller than %{size}
37
- unsupported_type: has an unsupported file type
4
+ announcements:
5
+ title: "Announcements"
6
+ open_link: "View details"
7
+ dismiss: "Dismiss announcement"
8
+ categories_info: "Information"
9
+ categories_success: "Success"
10
+ categories_warning: "Warning"
11
+ categories_danger: "Important notice"
12
+ abouts: {}
13
+ apps: {}
38
14
  header:
39
- applications: Applications
40
- dashboard: Lesli Dashboard
41
- logout: Logout
42
- notifications: Notifications
43
- notifications_unread:
44
- one: "%{count} unread notification"
45
- other: "%{count} unread notifications"
46
- profile: Profile
15
+ applications: "Applications"
16
+ dashboard: "Lesli Dashboard"
17
+ logout: "Logout"
18
+ notifications: "Notifications"
19
+ notifications_unread_one: "%{count} unread notification"
20
+ notifications_unread_other: "%{count} unread notifications"
21
+ profile: "Profile"
22
+ items:
23
+ activities_empty: "No activities have been recorded yet."
24
+ activities_unknown_actor: "Unknown user"
25
+ attachments_add: "Add file"
26
+ attachments_category: "Category"
27
+ attachments_choose_file: "Choose a file"
28
+ attachments_confirm_remove: "Remove this file?"
29
+ attachments_created: "File uploaded successfully"
30
+ attachments_deleted: "File removed successfully"
31
+ attachments_download: "Download"
32
+ attachments_empty: "No files have been attached yet."
33
+ attachments_errors_required: "must be attached"
34
+ attachments_errors_too_large: "must be smaller than %{size}"
35
+ attachments_errors_unsupported_type: "has an unsupported file type"
36
+ attachments_label: "Label"
37
+ attachments_remove: "Remove"
38
+ attachments_uploading: "Uploading..."
39
+ discussions_add: "Add comment"
40
+ discussions_created: "Comment added successfully"
41
+ discussions_empty: "No comments have been added yet."
42
+ discussions_invalid: "Comment could not be added"
43
+ discussions_unknown_author: "Unknown user"
44
+ tasks_add: "Add task"
45
+ tasks_complete_action: "Mark task as complete"
46
+ tasks_completed: "Task completed successfully"
47
+ tasks_created: "Task created successfully"
48
+ tasks_empty: "No tasks have been added yet."
49
+ tasks_reopen_action: "Mark task as incomplete"
50
+ tasks_reopened: "Task reopened successfully"
51
+ resources: {}
47
52
  shared:
48
- message_error_not_found: Not found
49
- message_error_resource_not_found: The resource you are looking for was not found.
50
- message_error_unauthorized: Unauthorized
51
- message_error_unauthorized_credentials: 'We could not validate your privileges.
52
- Please ask the administrator to send you an invite to see this content. '
53
+ message_error_not_found: "Not found"
54
+ message_error_resource_not_found: "The resource you are looking for was not found."
55
+ message_error_unauthorized: "Unauthorized"
56
+ message_error_unauthorized_credentials: "We could not validate your privileges. Please ask the administrator to send you an invite to see this content. "
57
+ users/confirmations: {}
58
+ users/passwords: {}
59
+ users/registrations: {}
60
+ users/sessions: {}
@@ -1,51 +1,60 @@
1
1
  ---
2
- :es:
2
+ es:
3
3
  lesli:
4
- items:
5
- activities:
6
- empty: Todavía no se ha registrado actividad.
7
- unknown_actor: Usuario desconocido
8
- discussions:
9
- add: Agregar comentario
10
- created: Comentario agregado exitosamente
11
- empty: Todavía no se han agregado comentarios.
12
- invalid: No se pudo agregar el comentario
13
- unknown_author: Usuario desconocido
14
- tasks:
15
- add: Agregar tarea
16
- complete_action: Marcar tarea como completada
17
- completed: Tarea completada exitosamente
18
- created: Tarea creada exitosamente
19
- empty: Todavía no se han agregado tareas.
20
- reopen_action: Marcar tarea como pendiente
21
- reopened: Tarea reabierta exitosamente
22
- attachments:
23
- add: Agregar archivo
24
- category: Categoría
25
- choose_file: Seleccionar archivo
26
- created: Archivo subido exitosamente
27
- deleted: Archivo eliminado exitosamente
28
- download: Descargar
29
- empty: Todavía no se han adjuntado archivos.
30
- label: Etiqueta
31
- remove: Eliminar
32
- confirm_remove: ¿Eliminar este archivo?
33
- uploading: Subiendo...
34
- errors:
35
- required: debe ser adjuntado
36
- too_large: debe ser menor de %{size}
37
- unsupported_type: tiene un tipo de archivo no permitido
4
+ announcements:
5
+ title: "Anuncios"
6
+ open_link: "Ver detalles"
7
+ dismiss: "Cerrar anuncio"
8
+ categories_info: "Información"
9
+ categories_success: "Éxito"
10
+ categories_warning: "Advertencia"
11
+ categories_danger: "Aviso importante"
12
+ abouts: {}
13
+ apps: {}
38
14
  header:
39
- applications: Aplicaciones
40
- dashboard: Panel de Lesli
41
- logout: Cerrar sesión
42
- notifications: Notificaciones
43
- notifications_unread:
44
- one: "%{count} notificación sin leer"
45
- other: "%{count} notificaciones sin leer"
46
- profile: Perfil
15
+ applications: "Aplicaciones"
16
+ dashboard: "Panel de Lesli"
17
+ logout: "Cerrar sesión"
18
+ notifications: "Notificaciones"
19
+ notifications_unread_one: "%{count} notificación sin leer"
20
+ notifications_unread_other: "%{count} notificaciones sin leer"
21
+ profile: "Perfil"
22
+ items:
23
+ activities_empty: "Todavía no se ha registrado actividad."
24
+ activities_unknown_actor: "Usuario desconocido"
25
+ attachments_add: "Agregar archivo"
26
+ attachments_category: "Categoría"
27
+ attachments_choose_file: "Seleccionar archivo"
28
+ attachments_confirm_remove: "¿Eliminar este archivo?"
29
+ attachments_created: "Archivo subido exitosamente"
30
+ attachments_deleted: "Archivo eliminado exitosamente"
31
+ attachments_download: "Descargar"
32
+ attachments_empty: "Todavía no se han adjuntado archivos."
33
+ attachments_errors_required: "debe ser adjuntado"
34
+ attachments_errors_too_large: "debe ser menor de %{size}"
35
+ attachments_errors_unsupported_type: "tiene un tipo de archivo no permitido"
36
+ attachments_label: "Etiqueta"
37
+ attachments_remove: "Eliminar"
38
+ attachments_uploading: "Subiendo..."
39
+ discussions_add: "Agregar comentario"
40
+ discussions_created: "Comentario agregado exitosamente"
41
+ discussions_empty: "Todavía no se han agregado comentarios."
42
+ discussions_invalid: "No se pudo agregar el comentario"
43
+ discussions_unknown_author: "Usuario desconocido"
44
+ tasks_add: "Agregar tarea"
45
+ tasks_complete_action: "Marcar tarea como completada"
46
+ tasks_completed: "Tarea completada exitosamente"
47
+ tasks_created: "Tarea creada exitosamente"
48
+ tasks_empty: "Todavía no se han agregado tareas."
49
+ tasks_reopen_action: "Marcar tarea como pendiente"
50
+ tasks_reopened: "Tarea reabierta exitosamente"
51
+ resources: {}
47
52
  shared:
48
53
  message_error_not_found: ":lesli.shared.message_error_not_found:"
49
- message_error_resource_not_found: El recurso que estás buscando no fue encontrado
50
- message_error_unauthorized: No autorizado
54
+ message_error_resource_not_found: "El recurso que estás buscando no fue encontrado"
55
+ message_error_unauthorized: "No autorizado"
51
56
  message_error_unauthorized_credentials: ":lesli.shared.message_error_unauthorized_credentials:"
57
+ users/confirmations: {}
58
+ users/passwords: {}
59
+ users/registrations: {}
60
+ users/sessions: {}
@@ -46,6 +46,7 @@ class CreateLesliUsers < ActiveRecord::Migration[7.2]
46
46
  t.string :salutation
47
47
  t.string :first_name
48
48
  t.string :last_name
49
+ t.string :time_zone
49
50
 
50
51
  # Authenticable
51
52
  t.string :email, null: false, default: ""
data/db/seed/xyz.rb CHANGED
@@ -32,9 +32,7 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  def create_account_user(account:, email:, role_name:, first_name:, last_name:, password: nil)
34
34
  user = Lesli::User.find_or_initialize_by(email: email)
35
- authentication_enabled =
36
- user.respond_to?(:password=) &&
37
- user.respond_to?(:password_confirmation=)
35
+ authentication_enabled = user.respond_to?(:password=) && user.respond_to?(:password_confirmation=)
38
36
  credentials_created = false
39
37
 
40
38
  Lesli::User.transaction do
@@ -15,9 +15,25 @@ module Lesli
15
15
  #
16
16
  # This avoids duplicating the same helper methods in every generator.
17
17
  class BaseGenerator < Rails::Generators::NamedBase
18
+ class_option :engine_root,
19
+ type: :string,
20
+ desc: "Target Rails engine root"
21
+
22
+ class_option :engine_namespace,
23
+ type: :string,
24
+ desc: "Target Rails engine Ruby namespace"
18
25
 
19
26
  private
20
27
 
28
+ def engine_root
29
+ configured_root = options[:engine_root]
30
+ configured_root = ENGINE_ROOT if configured_root.blank? && defined?(ENGINE_ROOT)
31
+
32
+ raise Thor::Error, "Lesli generators must run inside a Rails engine or receive --engine-root" if configured_root.blank?
33
+
34
+ Pathname.new(configured_root).expand_path
35
+ end
36
+
21
37
  # Returns the current engine folder name in snake_case.
22
38
  #
23
39
  # Examples:
@@ -25,7 +41,7 @@ module Lesli
25
41
  # "/code/my_engine" -> "my_engine"
26
42
  # "/code/store" -> "store"
27
43
  def engine_name
28
- File.basename(ENGINE_ROOT.to_s).underscore
44
+ engine_root.basename.to_s.underscore
29
45
  end
30
46
 
31
47
  # Returns the current engine Ruby namespace in CamelCase.
@@ -35,7 +51,7 @@ module Lesli
35
51
  # "my_engine" -> "MyEngine"
36
52
  # "store" -> "Store"
37
53
  def engine_namespace
38
- engine_name.camelize
54
+ options[:engine_namespace].presence || engine_name.camelize
39
55
  end
40
56
 
41
57
  # Returns the domain name derived from the engine namespace.
@@ -161,12 +177,34 @@ module Lesli
161
177
  def permitted_attributes
162
178
  return [] unless respond_to?(:attributes)
163
179
 
164
- attributes.map(&:name)
180
+ attributes.map(&:column_name)
165
181
  end
166
182
 
167
183
  def namespaced_model_name
168
184
  "#{engine_namespace}::#{resource_class_name}"
169
185
  end
186
+
187
+ def table_name
188
+ "#{engine_name}_#{resource_collection_name}"
189
+ end
190
+
191
+ def account_table_name
192
+ "#{engine_name}_accounts"
193
+ end
194
+
195
+ def account_model_path
196
+ engine_root.join("app/models", engine_name, "account.rb")
197
+ end
198
+
199
+ def routes_path
200
+ engine_root.join("config/routes.rb")
201
+ end
202
+
203
+ def account_resource_scope
204
+ return "current_user.account.#{resource_collection_name}" if engine_namespace == "Lesli"
205
+
206
+ "current_user.account.#{engine_domain_name}.#{resource_collection_name}"
207
+ end
170
208
  end
171
209
  end
172
210
  end
@@ -25,7 +25,7 @@ module Lesli
25
25
  # app/controllers/my_engine
26
26
  # app/controllers/store
27
27
  def controller_directory
28
- File.join(ENGINE_ROOT, "app/controllers", engine_name)
28
+ engine_root.join("app/controllers", engine_name)
29
29
  end
30
30
 
31
31
  # Returns the final controller file path.
@@ -11,7 +11,7 @@ module <%= engine_namespace %>
11
11
  end
12
12
 
13
13
  def show
14
- <%= resource_instance %> = <%= resource_instance %>.show
14
+ <%= resource_instance %> = <%= resource_instance %>_service.show
15
15
  end
16
16
 
17
17
  def new
@@ -40,13 +40,16 @@ module <%= engine_namespace %>
40
40
  end
41
41
 
42
42
  def update
43
- if <%= resource_instance %>.update(<%= params_method_name %>)
43
+ <%= resource_name %> = <%= resource_instance %>_service.update(<%= params_method_name %>)
44
+ <%= resource_instance %> = <%= resource_name %>.result
45
+
46
+ if <%= resource_name %>.successful?
44
47
  respond_with_lesli(
45
48
  turbo: stream_notification_success("<%= resource_class_name %> updated successfully")
46
49
  )
47
50
  else
48
51
  respond_with_lesli(
49
- turbo: stream_notification_danger(<%= resource_instance %>.errors)
52
+ turbo: stream_notification_danger(<%= resource_name %>.errors_as_sentence)
50
53
  )
51
54
  end
52
55
  end
@@ -55,20 +58,22 @@ module <%= engine_namespace %>
55
58
  return respond_with_not_found unless <%= resource_instance %>
56
59
  return respond_with_unauthorized unless <%= resource_instance %>.is_editable_by?(current_user)
57
60
 
58
- <%= resource_name %>_destroy_response = <%= service_class_name %>.destroy(current_user, <%= resource_instance %>)
61
+ <%= resource_name %>_destroy_response = <%= resource_instance %>_service.destroy
59
62
 
60
63
  if <%= resource_name %>_destroy_response.successful?
61
64
  respond_with_successful
62
65
  else
63
- respond_with_error(<%= resource_name %>_destroy_response.payload.errors.full_messages.to_sentence)
66
+ respond_with_error(<%= resource_name %>_destroy_response.errors_as_sentence)
64
67
  end
65
68
  end
66
69
 
67
70
  private
68
71
 
69
72
  def <%= set_resource_method_name %>
70
- <%= resource_instance %> = <%= service_class_name %>.new(current_user, query).find(id: params[:id])
71
- return respond_with_not_found unless <%= resource_instance %>.found?
73
+ <%= resource_instance %>_service = <%= service_class_name %>.new(current_user, query).find(id: params[:id])
74
+ return respond_with_not_found unless <%= resource_instance %>_service.found?
75
+
76
+ <%= resource_instance %> = <%= resource_instance %>_service.result
72
77
  end
73
78
 
74
79
  def <%= params_method_name %>
@@ -29,7 +29,7 @@ module Lesli
29
29
  # Example:
30
30
  # app/models/lesli_support
31
31
  def model_directory
32
- File.join(ENGINE_ROOT, "app/models", engine_name)
32
+ engine_root.join("app/models", engine_name)
33
33
  end
34
34
 
35
35
  # Returns the final path of the generated model file.
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require_relative "../base_generator"
5
+
6
+ module Lesli
7
+ module Generators
8
+ # Generates a complete account-scoped Lesli resource inside an engine.
9
+ class ResourceGenerator < BaseGenerator
10
+ ALLOWED_ITEMS = %w[activities attachments discussions tasks].freeze
11
+
12
+ desc "Generate a complete account-scoped resource inside a Lesli engine"
13
+
14
+ argument :attributes,
15
+ type: :array,
16
+ default: [],
17
+ banner: "field:type field:type"
18
+
19
+ class_option :skip_operator,
20
+ type: :boolean,
21
+ default: false,
22
+ desc: "Do not generate the resource operator"
23
+
24
+ class_option :skip_views,
25
+ type: :boolean,
26
+ default: false,
27
+ desc: "Do not generate HTML views"
28
+
29
+ class_option :skip_tests,
30
+ type: :boolean,
31
+ default: false,
32
+ desc: "Do not generate tests"
33
+
34
+ class_option :items,
35
+ type: :array,
36
+ default: [],
37
+ desc: "Shared items to enable: activities attachments discussions tasks"
38
+
39
+ class_option :migration_namespace,
40
+ type: :numeric,
41
+ desc: "Two-digit Lesli migration feature namespace"
42
+
43
+ source_root File.expand_path("templates", __dir__)
44
+
45
+ def validate_engine
46
+ raise Thor::Error, "Missing engine account model: #{account_model_path}" unless account_model_path.file?
47
+ raise Thor::Error, "Missing engine routes file: #{routes_path}" unless routes_path.file?
48
+
49
+ unsupported_items = selected_items - ALLOWED_ITEMS
50
+ raise Thor::Error, "Unsupported Lesli items: #{unsupported_items.join(', ')}" if unsupported_items.any?
51
+
52
+ conflicting_migration = migration_with_same_version
53
+ return unless conflicting_migration
54
+
55
+ raise Thor::Error, <<~MESSAGE.squish
56
+ Migration version #{migration_version} already belongs to
57
+ #{conflicting_migration.basename}. Choose another --migration-namespace.
58
+ MESSAGE
59
+ end
60
+
61
+ def create_model
62
+ template "model.rb.tt", model_path
63
+ end
64
+
65
+ def create_migration
66
+ template "migration.rb.tt", migration_path
67
+ end
68
+
69
+ def create_service
70
+ template "service.rb.tt", service_path
71
+ end
72
+
73
+ def create_operator
74
+ return if options[:skip_operator]
75
+
76
+ template "operator.rb.tt", operator_path
77
+ end
78
+
79
+ def create_controller
80
+ template "controller.rb.tt", controller_path
81
+ end
82
+
83
+ def create_views
84
+ return if options[:skip_views]
85
+
86
+ template "views/_form.html.erb.tt", views_path.join("_form.html.erb")
87
+ template "views/index.html.erb.tt", views_path.join("index.html.erb")
88
+ template "views/show.html.erb.tt", views_path.join("show.html.erb")
89
+ template "views/new.html.erb.tt", views_path.join("new.html.erb")
90
+ template "views/edit.html.erb.tt", views_path.join("edit.html.erb")
91
+ end
92
+
93
+ def create_locale
94
+ template "locale.en.yml.tt", locale_path
95
+ end
96
+
97
+ def create_tests
98
+ return if options[:skip_tests]
99
+
100
+ template "tests/model_test.rb.tt", model_test_path
101
+ template "tests/service_test.rb.tt", service_test_path
102
+ template "tests/controller_test.rb.tt", controller_test_path
103
+ template "tests/operator_test.rb.tt", operator_test_path unless options[:skip_operator]
104
+ end
105
+
106
+ def add_account_association
107
+ association = <<~RUBY.indent(8)
108
+ has_many :#{resource_collection_name},
109
+ class_name: "#{namespaced_resource_class_name}",
110
+ foreign_key: :account_id,
111
+ dependent: :destroy
112
+ RUBY
113
+
114
+ update_class_file(account_model_path, "Account", association, "has_many :#{resource_collection_name}")
115
+ end
116
+
117
+ def add_resource_route
118
+ route = " resources :#{resource_collection_name}\n"
119
+ update_routes_file(route)
120
+ end
121
+
122
+ private
123
+
124
+ def update_class_file(path, class_name, content, marker)
125
+ existing_content = path.read
126
+ return if behavior != :revoke && existing_content.include?(marker)
127
+
128
+ inject_into_class path, class_name, content
129
+ end
130
+
131
+ def update_routes_file(route)
132
+ existing_content = routes_path.read
133
+ return if behavior != :revoke && existing_content.include?(route.strip)
134
+
135
+ inject_into_file routes_path, route, after: /routes\.draw do\s*\n/
136
+ end
137
+
138
+ def model_path
139
+ engine_root.join("app/models", engine_name, "#{resource_name}.rb")
140
+ end
141
+
142
+ def service_path
143
+ engine_root.join("app/services", engine_name, "#{resource_name}_service.rb")
144
+ end
145
+
146
+ def operator_path
147
+ engine_root.join("app/operators", engine_name, "#{resource_name}_operator.rb")
148
+ end
149
+
150
+ def controller_path
151
+ engine_root.join("app/controllers", engine_name, "#{resource_collection_name}_controller.rb")
152
+ end
153
+
154
+ def views_path
155
+ engine_root.join("app/views", engine_name, resource_collection_name)
156
+ end
157
+
158
+ def locale_path
159
+ engine_root.join("config/locales", "#{engine_name}.#{resource_name}.en.yml")
160
+ end
161
+
162
+ def model_test_path
163
+ engine_root.join("test/models", engine_name, "#{resource_name}_test.rb")
164
+ end
165
+
166
+ def service_test_path
167
+ engine_root.join("test/services", engine_name, "#{resource_name}_service_test.rb")
168
+ end
169
+
170
+ def operator_test_path
171
+ engine_root.join("test/operators", engine_name, "#{resource_name}_operator_test.rb")
172
+ end
173
+
174
+ def controller_test_path
175
+ engine_root.join("test/controllers", engine_name, "#{resource_collection_name}_controller_test.rb")
176
+ end
177
+
178
+ def migration_path
179
+ existing_resource_migration ||
180
+ migration_directory.join("#{migration_version}_create_#{table_name}.rb")
181
+ end
182
+
183
+ def existing_resource_migration
184
+ @existing_resource_migration ||= begin
185
+ pattern = engine_root.join("db/migrate/**/*_create_#{table_name}.rb")
186
+ path = Dir[pattern.to_s].sort.first
187
+ Pathname.new(path) if path
188
+ end
189
+ end
190
+
191
+ def migration_directory
192
+ account_migration_path.dirname
193
+ end
194
+
195
+ def account_migration_path
196
+ @account_migration_path ||= begin
197
+ pattern = engine_root.join("db/migrate/**/*_create_#{account_table_name}.rb")
198
+ path = Dir[pattern.to_s].sort.find do |candidate|
199
+ File.basename(candidate).match?(/\A\d{10}_/)
200
+ end
201
+
202
+ raise Thor::Error, "Could not find the versioned #{account_table_name} migration" unless path
203
+
204
+ Pathname.new(path)
205
+ end
206
+ end
207
+
208
+ def migration_version
209
+ @migration_version ||= begin
210
+ return existing_resource_migration.basename.to_s.first(10) if existing_resource_migration
211
+
212
+ "#{engine_migration_prefix}#{migration_namespace_code}0110"
213
+ end
214
+ end
215
+
216
+ def migration_with_same_version
217
+ pattern = engine_root.join("db/migrate/**/#{migration_version}_*.rb")
218
+
219
+ Dir[pattern.to_s].map { |path| Pathname.new(path) }.find do |path|
220
+ path != existing_resource_migration
221
+ end
222
+ end
223
+
224
+ def engine_migration_prefix
225
+ account_migration_path.basename.to_s.first(4)
226
+ end
227
+
228
+ def migration_namespace_code
229
+ namespace = options[:migration_namespace]&.to_i
230
+ namespace ||= next_migration_namespace
231
+
232
+ unless namespace.between?(1, 99)
233
+ raise Thor::Error, "Migration namespace must be between 1 and 99"
234
+ end
235
+
236
+ namespace.to_s.rjust(2, "0")
237
+ end
238
+
239
+ def next_migration_namespace
240
+ pattern = engine_root.join("db/migrate/**/*.rb")
241
+ namespaces = Dir[pattern.to_s].filter_map do |path|
242
+ version = File.basename(path)[/\A\d{10}/]
243
+ next unless version&.start_with?(engine_migration_prefix)
244
+
245
+ version[4, 2].to_i
246
+ end
247
+
248
+ [namespaces.max.to_i + 1, 1].max
249
+ end
250
+
251
+ def migration_class_name
252
+ "Create#{engine_name.camelize}#{resource_collection_name.camelize}"
253
+ end
254
+
255
+ def database_attributes
256
+ attributes.reject do |attribute|
257
+ attribute.virtual? || %w[account account_id deleted_at created_at updated_at].include?(attribute.name)
258
+ end
259
+ end
260
+
261
+ def selected_items
262
+ @selected_items ||= Array(options[:items]).map(&:to_s).map(&:downcase).uniq.sort
263
+ end
264
+
265
+ def item_enabled?(name)
266
+ selected_items.include?(name.to_s)
267
+ end
268
+
269
+ def form_attributes
270
+ attributes.reject { |attribute| attribute.name == "id" }
271
+ end
272
+
273
+ def index_attributes
274
+ form_attributes.reject(&:virtual?)
275
+ end
276
+
277
+ def operator_class_name
278
+ "#{resource_class_name}Operator"
279
+ end
280
+
281
+ def valid_attribute_pairs
282
+ attributes.filter_map do |attribute|
283
+ next if attribute.reference? || attribute.virtual?
284
+
285
+ [attribute.column_name, attribute.default]
286
+ end
287
+ end
288
+ end
289
+ end
290
+ end