Module: Sinatra::NotificationHandler

Extended by:
Hexacta
Defined in:
lib/sinatra/handlers/notifications.rb

Constant Summary

Constants included from Hexacta

Hexacta::GEM_FILE_DIR

Instance Method Summary collapse

Methods included from Hexacta

copy_all_files, copy_dir_structure, copy_file, gem_path, setup_dir

Instance Method Details

#enable_notificationsObject



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
37
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/sinatra/handlers/notifications.rb', line 6

def enable_notifications
  p "Enabling notifications..."
  NotificationHandler.setup_dir("/app/views/#{Hexacta::GEM_FILE_DIR}/notifications")
  NotificationHandler.copy_file("/lib/sinatra/views/notifications.slim","/app/views/#{Hexacta::GEM_FILE_DIR}/notifications.slim")
  NotificationHandler.copy_all_files("/lib/sinatra/views/notifications","/app/views/#{Hexacta::GEM_FILE_DIR}/notifications")

  post '/notification' do
    if params[:user_ids].blank?
      NotificationSender.instance.send_to_subscriptors(authenticated(User),params[:title],params[:message],params[:label])
    else
      for id in params[:user_ids]
        NotificationSender.instance.send_to(User.find(:id => id),authenticated(User),params[:title],params[:message],params[:label])
      end
    end
    redirect back
  end

  post '/notifications/:id' do |id|
    Notification.where(:user_id => authenticated(User).id, :id => id).update(:read_date => Date.today)
    200
  end

  post '/notifications' do
    Notification.where(:user_id => authenticated(User).id, :id => params[:ids]).update(:read_date => Date.today)
    redirect back
  end

  get '/notifications/:id' do |id|
    notification = Notification.find(:id => id)
    redirect "/notifications" if notification.nil?
    slim "#{Hexacta::GEM_FILE_DIR}/notifications/view".to_sym, locals: { :notification => notification }
  end

  delete '/notifications/:id' do |id|
    notification = Notification.find(:user_id => authenticated(User).id, :id => id)
    error(404) if notification.nil?
    notification.destroy.to_hash.to_json.to_s
  end

  delete '/notifications' do
    Notification.where(:user_id => authenticated(User).id, :id => params[:ids]).destroy.to_s
  end

  get '/notifications' do
    filters = params.select { |attribute| Notification.columns.include?(attribute.to_sym) }
    params[:limit] = "20" if params[:limit].to_i < 20
    params[:limit] = "100" if params[:limit].to_i > 100
    limit = params[:limit].to_i
    offset = params[:offset].to_i

    query = Notification.where(:user_id => authenticated(User).id).where(filters.to_filter).order(Sequel.desc(:id))

    slim "#{Hexacta::GEM_FILE_DIR}/notifications".to_sym, locals: { :notifications => query.limit(limit).offset(offset*limit).all, :total => query.count, :filters => params, :query => query }
  end
end