Class: EmailControllerHelper::TopicEmailUnsubscriber

Inherits:
BaseEmailUnsubscriber show all
Defined in:
lib/email_controller_helper/topic_email_unsubscriber.rb

Instance Attribute Summary

Attributes inherited from BaseEmailUnsubscriber

#unsubscribe_key

Instance Method Summary collapse

Methods inherited from BaseEmailUnsubscriber

#initialize

Constructor Details

This class inherits a constructor from EmailControllerHelper::BaseEmailUnsubscriber

Instance Method Details

#prepare_unsubscribe_options(controller) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/email_controller_helper/topic_email_unsubscriber.rb', line 5

def prepare_unsubscribe_options(controller)
  super(controller)
  watching = TopicUser.notification_levels[:watching]

  topic = unsubscribe_key.associated_topic

  return if topic.blank?

  controller.instance_variable_set(:@topic, topic)
  controller.instance_variable_set(
    :@watching_topic,
    TopicUser.exists?(user: key_owner, notification_level: watching, topic_id: topic.id),
  )

  return if topic.category_id.blank?
  if !CategoryUser.exists?(
       user: key_owner,
       notification_level: CategoryUser.watching_levels,
       category_id: topic.category_id,
     )
    return
  end

  controller.instance_variable_set(
    :@watched_count,
    TopicUser
      .joins(:topic)
      .where(user: key_owner, notification_level: watching)
      .where(topics: { category_id: topic.category_id })
      .count,
  )
end

#unsubscribe(params) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/email_controller_helper/topic_email_unsubscriber.rb', line 38

def unsubscribe(params)
  updated = super(params)

  topic = unsubscribe_key.associated_topic
  return updated if topic.nil?

  if params[:unwatch_topic]
    TopicUser.where(topic_id: topic.id, user_id: key_owner.id).update_all(
      notification_level: TopicUser.notification_levels[:tracking],
    )

    updated = true
  end

  if params[:unwatch_category] && topic.category_id
    TopicUser
      .joins(:topic)
      .where(user: key_owner, notification_level: TopicUser.notification_levels[:watching])
      .where(topics: { category_id: topic.category_id })
      .update_all(notification_level: TopicUser.notification_levels[:tracking])

    CategoryUser.where(
      user_id: key_owner.id,
      category_id: topic.category_id,
      notification_level: CategoryUser.watching_levels,
    ).destroy_all

    updated = true
  end

  if params[:mute_topic]
    TopicUser.where(topic_id: topic.id, user_id: key_owner.id).update_all(
      notification_level: TopicUser.notification_levels[:muted],
    )

    updated = true
  end

  updated
end