decidim-core 0.31.7 → 0.31.9

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 (164) hide show
  1. checksums.yaml +4 -4
  2. data/app/cells/decidim/address_cell.rb +7 -3
  3. data/app/cells/decidim/card_metadata_cell.rb +3 -1
  4. data/app/cells/decidim/content_blocks/participatory_space_main_data/title.erb +1 -14
  5. data/app/cells/decidim/date_range/show.erb +6 -6
  6. data/app/cells/decidim/date_range_cell.rb +7 -2
  7. data/app/cells/decidim/following_cell.rb +1 -1
  8. data/app/cells/decidim/nav_links/show.erb +3 -1
  9. data/app/cells/decidim/participatory_space_private_user/show.erb +1 -1
  10. data/app/cells/decidim/share_text_widget/modal.erb +1 -0
  11. data/app/cells/decidim/share_widget/modal.erb +1 -0
  12. data/app/cells/decidim/share_widget/qr_code_modal.erb +2 -4
  13. data/app/commands/decidim/create_follow.rb +3 -7
  14. data/app/commands/decidim/create_omniauth_registration.rb +19 -7
  15. data/app/commands/decidim/create_user_report.rb +1 -1
  16. data/app/commands/decidim/destroy_account.rb +6 -6
  17. data/app/commands/decidim/destroy_ephemeral_user.rb +2 -0
  18. data/app/commands/decidim/invite_user.rb +1 -1
  19. data/app/commands/decidim/multiple_attachments_methods.rb +17 -3
  20. data/app/commands/decidim/update_resources_taxonomies.rb +2 -1
  21. data/app/controllers/concerns/decidim/devise_authentication_methods.rb +4 -0
  22. data/app/controllers/concerns/decidim/devise_controllers.rb +4 -0
  23. data/app/controllers/concerns/decidim/direct_upload.rb +12 -6
  24. data/app/controllers/concerns/decidim/disable_redirection_to_external_host.rb +2 -0
  25. data/app/controllers/concerns/decidim/filter_resource.rb +2 -0
  26. data/app/controllers/concerns/decidim/impersonate_users.rb +4 -4
  27. data/app/controllers/concerns/decidim/locale_switcher.rb +62 -11
  28. data/app/controllers/concerns/decidim/needs_organization.rb +15 -0
  29. data/app/controllers/concerns/decidim/needs_permission.rb +22 -10
  30. data/app/controllers/concerns/decidim/onboarding_action_methods.rb +2 -0
  31. data/app/controllers/concerns/decidim/paginable.rb +7 -3
  32. data/app/controllers/concerns/decidim/payload_info.rb +2 -0
  33. data/app/controllers/concerns/decidim/resource_versions_concern.rb +14 -0
  34. data/app/controllers/decidim/amendments_controller.rb +34 -6
  35. data/app/controllers/decidim/application_controller.rb +2 -6
  36. data/app/controllers/decidim/authorization_modals_controller.rb +3 -1
  37. data/app/controllers/decidim/components/base_controller.rb +2 -0
  38. data/app/controllers/decidim/devise/passwords_controller.rb +8 -0
  39. data/app/controllers/decidim/doorkeeper/authorizations_controller.rb +10 -4
  40. data/app/controllers/decidim/doorkeeper/credentials_controller.rb +6 -1
  41. data/app/controllers/decidim/favicon_controller.rb +9 -0
  42. data/app/controllers/decidim/free_resource_authorization_modals_controller.rb +2 -0
  43. data/app/controllers/decidim/gamification/badges_controller.rb +2 -0
  44. data/app/controllers/decidim/homepage_controller.rb +4 -1
  45. data/app/controllers/decidim/likes_controller.rb +1 -1
  46. data/app/controllers/decidim/links_controller.rb +2 -0
  47. data/app/controllers/decidim/manifests_controller.rb +2 -0
  48. data/app/controllers/decidim/messaging/conversations_controller.rb +11 -5
  49. data/app/controllers/decidim/newsletters_controller.rb +4 -0
  50. data/app/controllers/decidim/notifications_subscriptions_controller.rb +4 -0
  51. data/app/controllers/decidim/offline_controller.rb +5 -1
  52. data/app/controllers/decidim/open_data_controller.rb +5 -1
  53. data/app/controllers/decidim/pages_controller.rb +31 -3
  54. data/app/controllers/decidim/private_downloads_controller.rb +2 -0
  55. data/app/controllers/decidim/profiles_controller.rb +11 -1
  56. data/app/controllers/decidim/qr_controller.rb +3 -1
  57. data/app/controllers/decidim/resource_autocomplete_controller.rb +2 -0
  58. data/app/controllers/decidim/searches_controller.rb +2 -0
  59. data/app/controllers/decidim/short_links_controller.rb +4 -0
  60. data/app/controllers/decidim/static_map_controller.rb +2 -0
  61. data/app/controllers/decidim/timeouts_controller.rb +4 -0
  62. data/app/controllers/decidim/tos_controller.rb +2 -0
  63. data/app/controllers/decidim/upload_validations_controller.rb +4 -2
  64. data/app/controllers/decidim/user_activities_controller.rb +2 -0
  65. data/app/forms/decidim/upload_validation_form.rb +16 -10
  66. data/app/helpers/concerns/decidim/user_role_checker.rb +35 -10
  67. data/app/helpers/decidim/cells_paginate_helper.rb +0 -5
  68. data/app/helpers/decidim/layout_helper.rb +1 -0
  69. data/app/helpers/decidim/orders_helper.rb +8 -6
  70. data/app/helpers/decidim/paginate_helper.rb +27 -2
  71. data/app/helpers/decidim/sanitize_helper.rb +11 -1
  72. data/app/helpers/decidim/url_helper.rb +35 -0
  73. data/app/jobs/decidim/application_job.rb +4 -0
  74. data/app/jobs/decidim/open_data_job.rb +21 -5
  75. data/app/models/decidim/action_log.rb +7 -1
  76. data/app/models/decidim/authorization.rb +24 -0
  77. data/app/models/decidim/component.rb +10 -0
  78. data/app/models/decidim/like.rb +1 -1
  79. data/app/models/decidim/oauth_application.rb +1 -0
  80. data/app/models/decidim/user.rb +1 -0
  81. data/app/models/decidim/user_base_entity.rb +1 -1
  82. data/app/packs/src/decidim/callout.js +10 -0
  83. data/app/packs/src/decidim/callout.test.js +32 -0
  84. data/app/packs/src/decidim/controllers/accordion/accordion.test.js +64 -0
  85. data/app/packs/src/decidim/controllers/accordion/controller.js +43 -0
  86. data/app/packs/src/decidim/datepicker/datepicker_functions.js +54 -30
  87. data/app/packs/src/decidim/datepicker/form_datepicker.js +18 -2
  88. data/app/packs/src/decidim/datepicker/generate_datepicker.js +16 -8
  89. data/app/packs/src/decidim/datepicker/generate_timepicker.js +5 -3
  90. data/app/packs/src/decidim/datepicker/test/date.test.js +8 -0
  91. data/app/packs/src/decidim/datepicker/test/time.test.js +39 -24
  92. data/app/packs/src/decidim/utilities/dom.js +2 -2
  93. data/app/packs/stylesheets/decidim/_content_blocks.scss +4 -10
  94. data/app/packs/stylesheets/decidim/_conversations.scss +0 -8
  95. data/app/packs/stylesheets/decidim/_datepicker.scss +3 -3
  96. data/app/packs/stylesheets/decidim/_modal.scss +16 -8
  97. data/app/packs/stylesheets/decidim/_modal_share.scss +0 -22
  98. data/app/packs/stylesheets/decidim/_participatory_spaces.scss +11 -1
  99. data/app/packs/stylesheets/decidim/_rich_text.scss +12 -9
  100. data/app/packs/stylesheets/decidim/_spinner.scss +16 -0
  101. data/app/packs/stylesheets/decidim/_tom_select.scss +1 -1
  102. data/app/packs/stylesheets/decidim/_typography.scss +5 -2
  103. data/app/packs/stylesheets/decidim/legacy/email.scss +0 -5
  104. data/app/permissions/decidim/permissions.rb +20 -0
  105. data/app/presenters/decidim/admin_log/component_presenter.rb +1 -1
  106. data/app/presenters/decidim/log/base_presenter.rb +20 -2
  107. data/app/presenters/decidim/log/user_presenter.rb +2 -2
  108. data/app/serializers/decidim/exporters/open_data_blocked_user_serializer.rb +11 -2
  109. data/app/serializers/decidim/exporters/open_data_moderation_serializer.rb +25 -1
  110. data/app/services/decidim/action_logger.rb +97 -29
  111. data/app/services/decidim/svg_content_validator.rb +889 -0
  112. data/app/uploaders/decidim/application_uploader.rb +2 -2
  113. data/app/validators/uploader_image_content_validator.rb +282 -0
  114. data/app/views/decidim/application/_radio_accordion.html.erb +1 -5
  115. data/app/views/decidim/manifests/show.json.erb +25 -22
  116. data/app/views/decidim/messaging/conversations/create.js.erb +1 -3
  117. data/app/views/decidim/shared/_login_modal.html.erb +12 -14
  118. data/app/views/decidim/shared/_results_per_page.html.erb +1 -1
  119. data/app/views/layouts/decidim/_mailer_logo.html.erb +1 -1
  120. data/config/initializers/rack_attack.rb +10 -0
  121. data/config/locales/bg.yml +1 -4
  122. data/config/locales/ca-IT.yml +8 -4
  123. data/config/locales/ca.yml +10 -6
  124. data/config/locales/cs.yml +16 -4
  125. data/config/locales/de.yml +62 -6
  126. data/config/locales/en.yml +8 -4
  127. data/config/locales/es-MX.yml +8 -4
  128. data/config/locales/es-PY.yml +8 -4
  129. data/config/locales/es.yml +14 -10
  130. data/config/locales/eu.yml +165 -164
  131. data/config/locales/fi-plain.yml +3 -3
  132. data/config/locales/fi.yml +3 -3
  133. data/config/locales/fr-CA.yml +8 -4
  134. data/config/locales/fr.yml +8 -4
  135. data/config/locales/gl.yml +1440 -33
  136. data/config/locales/hu.yml +0 -1
  137. data/config/locales/it.yml +0 -2
  138. data/config/locales/ja.yml +4 -4
  139. data/config/locales/pl.yml +1 -4
  140. data/config/locales/pt-BR.yml +1 -4
  141. data/config/locales/ro-RO.yml +0 -3
  142. data/config/locales/sk.yml +51 -4
  143. data/config/locales/sv.yml +50 -13
  144. data/config/locales/tr-TR.yml +1 -3
  145. data/db/migrate/20250523104311_move_cta_to_hero_content_block.rb +1 -1
  146. data/db/migrate/20250527122040_move_highlighted_content_banner_settings_to_content_block.rb +3 -0
  147. data/db/migrate/20260909000000_add_failed_attempts_and_locked_at_to_decidim_authorizations.rb +8 -0
  148. data/db/migrate/20261007090000_make_user_optional_in_action_logs.rb +12 -0
  149. data/decidim-core.gemspec +1 -1
  150. data/lib/decidim/content_renderers/mention_resource_renderer.rb +6 -3
  151. data/lib/decidim/core/test/factories.rb +2 -2
  152. data/lib/decidim/core/test/shared_examples/participatory_space_members_page_examples.rb +77 -0
  153. data/lib/decidim/core/test/shared_examples/participatory_space_members_shared_examples.rb +85 -6
  154. data/lib/decidim/core/test/shared_examples/versions_controller_examples.rb +23 -1
  155. data/lib/decidim/core/version.rb +1 -1
  156. data/lib/decidim/core.rb +10 -1
  157. data/lib/decidim/has_upload_validations.rb +1 -0
  158. data/lib/decidim/legacy_form_builder.rb +4 -0
  159. data/lib/decidim/maintenance/import_models/scope.rb +2 -1
  160. data/lib/decidim/maintenance/taxonomy_importer.rb +4 -1
  161. data/lib/decidim/query_extensions.rb +2 -0
  162. data/lib/decidim/view_model.rb +1 -0
  163. data/lib/tasks/decidim_open_data_tasks.rake +7 -0
  164. metadata +22 -10
