class Decidim::Events::BaseEvent

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).

Attributes

event_name[R]
extra[R]
resource[R]
user[R]
user_role[R]

Public Class Methods

new(resource:, event_name:, user:, user_role: nil, extra: {}) click to toggle source

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.

# 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
type(type) click to toggle source

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]
# File lib/decidim/events/base_event.rb, line 34
def self.type(type)
  self.types += Array(type)
end

Public Instance Methods

resource_locator() click to toggle source

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

# File lib/decidim/events/base_event.rb, line 56
def resource_locator
  @resource_locator ||= Decidim::ResourceLocatorPresenter.new(resource)
end
resource_path() click to toggle source

Caches the path for the given resource.

# File lib/decidim/events/base_event.rb, line 61
def resource_path
  @resource_path ||= begin
    if resource&.respond_to?(:polymorphic_resource_path)
      resource.polymorphic_resource_path(resource_url_params)
    else
      resource_locator.path(resource_url_params)
    end
  end
end
resource_text() click to toggle source
# File lib/decidim/events/base_event.rb, line 82
def resource_text; end
resource_title() click to toggle source
# File lib/decidim/events/base_event.rb, line 88
def resource_title
  return unless resource

  title = if resource.respond_to?(:title)
            translated_attribute(resource.title)
          elsif resource.respond_to?(:name)
            translated_attribute(resource.name)
          end

  Decidim::ContentProcessor.render_without_format(title, links: false).html_safe
end
resource_url() click to toggle source

Caches the URL for the given resource.

# File lib/decidim/events/base_event.rb, line 72
def resource_url
  @resource_url ||= begin
    if resource&.respond_to?(:polymorphic_resource_url)
      resource.polymorphic_resource_url(resource_url_params)
    else
      resource_locator.url(resource_url_params)
    end
  end
end
safe_resource_text() click to toggle source
# File lib/decidim/events/base_event.rb, line 84
def safe_resource_text
  translated_attribute(resource_text).to_s.html_safe
end

Private Instance Methods

component() click to toggle source
# File lib/decidim/events/base_event.rb, line 102
def component
  return resource if resource.is_a?(Decidim::Component)

  resource.try(:component)
end
participatory_space() click to toggle source
# File lib/decidim/events/base_event.rb, line 108
def participatory_space
  return resource if resource.is_a?(Decidim::Participable)

  resource.try(:participatory_space)
end
resource_url_params() click to toggle source
# File lib/decidim/events/base_event.rb, line 114
def resource_url_params
  {}
end