Class: Decidim::Events::BaseEvent

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Includes:
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

SimpleEvent, WelcomeNotificationEvent

Class Method Summary collapse

Instance Method Summary collapse

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.



46
47
48
49
50
51
52
# File 'lib/decidim/events/base_event.rb', line 46

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]


34
35
36
# File 'lib/decidim/events/base_event.rb', line 34

def self.type(type)
  self.types += Array(type)
end

Instance Method Details

#resource_locatorObject

Caches the locator for the given resource, so that we can find the resource URL.



56
57
58
# File 'lib/decidim/events/base_event.rb', line 56

def resource_locator
  @resource_locator ||= Decidim::ResourceLocatorPresenter.new(resource)
end

#resource_pathObject

Caches the path for the given resource.



61
62
63
# File 'lib/decidim/events/base_event.rb', line 61

def resource_path
  @resource_path ||= resource_locator.path
end

#resource_textObject



70
# File 'lib/decidim/events/base_event.rb', line 70

def resource_text; end

#resource_titleObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/decidim/events/base_event.rb', line 72

def resource_title
  return unless resource

  if resource.is_a?(Decidim::Hashtaggable)
    translated_title = translated_attribute(resource.title)
    renderer = Decidim::ContentRenderers::HashtagRenderer.new(translated_title)
    renderer.render(links: false).html_safe
  elsif resource.respond_to?(:title)
    translated_attribute(resource.title)
  elsif resource.respond_to?(:name)
    translated_attribute(resource.name)
  end
end

#resource_urlObject

Caches the URL for the given resource.



66
67
68
# File 'lib/decidim/events/base_event.rb', line 66

def resource_url
  @resource_url ||= resource_locator.url
end