Class: UnsubscribeKey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/unsubscribe_key.rb

Constant Summary collapse

ALL_TYPE =
"all"
DIGEST_TYPE =
"digest"
TOPIC_TYPE =
"topic"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_key_for(user, type, post: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/unsubscribe_key.rb', line 15

def create_key_for(user, type, post: nil)
  unsubscribe_key = new(user_id: user.id, unsubscribe_key_type: type)

  if type == TOPIC_TYPE
    unsubscribe_key.topic_id = post.topic_id
    unsubscribe_key.post_id = post.id
  end

  unsubscribe_key.save!
  unsubscribe_key.key
end

.get_unsubscribe_strategy_for(key) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/unsubscribe_key.rb', line 31

def get_unsubscribe_strategy_for(key)
  strategies = {
    DIGEST_TYPE => EmailControllerHelper::DigestEmailUnsubscriber,
    TOPIC_TYPE => EmailControllerHelper::TopicEmailUnsubscriber,
    ALL_TYPE => EmailControllerHelper::BaseEmailUnsubscriber,
  }

  DiscoursePluginRegistry.email_unsubscribers.each do |unsubcriber|
    strategies.merge!(unsubcriber)
  end

  strategies[key.unsubscribe_key_type]&.new(key)
end

.user_for_key(key) ⇒ Object



27
28
29
# File 'app/models/unsubscribe_key.rb', line 27

def user_for_key(key)
  where(key: key).first&.user
end

Instance Method Details

#associated_topicObject



46
47
48
# File 'app/models/unsubscribe_key.rb', line 46

def associated_topic
  @associated_topic ||= topic || post&.topic
end