lib/mailkick.rb in mailkick-0.4.2 vs lib/mailkick.rb in mailkick-0.4.3

- old
+ new

@@ -5,10 +5,11 @@ require "set" # modules require "mailkick/model" require "mailkick/service" +require "mailkick/service/aws_ses" require "mailkick/service/mailchimp" require "mailkick/service/mailgun" require "mailkick/service/mandrill" require "mailkick/service/sendgrid" require "mailkick/service/sendgrid_v2" @@ -63,21 +64,20 @@ end def self.opt_outs(options = {}) relation = Mailkick::OptOut.where(active: true) - parts = [] - binds = [] + contact_relation = Mailkick::OptOut.none if (email = options[:email]) - parts << "email = ?" - binds << email + contact_relation = contact_relation.or(Mailkick::OptOut.where(email: email)) end if (user = options[:user]) - parts << "(user_id = ? and user_type = ?)" - binds.concat [user.id, user.class.name] + contact_relation = contact_relation.or( + Mailkick::OptOut.where("user_id = ? AND user_type = ?", user.id, user.class.name) + ) end - relation = relation.where(parts.join(" OR "), *binds) if parts.any? + relation = relation.merge(contact_relation) if email || user relation = if options[:list] relation.where("list IS NULL OR list = ?", options[:list]) else @@ -85,16 +85,18 @@ end relation end + # TODO use keyword arguments def self.opted_out_emails(options = {}) - Set.new(opt_outs(options).where("email IS NOT NULL").uniq.pluck(:email)) + Set.new(opt_outs(options).where.not(email: nil).distinct.pluck(:email)) end + # TODO use keyword arguments # does not take into account emails def self.opted_out_users(options = {}) - Set.new(opt_outs(options).where("user_id IS NOT NULL").map(&:user)) + Set.new(opt_outs(options).where.not(user_id: nil).map(&:user)) end def self.message_verifier @message_verifier ||= ActiveSupport::MessageVerifier.new(Mailkick.secret_token) end