@@ -27,7 +27,10 @@
27
27
  }
28
28
 
29
29
  .title-decorator {
30
- @apply h2 md:h1 decorator;
30
+ @apply h2 md:h1 decorator break-normal;
31
+
32
+ hyphens: auto;
33
+ overflow-wrap: anywhere;
31
34
  }
32
35
 
33
36
  .text-with-links {
@@ -38,7 +41,7 @@
38
41
 
39
42
  /* default styles for those dynamic text blocks */
40
43
  .editor-content {
41
- @apply prose max-w-full text-gray-2 text-lg prose-a:text-secondary break-words prose-ul:pl-4;
44
+ @apply prose max-w-full text-gray-2 text-lg prose-a:text-secondary break-words prose-ul:pl-8 prose-ol:pl-8;
42
45
 
43
46
  iframe {
44
47
  @apply w-full block max-w-[600px] max-h-[70vh] h-[450px] my-10 mx-auto;
@@ -1725,7 +1725,6 @@ body.outlook p {
1725
1725
  table.body th.decidim-bar,
1726
1726
  table.body td.decidim-bar {
1727
1727
  padding: 10px 0;
1728
- background-color: #1a181d;
1729
1728
  }
1730
1729
 
1731
1730
  table.body {
@@ -1742,10 +1741,6 @@ table.body {
1742
1741
  height: 30px;
1743
1742
  }
1744
1743
 
1745
- .cityhall-bar {
1746
- background-color: #2c2930;
1747
- }
1748
-
1749
1744
  .cityhall-logo {
1750
1745
  width: 150px;
1751
1746
  margin-top: 16px;
@@ -9,6 +9,7 @@ module Decidim
9
9
  locales_action?
10
10
  component_public_action?
11
11
  search_scope_action?
12
+ public_content_action?
12
13
 
13
14
  public_report_content_action?
14
15
 
@@ -66,6 +67,23 @@ module Decidim
66
67
  toggle_allow([:search, :pick].include?(permission_action.action))
67
68
  end
68
69
 
70
+ # Public, non-resource content that any visitor is allowed to reach. The
71
+ # controllers using these subjects enforce the actual data visibility
72
+ # themselves (profile publication, private download tokens, newsletter
73
+ # tokens, etc.).
74
+ def public_content_action?
75
+ case permission_action.subject
76
+ when :open_data, :search, :profile, :private_download, :short_link,
77
+ :calendar, :badges, :static_map, :qr_code, :homepage, :manifest,
78
+ :offline, :link, :authorization, :error, :timeout, :page
79
+ allow! if permission_action.action == :read
80
+ when :newsletter
81
+ allow! if [:read, :unsubscribe].include?(permission_action.action)
82
+ when :upload_validation
83
+ allow! if permission_action.action == :create
84
+ end
85
+ end
86
+
69
87
  def manage_self_user_action?
70
88
  return unless permission_action.subject == :user
71
89
 
@@ -78,6 +96,8 @@ module Decidim
78
96
  authorization = context.fetch(:authorization, nil)
79
97
 
80
98
  case permission_action.action
99
+ when :read
100
+ allow!
81
101
  when :create
82
102
  toggle_allow(authorization.user == user && not_already_active?(authorization))
83
103
  when :update, :destroy
@@ -29,7 +29,7 @@ module Decidim
29
29
 
30
30
  def action_string
31
31
  case action
32
- when "export_component", "soft_delete", "restore"
32
+ when "export_component", "soft_delete", "restore", "menu_hidden"
33
33
  "decidim.admin_log.component.#{action}"
34
34
  when "create", "delete", "publish", "unpublish", "update_permissions", "update_filters"
35
35
  generate_action_string(action)
@@ -111,9 +111,18 @@ module Decidim
111
111
  # Returns an HTML-safe String.
112
112
  def present_explanation
113
113
  h.content_tag(:div, class: "logs__log__explanation") do
114
+ escaped_params = i18n_params.transform_values do |value|
115
+ if value.is_a?(String) && !value.html_safe?
116
+ ERB::Util.html_escape(value)
117
+ else
118
+ value
119
+ end
120
+ end
121
+
114
122
  I18n.t(
115
123
  action_string,
116
- **i18n_params
124
+ escape: false,
125
+ **escaped_params
117
126
  ).html_safe
118
127
  end
119
128
  end
@@ -196,12 +205,21 @@ module Decidim
196
205
  # Returns a Hash.
197
206
  def i18n_params
198
207
  {
199
- user_name: user_presenter.present,
208
+ user_name: system_action? ? nil : user_presenter.present,
200
209
  resource_name: resource_presenter.try(:present),
201
210
  space_name: space_presenter.present
202
211
  }
203
212
  end
204
213
 
214
+ # Private: Whether the action was performed by the system rather than a
215
+ # user. A deleted user still leaves its reference in the log, so this
216
+ # cannot be based on the loaded `user` association.
217
+ #
218
+ # Returns a Boolean.
219
+ def system_action?
220
+ action_log.user_id.blank? && action_log.user_type.blank?
221
+ end
222
+
205
223
  # Private: Calculates if the diff has to be shown or not.
206
224
  #
207
225
  # Returns a Boolean.
@@ -66,9 +66,9 @@ module Decidim
66
66
 
67
67
  # Private: Presents the nickname of the user performing the action.
68
68
  #
69
- # Returns an HTML-safe String.
69
+ # Returns a String.
70
70
  def present_user_nickname
71
- extra["nickname"].html_safe
71
+ extra["nickname"]
72
72
  end
73
73
 
74
74
  # Private: Calculates the path for the user. Returns the path of the
@@ -16,10 +16,19 @@ module Decidim
16
16
  about: resource.user.about,
17
17
  reasons: resource.reports.map(&:reason),
18
18
  details: resource.reports.map(&:details),
19
- block_reasons: resource.blocking.justification,
20
- blocking_user: resource.blocking.blocking_user.presenter.name
19
+ block_reasons: blocking&.justification,
20
+ blocking_user: blocking_user&.presenter&.name
21
21
  }
22
22
  end
23
+
24
+ private
25
+
26
+ # The collection filters on `decidim_users.blocked_at`, but this
27
+ # association is resolved through `decidim_users.block_id`. The two can
28
+ # disagree, in which case there is no block to read the details from.
29
+ def blocking = resource.blocking
30
+
31
+ def blocking_user = blocking&.blocking_user
23
32
  end
24
33
  end
25
34
  end
@@ -14,7 +14,7 @@ module Decidim
14
14
  id: resource.id,
15
15
  hidden_at: resource.hidden_at,
16
16
  report_count: resource.report_count,
17
- reported_url: resource.reportable.reported_content_url,
17
+ reported_url:,
18
18
  reportable_type: resource.decidim_reportable_type,
19
19
  reportable_id: resource.decidim_reportable_id,
20
20
  reported_content: resource.reported_content,
@@ -25,6 +25,30 @@ module Decidim
25
25
  }
26
26
  }
27
27
  end
28
+
29
+ private
30
+
31
+ # `Decidim::Moderation#reportable` is polymorphic, so the database cannot
32
+ # enforce the reference, and the moderation row outlives the resource it
33
+ # points at. Both of the cases below leave the moderation in the export
34
+ # collection with no URL to build.
35
+ def reported_url
36
+ reportable = resource.reportable
37
+
38
+ # The resource is gone. Reportables such as proposals and meetings are
39
+ # soft-deletable while `belongs_to :reportable` does not use
40
+ # `with_deleted`, so moving one to the trash is enough for this.
41
+ return if reportable.nil?
42
+
43
+ # The component is gone but the resource is not: trashing a component
44
+ # does not trash its contents. `ResourceLocatorPresenter#route_proxy`
45
+ # falls back to the resource itself when `component` is nil, and
46
+ # `EngineRouter.main_proxy` then calls `mounted_engine` on it, which
47
+ # only components and participatory spaces respond to.
48
+ return if reportable.is_a?(Decidim::HasComponent) && reportable.component.nil?
49
+
50
+ reportable.reported_content_url
51
+ end
28
52
  end
29
53
  end
30
54
  end
@@ -5,31 +5,65 @@ module Decidim
5
5
  # use this class directly, but rather use `Decidim.traceability` instead.
6
6
  # Check the docs on `Decidim::Traceability` for more info.
7
7
  #
8
- # Usage:
8
+ # @example Log an action performed by a user
9
+ # Decidim::ActionLogger.log(:create, user, new_proposal, version_id, extra_data)
9
10
  #
10
- # ActionLogger.log(:create, user, new_proposal, extra_data)
11
+ # @example Log an action performed by the system
12
+ # Decidim::ActionLogger.log_system(:create, new_proposal, visibility: "public-only")
13
+ #
14
+ # @see Decidim::Traceability
11
15
  class ActionLogger
12
- # Public: Logs the given `action` by the given `user` on the given `resource`.
16
+ # Logs the given `action` by the given `user` on the given `resource`.
13
17
  # Delegates the work to the instance method.
14
18
  #
15
- # action - a String representing the name of the action
16
- # user - the Decidim::User that performed the action
17
- # resource - the resource on which the action was performed
18
- # version_id - the ID of the `PaperTrail::Version` that was created on that action
19
- # resource_extra - a Hash with resource_extra info to be recorded
19
+ # @param action [String, Symbol] the name of the action performed
20
+ # @param user [Decidim::User, Decidim::Api::ApiUser] the user that
21
+ # performed the action
22
+ # @param resource [ActiveRecord::Base] the resource on which the action was
23
+ # performed
24
+ # @param version_id [Integer, nil] the ID of the `PaperTrail::Version` that
25
+ # was created by the action
26
+ # @param resource_extra [Hash] extra information to be recorded in the
27
+ # action log. It may include the `:visibility` key to set the visibility
28
+ # of the action log.
29
+ #
30
+ # @return [Decidim::ActionLog] the newly created action log
20
31
  #
21
- # Returns the newly created `Decidim::ActionLog` resource.
32
+ # @raise [ArgumentError] if `user` is `nil`. Use {.log_system} for actions
33
+ # performed by the system.
22
34
  def self.log(action, user, resource, version_id, resource_extra = {})
23
- new(action, user, resource, version_id, resource_extra).log!
35
+ raise ArgumentError, "ActionLogger.log requires a user. Use ActionLogger.log_system for actions performed by the system." if user.nil?
36
+
37
+ new(action, user, resource, version_id, resource_extra).send(:log!)
38
+ end
39
+
40
+ # Logs the given `action` performed by the system (i.e. without a user) on
41
+ # the given `resource`.
42
+ #
43
+ # @param action [String, Symbol] the name of the action performed
44
+ # @param resource [ActiveRecord::Base] the resource on which the action was
45
+ # performed
46
+ # @param resource_extra [Hash] keyword arguments with extra information to
47
+ # be recorded in the action log. It may include the `:visibility` key to
48
+ # set the visibility of the action log.
49
+ #
50
+ # @return [Decidim::ActionLog] the newly created action log
51
+ def self.log_system(action, resource, **resource_extra)
52
+ new(action, nil, resource, nil, resource_extra).send(:log!)
24
53
  end
25
54
 
26
- # Public: Initializes the instance.
55
+ # Initializes the instance.
27
56
  #
28
- # action - a String representing the name of the action
29
- # user - the Decidim::User that performed the action
30
- # resource - the resource on which the action was performed
31
- # version_id - the ID of the `PaperTrail::Version` that was created on that action
32
- # resource_extra - a Hash with resource_extra info to be recorded
57
+ # @param action [String, Symbol] the name of the action performed
58
+ # @param user [Decidim::User, Decidim::Api::ApiUser, nil] the user that
59
+ # performed the action, or `nil` for actions performed by the system
60
+ # @param resource [ActiveRecord::Base] the resource on which the action was
61
+ # performed
62
+ # @param version_id [Integer, nil] the ID of the `PaperTrail::Version` that
63
+ # was created by the action
64
+ # @param resource_extra [Hash] extra information to be recorded in the
65
+ # action log. It may include the `:visibility` key to set the visibility
66
+ # of the action log, which defaults to `"admin-only"`.
33
67
  def initialize(action, user, resource, version_id = nil, resource_extra = {})
34
68
  @action = action
35
69
  @user = user
@@ -39,14 +73,17 @@ module Decidim
39
73
  @visibility = resource_extra.delete(:visibility).presence || "admin-only"
40
74
  end
41
75
 
42
- # Public: Logs the given `action` by the given `user` on the given
43
- # `resource`.
76
+ private
77
+
78
+ # Logs the given `action` by the given `user` on the given `resource`.
79
+ #
80
+ # @return [Decidim::ActionLog] the newly created action log
44
81
  #
45
- # Returns the newly created `Decidim::ActionLog` resource.
82
+ # @raise [ActiveRecord::RecordInvalid] if the action log is not valid
46
83
  def log!
47
84
  Decidim::ActionLog.create!(
48
- user_id: user.id,
49
- user_type: user.class.name,
85
+ user_id: user&.id,
86
+ user_type: user&.class&.name,
50
87
  organization:,
51
88
  action:,
52
89
  resource:,
@@ -62,44 +99,75 @@ module Decidim
62
99
  )
63
100
  end
64
101
 
65
- private
66
-
102
+ # The values given to the logger when it was initialized.
67
103
  attr_reader :action, :user, :resource, :resource_extra, :version_id, :visibility
68
104
 
105
+ # Finds the organization related to the action log.
106
+ #
107
+ # @return [Decidim::Organization, nil] the organization of the user, the
108
+ # participatory space, or the resource, whichever is found first
69
109
  def organization
70
- user.organization
110
+ user&.organization || participatory_space&.organization || resource.try(:organization)
71
111
  end
72
112
 
113
+ # Finds the component related to the resource.
114
+ #
115
+ # @return [Decidim::Component, nil] the component of the resource, if the
116
+ # resource responds to `component`
73
117
  def component
74
118
  resource.component if resource.respond_to?(:component)
75
119
  end
76
120
 
121
+ # Finds the participatory space related to the resource.
122
+ #
123
+ # @return [ActiveRecord::Base, nil] the participatory space of the
124
+ # component, of the resource, or `nil` if it cannot be found
77
125
  def participatory_space
78
126
  return component.participatory_space if component.respond_to?(:participatory_space)
79
127
 
80
128
  resource.participatory_space if resource.respond_to?(:participatory_space)
81
129
  end
82
130
 
131
+ # Finds the area related to the resource or its participatory space.
132
+ #
133
+ # @return [Decidim::Area, nil] the area of the resource or the
134
+ # participatory space, or `nil` if it cannot be found
83
135
  def area
84
136
  resource.try(:area) || participatory_space.try(:area)
85
137
  end
86
138
 
139
+ # Finds the scope related to the resource or its participatory space.
140
+ #
141
+ # @return [Decidim::Scope, nil] the scope of the resource or the
142
+ # participatory space, or `nil` if it cannot be found
87
143
  def scope
88
144
  resource.try(:scope) || participatory_space.try(:scope)
89
145
  end
90
146
 
147
+ # Finds a human-readable title for the given resource.
148
+ #
149
+ # @param resource [Object, nil] an object that may respond to `title`,
150
+ # `name`, or `subject`
151
+ # @return [String, nil] the title, name, or subject of the resource, or
152
+ # `nil` if none of them is available
91
153
  def title_for(resource)
92
154
  resource.try(:title) || resource.try(:name) || resource.try(:subject)
93
155
  end
94
156
 
157
+ # Finds the manifest name of the participatory space.
158
+ #
159
+ # @return [Symbol, nil] the manifest name of the participatory space, or
160
+ # `nil` if it cannot be found
95
161
  def participatory_space_manifest_name
96
162
  participatory_space.try(:class).try(:participatory_space_manifest).try(:name)
97
163
  end
98
164
 
99
- # Private: Defines some extra data that will be saved in the action log `extra`
165
+ # Defines some extra data that will be saved in the action log `extra`
100
166
  # field.
101
167
  #
102
- # Returns a Hash.
168
+ # @return [Hash] the extra data with information about the component, the
169
+ # participatory space, the resource, and the user, merged with the
170
+ # `resource_extra` information
103
171
  def extra_data
104
172
  {
105
173
  component: {
@@ -114,9 +182,9 @@ module Decidim
114
182
  title: title_for(resource)
115
183
  }.compact,
116
184
  user: {
117
- ip: user.current_sign_in_ip,
118
- name: user.name,
119
- nickname: user.nickname
185
+ ip: user&.current_sign_in_ip,
186
+ name: user&.name,
187
+ nickname: user&.nickname
120
188
  }.compact
121
189
  }.deep_merge(resource_extra)
122
190
  end