Class: NotifyUser::Unsubscribe

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

Class Method Summary collapse

Class Method Details

.for_target(target) ⇒ Object



18
19
20
21
# File 'app/models/notify_user/unsubscribe.rb', line 18

def self.for_target(target)
  where(target_id: target.id)
  .where(target_type: target.class.base_class)
end

.has_unsubscribed_from(target, type, group_id = nil) ⇒ Object



55
56
57
58
59
# File 'app/models/notify_user/unsubscribe.rb', line 55

def self.has_unsubscribed_from(target, type, group_id=nil)
  where(target_id: target.id)
  .where(target_type: target.class.base_class)
  .where(type: type, group_id: group_id)
end

.has_unsubscribed_from?(target, type, group_id = nil, channel_name = nil) ⇒ Boolean

checks to see if you’ve unsubscribed from the overall notification type before checking you’ve unsubscribed from the specific group_id

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'app/models/notify_user/unsubscribe.rb', line 48

def self.has_unsubscribed_from?(target, type, group_id=nil, channel_name=nil)
  return true if where(target_id: target.id, target_type: target.class.base_class).where(type: type).any?
  return true if where(target_id: target.id, target_type: target.class.base_class, type: type, group_id: group_id).any? if group_id
  return true if where(target_id: target.id, target_type: target.class.base_class, type: channel_name).any? if channel_name
  false
end

.subscribe(target, type, group_id = nil) ⇒ Object



39
40
41
42
43
44
# File 'app/models/notify_user/unsubscribe.rb', line 39

def self.subscribe(target, type, group_id=nil)
  #deletes unsubscribe object in essence subscribing a user
  where(target_id: target.id)
  .where(target_type: target.class.base_class)
  .where(type: type, group_id: group_id).destroy_all
end

.toggle_status(target, type) ⇒ Object



23
24
25
26
27
28
29
# File 'app/models/notify_user/unsubscribe.rb', line 23

def self.toggle_status(target, type)
  if NotifyUser::Unsubscribe.has_unsubscribed_from?(target, type)
    NotifyUser::Unsubscribe.subscribe(target, type)
  else
    NotifyUser::Unsubscribe.unsubscribe(target,type)
  end
end

.unsubscribe(target, type, group_id = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'app/models/notify_user/unsubscribe.rb', line 31

def self.unsubscribe(target, type, group_id=nil)
  ## creates unsubscribe object if it doesn't already exist
  unless exists?(target_id: target.id, target_type: target.class.base_class,
    type: type, group_id: group_id)
    create(target: target, type: type, group_id: group_id)
  end
end