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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, event_name:, user:, 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 extra - a Hash with extra information of the event.



44
45
46
47
48
49
# File 'lib/decidim/events/base_event.rb', line 44

def initialize(resource:, event_name:, user:, extra:)
  @event_name = event_name
  @resource = resource
  @user = user
  @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

#notifiable?Boolean

Whether this event should be notified or not. Useful when you want the event to decide based on the params.

It returns false when the resource or any element in the chain is a ‘Decidim::Publicable` and it isn’t published or participatory_space is a ‘Decidim::Participable` and the user can’t participate.

Returns:

  • (Boolean)


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

def notifiable?
  return false if resource.is_a?(Decidim::Publicable) && !resource.published?
  return false if participatory_space.is_a?(Decidim::Publicable) && !participatory_space&.published?
  return false if component && !component.published?

  return false if participatory_space.is_a?(Decidim::Participable) && !participatory_space.can_participate?(user)

  true
end

#resource_locatorObject

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



53
54
55
# File 'lib/decidim/events/base_event.rb', line 53

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

#resource_pathObject

Caches the path for the given resource.



58
59
60
# File 'lib/decidim/events/base_event.rb', line 58

def resource_path
  @resource_path ||= resource_locator.path
end

#resource_urlObject

Caches the URL for the given resource.



63
64
65
# File 'lib/decidim/events/base_event.rb', line 63

def resource_url
  @resource_url ||= resource_locator.url
end