Class: Decidim::Events::BaseEvent
- Inherits:
-
Object
- Object
- Decidim::Events::BaseEvent
- Extended by:
- ActiveModel::Translation
- Includes:
- SanitizeHelper, TranslatableAttributes
- Defined in:
- lib/decidim/events/base_event.rb
Overview
This class serves as a base for all event classes. Event classes are intended to add more logic to a ‘Decidim::Notification` and are used to render them in the notifications dashboard and to generate other notifications (emails, for example).
Direct Known Subclasses
Class Method Summary collapse
-
.type(type) ⇒ Object
Public: Stores all the notification types this event can create.
Instance Method Summary collapse
- #content_in_same_language? ⇒ Boolean
- #hidden_resource? ⇒ Boolean
-
#initialize(resource:, event_name:, user:, user_role: nil, extra: {}) ⇒ BaseEvent
constructor
Initializes the class.
- #organization ⇒ Object
- #perform_translation? ⇒ Boolean
-
#resource_locator ⇒ Object
Caches the locator for the given resource, so that we can find the resource URL.
-
#resource_path ⇒ Object
Caches the path for the given resource.
- #resource_text ⇒ Object
- #resource_title ⇒ Object
-
#resource_url ⇒ Object
Caches the URL for the given resource.
- #safe_resource_text ⇒ Object
- #safe_resource_translated_text ⇒ Object
- #translation_missing? ⇒ Boolean
Methods included from SanitizeHelper
#decidim_escape_translated, #decidim_html_escape, #decidim_sanitize, #decidim_sanitize_admin, #decidim_sanitize_editor, #decidim_sanitize_editor_admin, #decidim_sanitize_newsletter, #decidim_sanitize_translated, #decidim_url_escape, included
Methods included from TranslatableAttributes
Constructor Details
#initialize(resource:, event_name:, user:, user_role: nil, extra: {}) ⇒ BaseEvent
Initializes the class.
event_name - a String with the name of the event. resource - the resource that received the event user - the User that receives the event user_role - the role the user takes for this event (either ‘:follower` or
`:affected_user`)
extra - a Hash with extra information of the event.
49 50 51 52 53 54 55 |
# File 'lib/decidim/events/base_event.rb', line 49 def initialize(resource:, event_name:, user:, user_role: nil, extra: {}) @event_name = event_name @resource = resource @user = user @user_role = user_role @extra = extra.with_indifferent_access end |
Class Method Details
.type(type) ⇒ Object
Public: Stores all the notification types this event can create. Please, do not overwrite this method, consider it final. Instead, add values to the array via modules, take the ‘NotificationEvent` module as an example:
Example:
module WebPushNotificationEvent
extend ActiveSupport::Concern
included do
type :web_push_notifications
end
end
class MyEvent < Decidim::Events::BaseEvent
include WebPushNotificationEvent
end
MyEvent.types # => [:web_push_notifications]
37 38 39 |
# File 'lib/decidim/events/base_event.rb', line 37 def self.type(type) self.types += Array(type) end |
Instance Method Details
#content_in_same_language? ⇒ Boolean
91 92 93 |
# File 'lib/decidim/events/base_event.rb', line 91 def content_in_same_language? false end |
#hidden_resource? ⇒ Boolean
117 118 119 |
# File 'lib/decidim/events/base_event.rb', line 117 def hidden_resource? resource.respond_to?(:hidden?) && resource.hidden? end |
#organization ⇒ Object
83 84 85 |
# File 'lib/decidim/events/base_event.rb', line 83 def organization resource.try(:organization) end |
#perform_translation? ⇒ Boolean
87 88 89 |
# File 'lib/decidim/events/base_event.rb', line 87 def perform_translation? false end |
#resource_locator ⇒ Object
Caches the locator for the given resource, so that we can find the resource URL.
59 60 61 |
# File 'lib/decidim/events/base_event.rb', line 59 def resource_locator @resource_locator ||= Decidim::ResourceLocatorPresenter.new(resource) end |
#resource_path ⇒ Object
Caches the path for the given resource.
64 65 66 67 68 69 70 |
# File 'lib/decidim/events/base_event.rb', line 64 def resource_path @resource_path ||= if resource.respond_to?(:polymorphic_resource_path) resource.polymorphic_resource_path(resource_url_params) else resource_locator.path(resource_url_params) end end |
#resource_text ⇒ Object
81 |
# File 'lib/decidim/events/base_event.rb', line 81 def resource_text; end |
#resource_title ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/decidim/events/base_event.rb', line 105 def resource_title return unless resource title = if resource.respond_to?(:title) decidim_sanitize_translated(resource.title) elsif resource.respond_to?(:name) decidim_sanitize_translated(resource.name) end Decidim::ContentProcessor.render_without_format(title, links: false).html_safe end |
#resource_url ⇒ Object
Caches the URL for the given resource.
73 74 75 76 77 78 79 |
# File 'lib/decidim/events/base_event.rb', line 73 def resource_url @resource_url ||= if resource.respond_to?(:polymorphic_resource_url) resource.polymorphic_resource_url(resource_url_params) else resource_locator.url(resource_url_params) end end |
#safe_resource_text ⇒ Object
99 100 101 |
# File 'lib/decidim/events/base_event.rb', line 99 def safe_resource_text translated_attribute(resource_text).to_s.html_safe end |
#safe_resource_translated_text ⇒ Object
103 |
# File 'lib/decidim/events/base_event.rb', line 103 def safe_resource_translated_text; end |
#translation_missing? ⇒ Boolean
95 96 97 |
# File 'lib/decidim/events/base_event.rb', line 95 def translation_missing? false end |