lib/mailkick/model.rb in mailkick-0.0.3 vs lib/mailkick/model.rb in mailkick-0.0.4

- old
+ new

@@ -1,55 +1,34 @@ module Mailkick module Model def mailkick_user(options = {}) + email_key = options[:email_key] || :email class_eval do scope :opted_out, proc {|options = {}| binds = [self.class.name, true] if options[:list] - query = "(list IS NULL OR list = ?)" + query = "(mailkick_opt_outs.list IS NULL OR mailkick_opt_outs.list = ?)" binds << options[:list] else - query = "list IS NULL" + query = "mailkick_opt_outs.list IS NULL" end - where("#{options[:not] ? "NOT " : ""}EXISTS(SELECT * FROM mailkick_opt_outs WHERE (#{table_name}.email = mailkick_opt_outs.email OR (#{table_name}.id = mailkick_opt_outs.user_id AND mailkick_opt_outs.user_type = ?)) AND active = ? AND #{query})", *binds) + where("#{options[:not] ? "NOT " : ""}EXISTS(SELECT * FROM mailkick_opt_outs WHERE (#{table_name}.#{email_key} = mailkick_opt_outs.email OR (#{table_name}.#{primary_key} = mailkick_opt_outs.user_id AND mailkick_opt_outs.user_type = ?)) AND mailkick_opt_outs.active = ? AND #{query})", *binds) } scope :not_opted_out, proc {|options = {}| opted_out(options.merge(not: true)) } - def opt_outs(options = {}) - relation = Mailkick::OptOut.where("email = ? OR (user_id = ? AND user_type = ?)", email, id, self.class.name) - if options[:list] - relation.where("list IS NULL OR list = ?", options[:list]) - else - relation.where(list: nil) - end - end - def opted_out?(options = {}) - opt_outs(options).where(active: true).any? + Mailkick.opted_out?({email: email, user: self}.merge(options)) end def opt_out(options = {}) - if !opted_out?(options) - OptOut.create! do |o| - o.email = email - o.user = self - o.reason = "unsubscribe" - o.list = options[:list] - o.save! - end - end - true + Mailkick.opt_out({email: email, user: self}.merge(options)) end def opt_in(options = {}) - opt_outs(options).where(active: true).each do |opt_out| - opt_out.active = false - opt_out.save! - end - true + Mailkick.opt_in({email: email, user: self}.merge(options)) end end end