notify_on 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +29 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/notify_on_manifest.js +2 -0
  6. data/app/assets/javascripts/notify_on/application.js +13 -0
  7. data/app/assets/stylesheets/notify_on/application.css +15 -0
  8. data/app/controllers/notify_on/application_controller.rb +5 -0
  9. data/app/helpers/notify_on/application_helper.rb +4 -0
  10. data/app/helpers/notify_on/mailer_helper.rb +9 -0
  11. data/app/jobs/notify_on/application_job.rb +4 -0
  12. data/app/mailers/notify_on/application_mailer.rb +6 -0
  13. data/app/mailers/notify_on/notification_mailer.rb +26 -0
  14. data/app/models/concerns/notify_on/email_support.rb +97 -0
  15. data/app/models/concerns/notify_on/pusher_support.rb +66 -0
  16. data/app/models/concerns/notify_on/string_interpolation.rb +39 -0
  17. data/app/models/notify_on/application_record.rb +5 -0
  18. data/app/models/notify_on/notification.rb +86 -0
  19. data/app/views/layouts/notify_on/application.html.erb +17 -0
  20. data/app/views/notifications/notify.html.erb +9 -0
  21. data/config/database.travis.yml +4 -0
  22. data/config/routes.rb +2 -0
  23. data/lib/generators/notify_on/install_generator.rb +29 -0
  24. data/lib/generators/templates/notifications.yml +19 -0
  25. data/lib/generators/templates/notify_on.rb +64 -0
  26. data/lib/notify_on.rb +29 -0
  27. data/lib/notify_on/bulk_config.rb +36 -0
  28. data/lib/notify_on/configuration.rb +22 -0
  29. data/lib/notify_on/creator.rb +85 -0
  30. data/lib/notify_on/engine.rb +19 -0
  31. data/lib/notify_on/notify_on.rb +36 -0
  32. data/lib/notify_on/receives_notifications.rb +11 -0
  33. data/lib/notify_on/utilities.rb +13 -0
  34. data/lib/notify_on/version.rb +3 -0
  35. data/lib/tasks/notify_on_tasks.rake +4 -0
  36. data/spec/dummy/Rakefile +6 -0
  37. data/spec/dummy/app/assets/config/manifest.js +5 -0
  38. data/spec/dummy/app/assets/javascripts/application.js +7 -0
  39. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  40. data/spec/dummy/app/assets/stylesheets/application.scss +28 -0
  41. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  42. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  43. data/spec/dummy/app/controllers/application_controller.rb +7 -0
  44. data/spec/dummy/app/controllers/messages_controller.rb +31 -0
  45. data/spec/dummy/app/helpers/application_helper.rb +8 -0
  46. data/spec/dummy/app/jobs/application_job.rb +2 -0
  47. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  48. data/spec/dummy/app/mailers/notification_mailer.rb +15 -0
  49. data/spec/dummy/app/models/application_record.rb +3 -0
  50. data/spec/dummy/app/models/comment.rb +10 -0
  51. data/spec/dummy/app/models/message.rb +45 -0
  52. data/spec/dummy/app/models/post.rb +11 -0
  53. data/spec/dummy/app/models/user.rb +21 -0
  54. data/spec/dummy/app/views/application/_header.html.erb +44 -0
  55. data/spec/dummy/app/views/application/home.html.erb +1 -0
  56. data/spec/dummy/app/views/layouts/application.html.erb +19 -0
  57. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  58. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  59. data/spec/dummy/app/views/messages/_message.html.erb +6 -0
  60. data/spec/dummy/app/views/messages/index.html.erb +33 -0
  61. data/spec/dummy/app/views/messages/new.html.erb +5 -0
  62. data/spec/dummy/app/views/messages/show.html.erb +7 -0
  63. data/spec/dummy/app/views/notifications/new_message.html.erb +9 -0
  64. data/spec/dummy/bin/bundle +3 -0
  65. data/spec/dummy/bin/rails +4 -0
  66. data/spec/dummy/bin/rake +4 -0
  67. data/spec/dummy/bin/setup +34 -0
  68. data/spec/dummy/bin/update +29 -0
  69. data/spec/dummy/config.ru +5 -0
  70. data/spec/dummy/config/application.rb +35 -0
  71. data/spec/dummy/config/boot.rb +5 -0
  72. data/spec/dummy/config/cable.yml +9 -0
  73. data/spec/dummy/config/database.yml +12 -0
  74. data/spec/dummy/config/environment.rb +5 -0
  75. data/spec/dummy/config/environments/development.rb +58 -0
  76. data/spec/dummy/config/environments/production.rb +86 -0
  77. data/spec/dummy/config/environments/test.rb +42 -0
  78. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  79. data/spec/dummy/config/initializers/assets.rb +11 -0
  80. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  81. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  82. data/spec/dummy/config/initializers/devise.rb +274 -0
  83. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  84. data/spec/dummy/config/initializers/inflections.rb +16 -0
  85. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  86. data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
  87. data/spec/dummy/config/initializers/notify_on.rb +64 -0
  88. data/spec/dummy/config/initializers/session_store.rb +3 -0
  89. data/spec/dummy/config/initializers/simple_form.rb +165 -0
  90. data/spec/dummy/config/initializers/simple_form_bootstrap.rb +149 -0
  91. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  92. data/spec/dummy/config/locales/devise.en.yml +62 -0
  93. data/spec/dummy/config/locales/en.yml +23 -0
  94. data/spec/dummy/config/locales/simple_form.en.yml +31 -0
  95. data/spec/dummy/config/notifications.yml +20 -0
  96. data/spec/dummy/config/puma.rb +47 -0
  97. data/spec/dummy/config/routes.rb +14 -0
  98. data/spec/dummy/config/secrets.yml +22 -0
  99. data/spec/dummy/config/spring.rb +6 -0
  100. data/spec/dummy/db/migrate/20160801112429_devise_create_users.rb +42 -0
  101. data/spec/dummy/db/migrate/20160801130804_create_messages.rb +11 -0
  102. data/spec/dummy/db/migrate/20160823102904_create_notify_on_notifications.rb +20 -0
  103. data/spec/dummy/db/migrate/20161021100707_create_comments.rb +11 -0
  104. data/spec/dummy/db/migrate/20161021100842_create_posts.rb +11 -0
  105. data/spec/dummy/db/schema.rb +77 -0
  106. data/spec/dummy/db/seeds.rb +4 -0
  107. data/spec/dummy/lib/templates/erb/scaffold/_form.html.erb +13 -0
  108. data/spec/dummy/public/404.html +67 -0
  109. data/spec/dummy/public/422.html +67 -0
  110. data/spec/dummy/public/500.html +66 -0
  111. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  112. data/spec/dummy/public/apple-touch-icon.png +0 -0
  113. data/spec/dummy/public/favicon.ico +0 -0
  114. data/spec/dummy/spec/controllers/messages_controller_spec.rb +5 -0
  115. data/spec/dummy/spec/factories/comments.rb +7 -0
  116. data/spec/dummy/spec/factories/messages.rb +7 -0
  117. data/spec/dummy/spec/factories/posts.rb +6 -0
  118. data/spec/dummy/spec/factories/users.rb +6 -0
  119. data/spec/dummy/spec/features/send_message_spec.rb +25 -0
  120. data/spec/dummy/spec/mailers/notify_on/notification_mailer_spec.rb +30 -0
  121. data/spec/dummy/spec/mailers/previews/notification_mailer_preview.rb +4 -0
  122. data/spec/dummy/spec/models/comment_spec.rb +22 -0
  123. data/spec/dummy/spec/models/message_spec.rb +60 -0
  124. data/spec/dummy/spec/models/post_spec.rb +23 -0
  125. data/spec/dummy/spec/models/user_spec.rb +21 -0
  126. data/spec/factories/notify_on_notifications.rb +10 -0
  127. data/spec/lib/notify_on/configuration_spec.rb +53 -0
  128. data/spec/mailers/notify_on/notification_mailer_spec.rb +6 -0
  129. data/spec/mailers/previews/notify_on/notification_mailer_preview.rb +6 -0
  130. data/spec/models/notify_on/notification_spec.rb +335 -0
  131. data/spec/rails_helper.rb +95 -0
  132. data/spec/spec_helper.rb +103 -0
  133. data/spec/support/feature_helpers.rb +19 -0
  134. data/spec/support/general_helpers.rb +19 -0
  135. metadata +543 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 522b65290d13dbc3fcde080221b2e3ab63dd8567
