lib/mailkick.rb in mailkick-0.2.1 vs lib/mailkick.rb in mailkick-0.3.0

- old
+ new

@@ -1,23 +1,21 @@ require "set" -require "safely/core" require "active_support" + require "mailkick/engine" if defined?(Rails) -require "mailkick/processor" -require "mailkick/mailer" require "mailkick/model" require "mailkick/service" require "mailkick/service/mailchimp" +require "mailkick/service/mailgun" require "mailkick/service/mandrill" require "mailkick/service/sendgrid" -require "mailkick/service/mailgun" require "mailkick/version" module Mailkick mattr_accessor :services, :user_method, :secret_token, :mount self.services = [] - self.user_method = proc { |email| User.where(email: email).first rescue nil } + self.user_method = ->(email) { User.where(email: email).first rescue nil } self.mount = true def self.fetch_opt_outs services.each(&:fetch_opt_outs) end @@ -63,11 +61,11 @@ if (email = options[:email]) parts << "email = ?" binds << email end if (user = options[:user]) - parts << "user_id = ? and user_type = ?" + parts << "(user_id = ? and user_type = ?)" binds.concat [user.id, user.class.name] end relation = relation.where(parts.join(" OR "), *binds) if parts.any? relation = @@ -86,13 +84,18 @@ # 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)) end -end -ActiveSupport.on_load(:action_mailer) do - prepend Mailkick::Mailer + def self.message_verifier + @message_verifier ||= ActiveSupport::MessageVerifier.new(Mailkick.secret_token) + end + + def self.generate_token(email, list: nil) + user = Mailkick.user_method.call(email) if Mailkick.user_method + message_verifier.generate([email, user.try(:id), user.try(:class).try(:name), list]) + end end ActiveSupport.on_load(:active_record) do extend Mailkick::Model end