app/controllers/mailkick/subscriptions_controller.rb in mailkick-0.4.3 vs app/controllers/mailkick/subscriptions_controller.rb in mailkick-1.0.0

- old
+ new

@@ -1,43 +1,53 @@ module Mailkick class SubscriptionsController < ActionController::Base protect_from_forgery with: :exception - before_action :set_email + before_action :set_subscription def show end def unsubscribe - Mailkick.opt_out(@options) + subscription.delete_all + + Mailkick::Legacy.opt_out(legacy_options) if Mailkick::Legacy.opt_outs? + redirect_to subscription_path(params[:id]) end def subscribe - Mailkick.opt_in(@options) + 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_email - @email, user_id, user_type, @list = Mailkick.message_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 - } + 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? - Mailkick.opted_out?(@options) + !subscribed? end helper_method :opted_out? def subscribe_url subscribe_subscription_path(params[:id]) @@ -46,7 +56,19 @@ 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