Sha256: 8c9c21192ac34c5173161fe53e4d0b1cbe61ec311c34f3c451f9823723f382af

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

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

    def show
    end

    def unsubscribe
      if subscribed?
        Mailkick::OptOut.create! do |o|
          o.email = @email
          o.user = @user
          o.reason = "unsubscribe"
        end
      end

      redirect_to subscription_path(params[:id])
    end

    def subscribe
      Mailkick::OptOut.where(email: @email, active: true).each do |opt_out|
        opt_out.active = false
        opt_out.save!
      end
      if @user and @user.respond_to?(:subscribe)
        @user.subscribe
      end

      redirect_to subscription_path(params[:id])
    end

    protected

    def set_email
      verifier = ActiveSupport::MessageVerifier.new(Mailkick.secret_token)
      begin
        @email, user_id, user_type = 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
      rescue ActiveSupport::MessageVerifier::InvalidSignature
        render text: "Subscription not found", status: :bad_request
      end
    end

    def subscribed?
      if @user and @user.respond_to?(:subscribed?)
        @user.subscribed?
      else
        Mailkick::OptOut.where(email: @email, active: true).empty?
      end
    end
    helper_method :subscribed?

    def subscribe_url(options = {})
      subscribe_subscription_path(params[:id], options)
    end
    helper_method :subscribe_url

    def unsubscribe_url(options = {})
      unsubscribe_subscription_path(params[:id], options)
    end
    helper_method :unsubscribe_url

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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