class Decidim::Events::SimpleEvent

Extends the BaseEvent to add common components to most events so you don't need to write each time the same code.

The only convention you need to keep in mind is that the event name will be used as the i18n scope to search for the keys.

Public Class Methods

i18n_attributes(*attributes) click to toggle source

Public: A method to add values to pass as interpolations to the I18n.t method.

By default the resource_path, resource_title and resource_url are already included.

attribute - A Symbol of the method name (and interpolation value) to add.

Example:

class MyEvent < Decidim::Events::SimpleEvent
  i18n_attributes :participatory_space_title
end
# File lib/decidim/events/simple_event.rb, line 30
def self.i18n_attributes(*attributes)
  self.i18n_interpolations += Array(attributes)
end

Public Instance Methods

email_intro() click to toggle source
# File lib/decidim/events/simple_event.rb, line 38
def email_intro
  I18n.t("email_intro", i18n_options).html_safe
end
email_outro() click to toggle source
# File lib/decidim/events/simple_event.rb, line 42
def email_outro
  I18n.t("email_outro", i18n_options).html_safe
end
email_subject() click to toggle source
# File lib/decidim/events/simple_event.rb, line 34
def email_subject
  I18n.t("email_subject", i18n_options).html_safe
end
event_has_roles?() click to toggle source

Public: Whether the event differentiates between different user roles. This will change the default i18n scope.

Returns a boolean.

# File lib/decidim/events/simple_event.rb, line 65
def event_has_roles?
  false
end
i18n_options() click to toggle source

Public: The Hash of options to pass to the I18.t method.

# File lib/decidim/events/simple_event.rb, line 70
def i18n_options
  default_i18n_options.merge(event_interpolations).transform_values do |value|
    if value.is_a?(String)
      decidim_html_escape(value)
    else
      value
    end
  end
end
i18n_scope() click to toggle source

Public: The String to use as scope to search for the keys when using I18n.t

By default is the same value as the event name. If the event has roles, then the role is appended to the i18n scope.

# File lib/decidim/events/simple_event.rb, line 55
def i18n_scope
  return event_name if user_role.blank? || !event_has_roles?

  "#{event_name}.#{user_role}"
end
notification_title() click to toggle source
# File lib/decidim/events/simple_event.rb, line 46
def notification_title
  I18n.t("notification_title", i18n_options).html_safe
end
participatory_space_url() click to toggle source

Caches the URL for the resource's participatory space.

# File lib/decidim/events/simple_event.rb, line 95
def participatory_space_url
  return unless participatory_space

  @participatory_space_url ||= ResourceLocatorPresenter.new(participatory_space).url
end
resource_path() click to toggle source

Caches the path for the given resource when it's a Decidim::Component.

# File lib/decidim/events/simple_event.rb, line 81
def resource_path
  return super unless resource.is_a?(Decidim::Component)

  @resource_path ||= main_component_path(resource)
end
resource_text() click to toggle source

Caches the text to render as a quote in the email. It will appear, if present, after the `intro` section. This method is intended to be overwritten by each event class.

Returns a string.

# File lib/decidim/events/simple_event.rb, line 106
def resource_text
  nil
end
resource_url() click to toggle source

Caches the URL for the given resource when it's a Decidim::Component.

Calls superclass method Decidim::Events::BaseEvent#resource_url
# File lib/decidim/events/simple_event.rb, line 88
def resource_url
  return super unless resource.is_a?(Decidim::Component)

  @resource_url ||= main_component_url(resource)
end

Private Instance Methods

default_i18n_options() click to toggle source
# File lib/decidim/events/simple_event.rb, line 118
def default_i18n_options
  {
    resource_path: resource_path,
    resource_title: resource_title,
    resource_url: resource_url,
    participatory_space_title: participatory_space_title,
    participatory_space_url: participatory_space_url,
    scope: i18n_scope
  }
end
event_interpolations() click to toggle source
# File lib/decidim/events/simple_event.rb, line 112
def event_interpolations
  Array(self.class.i18n_interpolations).inject({}) do |all, attribute|
    all.update(attribute => send(attribute))
  end
end
participatory_space_title() click to toggle source
# File lib/decidim/events/simple_event.rb, line 129
def participatory_space_title
  translated_attribute(participatory_space.try(:title))
end