4
+ data.tar.gz: c88fa1da0714593511218f5159679ccf9e223421
5
+ SHA512:
6
+ metadata.gz: 9f50b96b159f20bbe1165013bc1337ec409078adaf2ba1081cd79f86d7a09036f7fb123698f1671a0bd566b736507f350721d5d9d0dd9258e985da423dd57e85
7
+ data.tar.gz: 309847d668e04e6d0485228be4a39d738030d30e36ff6996cde96c043b1e45390018682f8d49490b7adf85f0b834a747f41760144373acc3a483cc598bdbc1b3
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Mile Wille, Sean C Davis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ NotifyOn
2
+ ==========
3
+
4
+ ![](https://api.travis-ci.org/BrilliantChemistry/NotifyOn.svg?branch=overhaul)
5
+
6
+ NotifyOn generates automatic notifications as a result of state changes on a
7
+ particular model in a Rails application. It supports email messages, along with
8
+ third-party real-time delivery, while also storing each notification in your
9
+ database so you can use, adjust, and display as you'd wish.
10
+
11
+ Documentation can be found in [the wiki
12
+ library](https://github.com/digerata/NotifyOn/wiki). A good place to start is
13
+ [this article](https://github.com/BrilliantChemistry/NotifyOn/wiki/Getting-Started).
14
+
15
+ If you have questions, comments, ideas, or bug reports, please [create an
16
+ issue](https://github.com/BrilliantChemistry/NotifyOn/issues/new).
17
+
18
+ Contributors
19
+ ----------
20
+
21
+ This project was built and is maintained by [Mike
22
+ Wille](https://github.com/digerata) and [Sean C
23
+ Davis](https://github.com/seancdavis), and was made possible by [Brilliant
24
+ Chemistry](http://www.brilliantchemistry.com/).
25
+
26
+ License
27
+ ----------
28
+
29
+ See [MIT-LICENSE](https://github.com/BrilliantChemistry/NotifyOn/blob/master/MIT-LICENSE)
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'NotifyOn'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/notify_on .js
2
+ //= link_directory ../stylesheets/notify_on .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module NotifyOn
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module NotifyOn
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,9 @@
1
+ module NotifyOn::MailerHelper
2
+
3
+ def method_missing(method, *args, &block)
4
+ m = method.to_s
5
+ super unless m.ends_with?('_path') || m.ends_with?('_url')
6
+ main_app.send(m, *args)
7
+ end
8
+
9
+ end
@@ -0,0 +1,4 @@
1
+ module NotifyOn
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module NotifyOn
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,26 @@
1
+ module NotifyOn
2
+ class NotificationMailer < ApplicationMailer
3
+
4
+ helper 'notify_on/mailer'
5
+
6
+ def notify(notification_id, template = 'notify', files = [])
7
+ @notification = NotifyOn::Notification.find_by_id(notification_id)
8
+ @recipient = @notification.recipient
9
+ @sender = @notification.sender
10
+ @trigger = @notification.trigger
11
+ # Add attachments.
12
+ files.each { |attr| attachments[attr[:name]] = attr[:file] }
13
+ # Save a reference to the message if requested.
14
+ if @notification.should_save_email_id?
15
+ headers['message-id'] = (message_id = SecureRandom.uuid)
16
+ @trigger.update_columns(:message_id => message_id)
17
+ end
18
+ mail :to => @recipient.email,
19
+ :from => @notification.email_from,
20
+ :subject => @notification.email_subject,
21
+ :template_path => 'notifications',
22
+ :template_name => template
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,97 @@
1
+ module NotifyOn
2
+ module EmailSupport
3
+
4
+ def send_email!
5
+ return false unless can_send_email?
6
+ NotifyOn.configuration.mailer_class.constantize
7
+ .notify(id, email_template, email_attachments)
8
+ .send("deliver_#{email_delivery_timing}")
9
+ end
10
+
11
+ def email_template
12
+ return nil unless email_config?
13
+ email_config.template || 'notify'
14
+ end
15
+
16
+ def email_from
17
+ convert_string(email_from_raw)
18
+ end
19
+
20
+ def email_subject
21
+ return email_config.subject if email_config? && email_config.subject?
22
+ description
23
+ end
24
+
25
+ def should_save_email_id?
26
+ email_config? && email_config.save_id?
27
+ end
28
+
29
+ def email_attachments
30
+ attachments = []
31
+ return attachments unless email_config? && email_config.attachments?
32
+ email_config.attachments.each do |name, renderer|
33
+ next unless trigger.send(renderer.if) if renderer.try(:if?)
34
+ filename = begin
35
+ trigger.send(name)
36
+ rescue => e
37
+ name
38
+ end
39
+ begin
40
+ file = if renderer.is_a?(Symbol)
41
+ trigger.send(renderer)
42
+ else
43
+ trigger.send(renderer.file)
44
+ end
45
+ rescue => e
46
+ raise "Could not create file: #{filename}\n#{e}"
47
+ end
48
+ attachments << { :name => filename, :file => file }
49
+ end
50
+ attachments
51
+ end
52
+
53
+ private
54
+
55
+ def email_delivery_timing
56
+ return email_config.deliver if email_config? && email_config.deliver?
57
+ NotifyOn.configuration.deliver_mail.to_s
58
+ end
59
+
60
+ def email_config
61
+ return nil unless opts.email
62
+ opts.email.is_a?(Hashie::Mash) ? opts.email : Hashie::Mash.new
63
+ end
64
+
65
+ def email_enabled?
66
+ opts.email?
67
+ end
68
+
69
+ def email_disabled?
70
+ !email_enabled?
71
+ end
72
+
73
+ def email_config?
74
+ email_enabled? && email_config.respond_to?(:[])
75
+ end
76
+
77
+ def can_send_email?
78
+ return false if email_disabled?
79
+ return true unless email_config.unless?
80
+ begin
81
+ !trigger.send(email_config.unless)
82
+ rescue
83
+ !send(email_config.unless)
84
+ end
85
+ end
86
+
87
+ def use_default_email?
88
+ email_disabled? || !email_config.from?
89
+ end
90
+
91
+ def email_from_raw
92
+ return NotifyOn.configuration.default_email if use_default_email?
93
+ email_config.from
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,66 @@
1
+ module NotifyOn
2
+ module PusherSupport
3
+ extend ActiveSupport::Concern
4
+
5
+ def push!
6
+ return false unless can_push?
7
+
8
+ pusher_config
9
+
10
+ msg = "Pusher Event: #{pusher_event_name} | "
11
+ msg += "To Channel: #{pusher_channel_name}\n#{pusher_attrs}"
12
+ Rails.logger.debug(msg)
13
+
14
+ Pusher.trigger_async(pusher_channel_name, pusher_event_name, pusher_attrs)
15
+ end
16
+
17
+ def pusher_sender_active?
18
+ ids = Pusher.channel_users(pusher_channel_name)[:users]
19
+ .collect { |u| u["id"].to_i }
20
+ ids.include?(sender_id)
21
+ end
22
+
23
+ def pusher_recipient_active?
24
+ ids = Pusher.channel_users(pusher_channel_name)[:users]
25
+ .collect { |u| u["id"].to_i }
26
+ ids.include?(recipient_id)
27
+ end
28
+
29
+ def pusher_channel_name
30
+ return nil unless can_push?
31
+ channel = options[:pusher][:channel] if options[:pusher].respond_to?(:[])
32
+ channel = NotifyOn.configuration.default_pusher_channel if channel.blank?
33
+ @pusher_channel_name ||= convert_string(channel)
34
+ end
35
+
36
+ def pusher_event_name
37
+ return nil unless can_push?
38
+ event = options[:pusher][:event] if options[:pusher].respond_to?(:[])
39
+ event = NotifyOn.configuration.default_pusher_event if event.blank?
40
+ @pusher_event_name ||= convert_string(event)
41
+ end
42
+
43
+ def pusher_attrs
44
+ return nil unless can_push?
45
+ {
46
+ :notification => self.to_json,
47
+ :trigger => trigger.to_json,
48
+ :data => (options[:pusher][:data] if options[:pusher].respond_to?(:[]))
49
+ }
50
+ end
51
+
52
+ private
53
+
54
+ def pusher_config
55
+ Pusher.app_id = NotifyOn.configuration.pusher_app_id
56
+ Pusher.key = NotifyOn.configuration.pusher_key
57
+ Pusher.secret = NotifyOn.configuration.pusher_secret
58
+ end
59
+
60
+ def can_push?
61
+ options[:pusher].present? ||
62
+ (options[:pusher].nil? && NotifyOn.configuration.use_pusher_by_default)
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,39 @@
1
+ module NotifyOn
2
+ module StringInterpolation
3
+ extend ActiveSupport::Concern
4
+
5
+ private
6
+
7
+ def convert_string(input)
8
+ return nil if input.blank?
9
+ (output = input.to_s).scan(/{[\w\_\.\?]+}/).each do |match|
10
+ result = begin
11
+ match.gsub(/[^\w\_\.\?]/, '').split('.').inject(trigger, :send)
12
+ rescue NoMethodError => e
13
+ match.gsub(/[^\w\_\.\?]/, '').split('.').inject(self, :send)
14
+ end
15
+ output = output.gsub(/#{match}/, result.to_s)
16
+ end
17
+ output.gsub(/\{:env}/, Rails.env.downcase)
18
+ .gsub(/\{:recipient_id}/, recipient_id.to_s)
19
+ end
20
+
21
+ def convert_link(input)
22
+ return nil if input.blank?
23
+ input = input.split(/\(|\)|,/).map(&:strip)
24
+ if %w(_path _url).select { |r| input[0].end_with?(r) }.blank?
25
+ return convert_string(input[0])
26
+ end
27
+ args = []
28
+ input[1..-1].each do |arg|
29
+ args << if %w(self :self).include?(arg)
30
+ trigger
31
+ else
32
+ (arg[0] == ':') ? trigger.send(arg[1..-1]) : arg
33
+ end
34
+ end
35
+ Rails.application.routes.url_helpers.send(input[0], *args)
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module NotifyOn
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,86 @@
1
+ module NotifyOn
2
+ class Notification < ApplicationRecord
3
+
4
+ # ---------------------------------------- Plugins
5
+
6
+ include NotifyOn::StringInterpolation,
7
+ NotifyOn::PusherSupport,
8
+ NotifyOn::EmailSupport
9
+
10
+ # ---------------------------------------- Attributes
11
+
12
+ serialize :options, Hash
13
+
14
+ # ---------------------------------------- Associations
15
+
16
+ belongs_to :recipient, :polymorphic => true
17
+ belongs_to :sender, :polymorphic => true
18
+ belongs_to :trigger, :polymorphic => true
19
+
20
+ def recipient_type=(sType)
21
+ super(sType.to_s.classify.constantize.base_class.to_s)
22
+ end
23
+
24
+ def sender_type=(sType)
25
+ super(sType.to_s.classify.constantize.base_class.to_s)
26
+ end
27
+
28
+ def trigger_type=(sType)
29
+ super(sType.to_s.classify.constantize.base_class.to_s)
30
+ end
31
+
32
+ # ---------------------------------------- Scopes
33
+
34
+ scope :preloaded, -> { includes(:recipient, :sender, :trigger) }
35
+ scope :unread, -> { where(:unread => true) }
36
+ scope :recent, -> { order(:updated_at => :desc) }
37
+
38
+ scope :from_with_type, ->(from, type) {
39
+ where(:sender => from, :trigger_type => type)
40
+ }
41
+
42
+ # ---------------------------------------- Callbacks
43
+
44
+ after_save :convert_attrs
45
+
46
+ # ---------------------------------------- Class Methods
47
+
48
+ def self.mark_read_for(recipient, trigger)
49
+ where(:recipient => recipient, :trigger => trigger)
50
+ .update_all(:unread => false)
51
+ end
52
+
53
+ # ---------------------------------------- Instance Methods
54
+
55
+ def read!
56
+ update_columns(:unread => false)
57
+ end
58
+
59
+ def read?
60
+ !unread?
61
+ end
62
+
63
+ def link
64
+ link_cached || convert_link(link_raw)
65
+ end
66
+
67
+ def description
68
+ description_cached || convert_string(description_raw)
69
+ end
70
+
71
+ def method_missing(method_name, *args, &blk)
72
+ return Hashie::Mash.new(options) if method_name.to_s == 'opts'
73
+ super
74
+ end
75
+
76
+ # ---------------------------------------- Private Methods
77
+
78
+ private
79
+
80
+ def convert_attrs
81
+ update_columns(:description_cached => convert_string(description_raw),
82
+ :link_cached => convert_link(link_raw))
83
+ end
84
+
85
+ end
86
+ end