Class: NotifyUser::BaseNotificationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/notify_user/base_notifications_controller.rb

Direct Known Subclasses

NotificationsController

Instance Method Summary collapse

Instance Method Details

#collectionObject



10
11
12
13
14
15
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 10

def collection
  @notifications = NotifyUser::BaseNotification.for_target(@user)
                                                .order("created_at DESC")
                                                .limit(30)
  collection_pagination
end

#collection_paginationObject



17
18
19
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 17

def collection_pagination
  @notifications = @notifications.page(params[:page]).per(params[:per_page])
end

#indexObject



5
6
7
8
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 5

def index
  collection                                     
  respond_to_method
end

#mark_allObject



34
35
36
37
38
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 34

def mark_all
  @notifications = NotifyUser::BaseNotification.for_target(@user).where('state IN (?)', ["pending","sent"])
  @notifications.update_all(state: :read)
  redirect_to notify_user_notifications_path  
end

#mark_readObject



28
29
30
31
32
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 28

def mark_read
  @notifications = NotifyUser::BaseNotification.for_target(@user).where('id IN (?)', params[:ids])
  @notifications.update_all(state: :read)
  render json: @notifications
end

#mass_subscriptionsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 76

def mass_subscriptions
  types = build_notification_types()
  if params[:type]
    types[:subscriptions].each do |type|
      unsubscribe = NotifyUser::Unsubscribe.has_unsubscribed_from(@user, type[:type])
      if params[:type][type[:type]] == "1"
        NotifyUser::Unsubscribe.unsubscribe(@user,type[:type])
      else
        if unsubscribe.empty?
          #if unsubscribe doesn't exist create it 
          unsubscribe = NotifyUser::Unsubscribe.create(target: @user, type: type[:type])
        end
      end
    end
    flash[:message] = "Successfully updated your notifcation settings"
  end  
  redirect_to notify_user_notifications_unsubscribe_path  
end

#notifications_countObject



40
41
42
43
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 40

def notifications_count
  @notifications = NotifyUser::BaseNotification.for_target(@user).where('state IN (?)', ["sent", "pending"])
  render json: {:count => @notifications.count}
end

#readObject

get



46
47
48
49
50
51
52
53
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 46

def read
  @notification = NotifyUser::BaseNotification.for_target(@user).where('id = ?', params[:id]).first
  unless @notification.read?
    @notification.mark_as_read!
  end

  redirect_logic(@notification)
end

#redirect_logic(notification) ⇒ Object



55
56
57
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 55

def redirect_logic(notification)
  render :text => "redirect setup goes here"
end

#respond_to_methodObject



21
22
23
24
25
26
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 21

def respond_to_method
  respond_to do |format|
    format.html
    format.json {render :json => @notifications, meta: { pagination: { per_page: @notifications.limit_value, total_pages: @notifications.total_pages, total_objects: @notifications.total_count } }}
  end
end

#subscribeObject



124
125
126
127
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 124

def subscribe
  subscribe_to(params[:type]) if params[:type]
  redirect_to notify_user_notifications_unsubscribe_path
end

#subscriptionsObject

endpoint for accessing subscriptions and their statuses



70
71
72
73
74
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 70

def subscriptions
  update_subscriptions(params[:types]) if params[:types]
  @types = build_notification_types
  render :json => @types
end

#unauth_unsubscribeObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 109

def unauth_unsubscribe
  if params[:token] && params[:type]
    if NotifyUser::UserHash.confirm_hash(params[:token], params[:type])
      user_hash = NotifyUser::UserHash.where(token: params[:token], type: params[:type]).first
      @user = user_hash.target
      unsubscribe = NotifyUser::Unsubscribe.create(target: @user, type: params[:type])
      user_hash.deactivate
      return render :text => "successfully unsubscribed from #{params[:type]} notifications"
    else
      return render :text => "invalid token"
    end
  end
  return render :text => "Something went wrong please try again later"
end

#unsubscribeObject



59
60
61
62
63
64
65
66
67
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 59

def unsubscribe
  if params[:type]
    unsubscribe_from(params[:type])
    redirect_to notify_user_notifications_unsubscribe_path  
  end
  @types = build_notification_types
  @unsubscribale_types = NotifyUser.unsubscribable_notifications
  @unsubscribale_channels = NotifyUser::BaseNotification.channels
end

#update_subscriptions(types) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/notify_user/base_notifications_controller.rb', line 95

def update_subscriptions(types)
  types.each do |type|
    unsubscribe = NotifyUser::Unsubscribe.has_unsubscribed_from(@user, type[:type])
    if type[:status] == '0'
      if unsubscribe.empty?
        #if unsubscribe doesn't exist create it 
        unsubscribe = NotifyUser::Unsubscribe.create(target: @user, type: type[:type])
      end
    else
      subscribe_to(type[:type])
    end
  end 
end