Sha256: 88ea7e6d078f844e75c841c9c0a440af95a243a66024dfacc9c820af4ab526d6

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Devise
  module Models
    # Newsletterable adds all needed validations to acomplish GDPR standards
    # in newsletter notification mailing
    #
    # Validates that all users with notifications activated have Opt-in in
    # some time
    module DecidimNewsletterable
      GDPR_DATE = Time.zone.parse("2018-05-25 00:00 +02:00")

      def newsletter_opt_in_notify
        unless newsletter_opt_in_valid?
          set_newsletter_token!
          Decidim::NewslettersOptInJob.perform_later(self, newsletter_token)
        end
      end

      def newsletter_opt_in_validate
        set_newsletter_opt_in! if newsletter_notifications_at.blank?
      end

      protected

      # Checks if Opt-in is valid or not
      # An user with notifications activated, must have an Opt-in
      def newsletter_opt_in_valid?
        newsletter_notifications_at > GDPR_DATE
      end

      # Fill token value with some random value
      # Removes opt_int timestamp
      # Deactivate newsletter notifications
      def set_newsletter_token
        self.newsletter_notifications_at = nil
        self.newsletter_token = SecureRandom.base58(24)
      end

      # Sets token and Opt-in values & saves
      def set_newsletter_token!
        set_newsletter_token
        save
      end

      # Fill Opt-in value with current time &
      # removes any token involved
      def set_newsletter_opt_in
        self.newsletter_notifications_at = Time.zone.now
        self.newsletter_token = ""
      end

      # Sets Opt-in and token values & saves
      def set_newsletter_opt_in!
        set_newsletter_opt_in
        save
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-core-0.13.0 lib/devise/models/decidim_newsletterable.rb
decidim-core-0.13.0.pre1 lib/devise/models/decidim_newsletterable.rb