app/controllers/mailkick/subscriptions_controller.rb in mailkick-0.0.2 vs app/controllers/mailkick/subscriptions_controller.rb in mailkick-0.0.3
- old
+ new
@@ -4,15 +4,16 @@
def show
end
def unsubscribe
- if subscribed?
+ if !opted_out?
Mailkick::OptOut.create! do |o|
o.email = @email
o.user = @user
o.reason = "unsubscribe"
+ o.list = @list
end
end
redirect_to subscription_path(params[:id])
end
@@ -20,39 +21,47 @@
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
+ if @user and @user.respond_to?(:opt_in)
+ @user.opt_in(@options)
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])
+ @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 = {}
+ @options[:list] = @list if @list
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?
+ def opted_out?(options = {})
+ options = @options.merge(options)
+ if @user and @user.respond_to?(:opted_out?)
+ @user.opted_out?(options)
else
- Mailkick::OptOut.where(email: @email, active: true).empty?
+ relation = Mailkick::OptOut.where(email: @email, active: true)
+ if options[:list]
+ relation.where("list IS NULL OR list = ?", options[:list])
+ else
+ relation.where("list IS NULL")
+ end.any?
end
end
- helper_method :subscribed?
+ helper_method :opted_out?
def subscribe_url(options = {})
subscribe_subscription_path(params[:id], options)
end
helper_method :subscribe_url