Sha256: 2e2ffbdb1eee7e5ed6e0605dc0c33cfb96eabdb99f41266af08c9427850d3e0b

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Mailkick
  class SubscriptionsController < ActionController::Base
    before_filter :set_email

    def show
    end

    def unsubscribe
      Mailkick.opt_out(@options)
      redirect_to subscription_path(url_token)
    end

    def subscribe
      Mailkick.opt_in(@options)
      redirect_to subscription_path(url_token)
    end

    protected

    def set_email
      verifier = ActiveSupport::MessageVerifier.new(Mailkick.secret_token)
      begin
        @email, user_id, user_type, @list = verifier.verify(params[:id])
        if user_type
          # on the unprobabilistic chance user_type is compromised, not much damage
          @user = user_type.constantize.find(user_id)
        end
        @options = {
          email: @email,
          user: @user,
          list: @list
        }
      rescue ActiveSupport::MessageVerifier::InvalidSignature
        render text: "Subscription not found", status: :bad_request
      end
    end

    def opted_out?
      Mailkick.opted_out?(@options)
    end
    helper_method :opted_out?

    def subscribe_url
      subscribe_subscription_path(url_token)
    end
    helper_method :subscribe_url

    def unsubscribe_url
      unsubscribe_subscription_path(url_token)
    end
    helper_method :unsubscribe_url

    private

    def url_token
      @url_token ||= CGI.escape(params[:id])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mailkick-0.1.2 app/controllers/mailkick/subscriptions_controller.rb