rails_notice 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +165 -0
  3. data/README.md +76 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/rails_notice_manifest.js +3 -0
  6. data/app/assets/javascripts/controllers/notifications/index.js +0 -0
  7. data/app/assets/javascripts/controllers/rails_notice_admin/notification_settings/index.js +17 -0
  8. data/app/assets/javascripts/controllers/rails_notice_admin/notifications/index.js +15 -0
  9. data/app/assets/javascripts/controllers/rails_notice_admin/notify_settings/edit.js +3 -0
  10. data/app/assets/javascripts/controllers/rails_notice_admin/notify_settings/index.js +17 -0
  11. data/app/assets/javascripts/controllers/rails_notice_admin/notify_settings/new.js +18 -0
  12. data/app/assets/javascripts/rails_notice/cable.js +19 -0
  13. data/app/assets/javascripts/rails_notice/channels/notices.js +39 -0
  14. data/app/assets/javascripts/rails_notice/messenger-theme-future.js +33 -0
  15. data/app/assets/javascripts/rails_notice/messenger.min.js +2 -0
  16. data/app/assets/stylesheets/the_notify/cable.css +9 -0
  17. data/app/assets/stylesheets/the_notify/messenger-theme-air.css +357 -0
  18. data/app/assets/stylesheets/the_notify/messenger.css +105 -0
  19. data/app/channels/notices_channel.rb +7 -0
  20. data/app/channels/the_notify_connection.rb +30 -0
  21. data/app/controllers/concerns/rails_notice_controller.rb +18 -0
  22. data/app/controllers/rails_notice_admin/base_controller.rb +6 -0
  23. data/app/controllers/rails_notice_admin/notification_settings_controller.rb +55 -0
  24. data/app/controllers/rails_notice_admin/notifications_controller.rb +71 -0
  25. data/app/controllers/rails_notice_my/base_controller.rb +5 -0
  26. data/app/controllers/rails_notice_my/notification_settings_controller.rb +28 -0
  27. data/app/controllers/rails_notice_my/notifications_controller.rb +78 -0
  28. data/app/controllers/receivers_controller.rb +12 -0
  29. data/app/jobs/notification_job.rb +17 -0
  30. data/app/mailers/rails_notice_mailer.rb +21 -0
  31. data/app/models/annunciation.rb +4 -0
  32. data/app/models/concerns/i18n_helper.rb +14 -0
  33. data/app/models/concerns/the_notifiable.rb +33 -0
  34. data/app/models/concerns/the_publishable.rb +9 -0
  35. data/app/models/concerns/the_receivable.rb +17 -0
  36. data/app/models/notification.rb +156 -0
  37. data/app/models/notification_setting.rb +8 -0
  38. data/app/views/rails_notice/_link.html.erb +6 -0
  39. data/app/views/rails_notice_admin/base/_nav.html.erb +16 -0
  40. data/app/views/rails_notice_admin/notification_settings/_form.html.erb +7 -0
  41. data/app/views/rails_notice_admin/notification_settings/_search_form.html.erb +11 -0
  42. data/app/views/rails_notice_admin/notification_settings/edit.html.erb +14 -0
  43. data/app/views/rails_notice_admin/notification_settings/index.html.erb +52 -0
  44. data/app/views/rails_notice_admin/notification_settings/new.html.erb +13 -0
  45. data/app/views/rails_notice_admin/notification_settings/show.html.erb +0 -0
  46. data/app/views/rails_notice_admin/notifications/_form.html.erb +6 -0
  47. data/app/views/rails_notice_admin/notifications/_menu.html.erb +6 -0
  48. data/app/views/rails_notice_admin/notifications/_search_form.html.erb +11 -0
  49. data/app/views/rails_notice_admin/notifications/edit.html.erb +14 -0
  50. data/app/views/rails_notice_admin/notifications/index.html.erb +44 -0
  51. data/app/views/rails_notice_admin/notifications/new.html.erb +15 -0
  52. data/app/views/rails_notice_mailer/notify.html.erb +35 -0
  53. data/app/views/rails_notice_my/base/_nav_my.html.erb +4 -0
  54. data/app/views/rails_notice_my/notification_settings/_form.html.erb +5 -0
  55. data/app/views/rails_notice_my/notification_settings/show.html.erb +5 -0
  56. data/app/views/rails_notice_my/notifications/_navbar.html.erb +13 -0
  57. data/app/views/rails_notice_my/notifications/_notifications.html.erb +17 -0
  58. data/app/views/rails_notice_my/notifications/edit.html.erb +2 -0
  59. data/app/views/rails_notice_my/notifications/index.html.erb +28 -0
  60. data/app/views/rails_notice_my/notifications/index.js.erb +13 -0
  61. data/app/views/rails_notice_my/notifications/new.html.erb +1 -0
  62. data/app/views/rails_notice_my/notifications/read.js.erb +2 -0
  63. data/app/views/rails_notice_my/notifications/read_all.js.erb +2 -0
  64. data/app/views/rails_notice_my/notifications/show.html.erb +7 -0
  65. data/config/locales/en.yml +7 -0
  66. data/config/locales/zh.yml +7 -0
  67. data/config/routes.rb +29 -0
  68. data/db/migrate/20170110025557_create_notification.rb +21 -0
  69. data/db/migrate/20170209025946_create_notification_settings.rb +23 -0
  70. data/lib/rails_notice.rb +13 -0
  71. data/lib/rails_notice/active_record.rb +18 -0
  72. data/lib/rails_notice/config.rb +13 -0
  73. data/lib/rails_notice/engine.rb +9 -0
  74. data/lib/rails_notice/version.rb +3 -0
  75. data/lib/tasks/the_notify_tasks.rake +4 -0
  76. metadata +132 -0
