Class: Decidim::NewslettersController

Inherits:
ApplicationController show all
Includes:
NewslettersHelper
Defined in:
app/controllers/decidim/newsletters_controller.rb

Overview

The controller to show the newsletter on the website.

Instance Method Summary collapse

Methods included from NewslettersHelper

#custom_url_for_mail_root, #parse_interpolations, #utm_codes

Methods included from NeedsSnippets

#snippets

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#newsletterObject



46
47
48
# File 'app/controllers/decidim/newsletters_controller.rb', line 46

def newsletter
  @newsletter ||= collection.find(params[:id])
end

#showObject

Raises:

  • (ActionController::RoutingError)


13
14
15
16
17
18
19
20
# File 'app/controllers/decidim/newsletters_controller.rb', line 13

def show
  @user = current_user
  @organization = current_organization

  raise ActionController::RoutingError, "Not Found" unless newsletter.sent?

  @encrypted_token = Decidim::NewsletterEncryptor.sent_at_encrypted(@user.id, newsletter.sent_at) if @user.present?
end

#unsubscribeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/decidim/newsletters_controller.rb', line 22

def unsubscribe
  encryptor = Decidim::NewsletterEncryptor

  decrypted_string = encryptor.sent_at_decrypted(params[:u])
  user = User.find_by(decidim_organization_id: current_organization.id, id: decrypted_string.split("-").first)
  sent_at_time = Time.zone.at(decrypted_string.split("-").second.to_i)

  if sent_at_time > (Time.current - 15.days)
    UnsubscribeSettings.call(user) do
      on(:ok) do
        flash.now[:notice] = t("newsletters.unsubscribe.success", scope: "decidim")
      end

      on(:invalid) do
        flash.now[:alert] = t("newsletters.unsubscribe.error", scope: "decidim")
        render action: :unsubscribe
      end
    end
  else
    flash.now[:alert] = t("newsletters.unsubscribe.token_error", scope: "decidim")
    render action: :unsubscribe
  end
end