Module: Mailkick::Legacy

Defined in:
lib/mailkick/legacy.rb

Class Method Summary collapse

Class Method Details

.opt_in(options) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mailkick/legacy.rb', line 30

def self.opt_in(options)
  opt_outs(options).each do |opt_out|
    opt_out.active = false
    opt_out.save!
  end
  true
end

.opt_out(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mailkick/legacy.rb', line 15

def self.opt_out(options)
  unless opted_out?(options)
    time = options[:time] || Time.now
    Mailkick::OptOut.create! do |o|
      o.email = options[:email]
      o.user = options[:user]
      o.reason = options[:reason] || "unsubscribe"
      o.list = options[:list]
      o.created_at = time
      o.updated_at = time
    end
  end
  true
end

.opt_outs(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mailkick/legacy.rb', line 38

def self.opt_outs(options = {})
  relation = Mailkick::OptOut.where(active: true)

  contact_relation = Mailkick::OptOut.none
  if (email = options[:email])
    contact_relation = contact_relation.or(Mailkick::OptOut.where(email: email))
  end
  if (user = options[:user])
    contact_relation = contact_relation.or(
      Mailkick::OptOut.where("user_id = ? AND user_type = ?", user.id, user.class.name)
    )
  end
  relation = relation.merge(contact_relation) if email || user

  relation =
    if options[:list]
      relation.where("list IS NULL OR list = ?", options[:list])
    else
      relation.where("list IS NULL")
    end

  relation
end

.opt_outs?Boolean

checks for table as long as it exists

Returns:

  • (Boolean)


4
5
6
7
8
9
# File 'lib/mailkick/legacy.rb', line 4

def self.opt_outs?
  unless defined?(@opt_outs) && @opt_outs == false
    @opt_outs = ActiveRecord::Base.connection.table_exists?("mailkick_opt_outs")
  end
  @opt_outs
end

.opted_out?(options) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/mailkick/legacy.rb', line 11

def self.opted_out?(options)
  opt_outs(options).any?
end

.opted_out_emails(options = {}) ⇒ Object



62
63
64
# File 'lib/mailkick/legacy.rb', line 62

def self.opted_out_emails(options = {})
  Set.new(opt_outs(options).where.not(email: nil).distinct.pluck(:email))
end

.opted_out_users(options = {}) ⇒ Object



66
67
68
# File 'lib/mailkick/legacy.rb', line 66

def self.opted_out_users(options = {})
  Set.new(opt_outs(options).where.not(user_id: nil).map(&:user))
end