@@ -0,0 +1,7 @@
1
+ class NoticesChannel < ApplicationCable::Channel
2
+
3
+ def subscribed
4
+ stream_from "#{current_receiver.class.name}:#{current_receiver&.id}"
5
+ end
6
+
7
+ end
@@ -0,0 +1,30 @@
1
+ # prepend this module
2
+ module RailsNoticeConnection
3
+
4
+ def self.prepended(model)
5
+ model.identified_by :current_receiver
6
+ end
7
+
8
+ def connect
9
+ self.current_receiver = find_verified_receiver
10
+ super
11
+ end
12
+
13
+ protected
14
+ def find_verified_receiver
15
+ if session && session['receiver_id'] && session['receiver_type']
16
+ Rails.logger.silence do
17
+ session['receiver_type'].constantize.find session['receiver_id']
18
+ end
19
+ else
20
+ logger.error 'An unauthorized connection attempt was rejected'
21
+ nil
22
+ end
23
+ end
24
+
25
+ def session
26
+ session_key = Rails.configuration.session_options[:key]
27
+ cookies.encrypted[session_key]
28
+ end
29
+
30
+ end
@@ -0,0 +1,18 @@
1
+ module RailsNoticeController
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :set_receiver
6
+ helper_method :current_receiver
7
+ end
8
+
9
+ def set_receiver
10
+ session['receiver_type'] = current_receiver.class.name
11
+ session['receiver_id'] = current_receiver&.id
12
+ end
13
+
14
+ def current_receiver
15
+ current_user
16
+ end
17
+
18
+ end
@@ -0,0 +1,6 @@
1
+ class RailsNoticeAdmin::BaseController < RailsNotice.config.admin_class.constantize
2
+ #skip_before_action :verify_authenticity_token#, if: -> { request.content_type == 'application/json' }
3
+
4
+
5
+
6
+ end
@@ -0,0 +1,55 @@
1
+ class RailsNoticeAdmin::NotificationSettingsController < RailsNoticeAdmin::BaseController
2
+ before_action :set_notification_setting, only: [:show, :edit, :update, :destroy]
3
+
4
+ def index
5
+ q_params = params.fetch(:q, {}).permit(:id, :receiver_type, :receiver_id)
6
+ @notification_settings = NotificationSetting.default_where(q_params).page(params[:page])
7
+ end
8
+
9
+ def new
10
+ @notification_setting = NotificationSetting.new
11
+ end
12
+
13
+ def create
14
+ @notification_setting = NotificationSetting.new(notification_setting_params)
15
+
16
+ if @notification_setting.save
17
+ redirect_to admin_notification_settings_url, notice: 'Notification setting was successfully created.'
18
+ else
19
+ render :new
20
+ end
21
+ end
22
+
23
+ def show
24
+ end
25
+
26
+ def edit
27
+ end
28
+
29
+ def update
30
+ respond_to do |format|
31
+ if @notification_setting.update(notification_setting_params)
32
+ format.html { redirect_to admin_notification_settings_url, notice: 'Notification setting was successfully updated.' }
33
+ format.js
34
+ else
35
+ format.html { render :edit }
36
+ format.js
37
+ end
38
+ end
39
+ end
40
+
41
+ def destroy
42
+ @notification_setting.destroy
43
+ redirect_to admin_notification_settings_url, notice: 'Notification setting was successfully destroyed.'
44
+ end
45
+
46
+ private
47
+ def set_notification_setting
48
+ @notification_setting = NotificationSetting.find(params[:id])
49
+ end
50
+
51
+ def notification_setting_params
52
+ params.fetch(:notification_setting, {}).permit(:showtime, :accept_email, notifiable_types: [])
53
+ end
54
+
55
+ end
@@ -0,0 +1,71 @@
1
+ class RailsNoticeAdmin::NotificationsController < RailsNoticeAdmin::BaseController
2
+ before_action :set_receiver, only: [:index, :new, :create]
3
+ before_action :set_notification, only: [:show, :push, :email, :edit, :update, :destroy]
4
+
5
+ def index
6
+ if @receiver
7
+ @notifications = @receiver.notifications.page(params[:page])
8
+ else
9
+ @notifications = Notification.default_where(params.fetch(:q, {}).permit(:id, :'body-like', :receiver_type, :receiver_id)).page(params[:page])
10
+ end
11
+ end
12
+
13
+ def show
14
+ end
15
+
16
+ def push
17
+ @notification.process_job
18
+ head :ok
19
+ end
20
+
21
+ def email
22
+ @notification.send_email
23
+ head :ok
24
+ end
25
+
26
+ def new
27
+ @notification = @receiver.notifications.build
28
+ end
29
+
30
+ def edit
31
+ end
32
+
33
+ def create
34
+ @notification = @receiver.notifications.build(notification_params)
35
+
36
+ if @notification.save
37
+ redirect_to admin_notifications_url(receiver_id: @receiver.id, receiver_type: @receiver.class.name), notice: 'User notification was successfully created.'
38
+ else
39
+ render :new
40
+ end
41
+ end
42
+
43
+ def update
44
+ if @notification.update(notification_params)
45
+ redirect_to admin_notifications_url(receiver_id: @receiver.id, receiver_type: @receiver.class.name), notice: 'User notification was successfully updated.'
46
+ else
47
+ render :edit
48
+ end
49
+ end
50
+
51
+ def destroy
52
+ @notification.destroy
53
+ redirect_to admin_notifications_url(receiver_id: @receiver.id, receiver_type: @receiver.class.name), notice: 'User notification was successfully destroyed.'
54
+ end
55
+
56
+ private
57
+ def set_receiver
58
+ if params[:receiver_type]
59
+ @receiver = params[:receiver_type].constantize.find(params[:receiver_id])
60
+ end
61
+ end
62
+
63
+ def set_notification
64
+ @notification = Notification.find(params[:id])
65
+ end
66
+
67
+ def notification_params
68
+ params[:notification].permit(:title, :body, :link)
69
+ end
70
+
71
+ end
@@ -0,0 +1,5 @@
1
+ class RailsNoticeMy::BaseController < RailsNotice.config.my_class.constantize
2
+
3
+
4
+
5
+ end
@@ -0,0 +1,28 @@
1
+ class RailsNoticeMy::NotificationSettingsController < RailsNoticeMy::BaseController
2
+ before_action :set_notification_setting, only: [:show, :edit, :update]
3
+
4
+ def show
5
+ end
6
+
7
+ def edit
8
+ end
9
+
10
+ def update
11
+ if @notification_setting.update(notification_setting_params)
12
+ redirect_to notification_settings_url(receiver: params[:receiver]), notice: 'Notification setting was successfully updated.'
13
+ else
14
+ render :edit
15
+ end
16
+ end
17
+
18
+ private
19
+ def set_notification_setting
20
+ @receiver = current_receiver
21
+ @notification_setting = @receiver.notification_setting || @receiver.build_notification_setting
22
+ end
23
+
24
+ def notification_setting_params
25
+ params.fetch(:notification_setting, {}).permit(:showtime, :accept_email)
26
+ end
27
+
28
+ end
@@ -0,0 +1,78 @@
1
+ class RailsNoticeMy::NotificationsController < RailsNoticeMy::BaseController
2
+ before_action :set_notification, only: [:show, :url, :read, :edit, :update, :destroy]
3
+ before_action :set_receiver, only: [:index, :read_all]
4
+ protect_from_forgery except: :read
5
+
6
+ def index
7
+ @notifications = @receiver.received_notifications.order(read_at: :asc)
8
+ if params[:scope] == 'have_read'
9
+ @notifications = @notifications.have_read
10
+ else
11
+ @notifications = @notifications.unread
12
+ end
13
+ @notifications = @notifications.page(params[:page])
14
+
15
+ respond_to do |format|
16
+ format.html
17
+ format.js
18
+ end
19
+ end
20
+
21
+ def read_all
22
+ @notifications = @receiver.received_notifications
23
+ @notifications.update_all(read_at: Time.now)
24
+ @count = Notification.update_unread_count(@receiver)
25
+ end
26
+
27
+ def new
28
+ @notification = Notification.new
29
+ end
30
+
31
+ def create
32
+ @notification = Notification.new(params[:notification].permit!)
33
+
34
+ if @notification.save
35
+ redirect_to(notifications_path, notice: 'Notification 创建成功。')
36
+ else
37
+ render action: 'new'
38
+ end
39
+ end
40
+
41
+ def show
42
+ end
43
+
44
+ def url
45
+ @notification.make_as_read
46
+ redirect_to @notification.link
47
+ end
48
+
49
+ def read
50
+ @notification.make_as_read
51
+ end
52
+
53
+ def edit
54
+ end
55
+
56
+ def update
57
+ if @notification.update(params[:notification].permit!)
58
+ redirect_to(notifications_path, notice: 'Notification 更新成功。')
59
+ else
60
+ render action: 'edit'
61
+ end
62
+ end
63
+
64
+ def destroy
65
+ @notification.destroy
66
+ redirect_to(notifications_path, notice: "删除成功。")
67
+ end
68
+
69
+ private
70
+ def set_receiver
71
+ @receiver = current_receiver
72
+ end
73
+
74
+ def set_notification
75
+ @notification = Notification.find(params[:id])
76
+ end
77
+
78
+ end
@@ -0,0 +1,12 @@
1
+ class ReceiversController < ApplicationController
2
+
3
+ def search
4
+ if Notification.options_i18n(:receiver_type).values.include?(params[:receiver_type].to_sym) && params[:receiver_type].safe_constantize
5
+ @receivers = params[:receiver_type].safe_constantize.default_where('name-like': params[:q])
6
+ render json: { results: @receivers.as_json(only: [:id], methods: [:name]) }
7
+ else
8
+ render json: { results: [] }
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,17 @@
1
+ class NotificationJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(notification_id)
5
+ notify = Notification.find(notification_id)
6
+ if notify.receiver
7
+ result = ActionCable.server.broadcast "#{notify.receiver_type}:#{notify.receiver_id}",
8
+ id: notify.id,
9
+ body: notify.body,
10
+ count: notify.unread_count,
11
+ link: notify.link,
12
+ showtime: notify.notification_setting&.showtime
13
+ notify.update sent_at: Time.now, sent_result: result
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,21 @@
1
+ class RailsNoticeMailer < ApplicationMailer
2
+ add_template_helper(RailsCom::FormatHelper)
3
+
4
+ def notify(notification_id)
5
+ @notification = Notification.find(notification_id)
6
+ unless @notification.receiver.respond_to?(:email) && @notification.receiver.email
7
+ return
8
+ end
9
+ if @notification.receiver.respond_to?(:locale)
10
+ I18n.locale = @notification.receiver.locale
11
+ end
12
+
13
+ mail_params = {to: @notification.receiver.email,
14
+ cc: @notification.cc_emails,
15
+ subject: @notification.title || 'Notification'}
16
+ mail_params[:from] = @notification.sender.email if @notification.sender && @notification.sender.respond_to?(:email) && @notification.sender.email
17
+
18
+ mail mail_params
19
+ end
20
+
21
+ end
@@ -0,0 +1,4 @@
1
+ class Annunciation < ApplicationRecord
2
+ belongs_to :publisher, polymorphic: true
3
+
4
+ end
@@ -0,0 +1,14 @@
1
+ module I18nHelper
2
+ extend self
3
+
4
+ def interpolate_key(string)
5
+ string.gsub(I18n::INTERPOLATION_PATTERN).map do |match|
6
+ if match == '%%'
7
+ next
8
+ else
9
+ ($1 || $2 || match.tr('%{}', '')).to_sym
10
+ end
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,33 @@
1
+ module TheNotifiable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ class_attribute :notifies, default: {}
6
+
7
+ delegate :url_helpers, to: 'Rails.application.routes'
8
+
9
+ has_many :notifications, as: 'notifiable', dependent: :delete_all
10
+
11
+ RailsNotice.notifiable_types << self.name unless RailsNotice.notifiable_types.include?(self.name)
12
+ end
13
+
14
+ def to_notification(receiver: , **other_params)
15
+ n = self.notifications.build
16
+ n.receiver_id = receiver.id
17
+ n.receiver_type = receiver.class
18
+
19
+ if other_params[:sender]
20
+ n.sender_id = other_params[:sender].id
21
+ n.sender_type = other_params[:sender].class
22
+ end
23
+
24
+ n.assign_attributes other_params.slice(
25
+ :title, :body, :link,
26
+ :verbose, :code,
27
+ :cc_emails
28
+ )
29
+
30
+ n.save!
31
+ end
32
+
33
+ end
@@ -0,0 +1,9 @@
1
+ module ThePublishable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ Annunciation.belongs_to :publisher, class_name: name, foreign_key: 'publisher_id', optional: true, inverse_of: :notifications
6
+ has_many :published_notifications, class_name: 'Notification', foreign_key: 'publisher_id', dependent: :nullify
7
+ end
8
+
9
+ end
@@ -0,0 +1,17 @@
1
+ module TheReceivable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ has_many :received_notifications, as: 'receiver', class_name: 'Notification', dependent: :destroy
6
+ has_one :notification_setting, as: 'receiver', dependent: :destroy
7
+ end
8
+
9
+ def unread_count
10
+ Rails.cache.read("#{self.class.name}_#{self.id}_unread") || 0
11
+ end
12
+
13
+ def endearing_name
14
+ name
15
+ end
16
+
17
+ end