Sha256: 95391a4323d319d81e2260d9442991baa8e6ad672f63ea9b1961f22561f3a268

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

module Mailkick
  class SubscriptionsController < ActionController::Base
    protect_from_forgery with: :exception

    before_action :set_subscription

    def show
    end

    def unsubscribe
      subscription.delete_all

      Mailkick::Legacy.opt_out(legacy_options) if Mailkick::Legacy.opt_outs?

      redirect_to subscription_path(params[:id])
    end

    def subscribe
      subscription.first_or_create!

      Mailkick::Legacy.opt_in(legacy_options) if Mailkick::Legacy.opt_outs?

      redirect_to subscription_path(params[:id])
    end

    protected

    def set_subscription
      @email, @subscriber_id, @subscriber_type, @list = Mailkick.message_verifier.verify(params[:id])
    rescue ActiveSupport::MessageVerifier::InvalidSignature
      render plain: "Subscription not found", status: :bad_request
    end

    def subscription
      Mailkick::Subscription.where(
        subscriber_id: @subscriber_id,
        subscriber_type: @subscriber_type,
        list: @list
      )
    end

    def subscribed?
      subscription.exists?
    end
    helper_method :subscribed?

    def opted_out?
      !subscribed?
    end
    helper_method :opted_out?

    def subscribe_url
      subscribe_subscription_path(params[:id])
    end
    helper_method :subscribe_url

    def unsubscribe_url
      unsubscribe_subscription_path(params[:id])
    end
    helper_method :unsubscribe_url

    def legacy_options
      if @subscriber_type
        # on the unprobabilistic chance subscriber_type is compromised, not much damage
        user = @subscriber_type.constantize.find(@subscriber_id)
      end
      {
        email: @email,
        user: user,
        list: @list
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mailkick-1.2.2 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.2.1 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.2.0 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.1.2 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.1.1 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.1.0 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.0.1 app/controllers/mailkick/subscriptions_controller.rb
mailkick-1.0.0 app/controllers/mailkick/subscriptions_controller.rb