wupee 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +30 -0
  4. data/app/controllers/wupee/api/notifications_controller.rb +33 -0
  5. data/app/controllers/wupee/application_controller.rb +4 -0
  6. data/app/mailers/wupee/notifications_mailer.rb +30 -0
  7. data/app/models/concerns/wupee/attached_object.rb +9 -0
  8. data/app/models/concerns/wupee/receiver.rb +16 -0
  9. data/app/models/wupee/notification.rb +16 -0
  10. data/app/models/wupee/notification_type.rb +18 -0
  11. data/app/models/wupee/notification_type_configuration.rb +17 -0
  12. data/app/models/wupee/notifier.rb +73 -0
  13. data/app/views/wupee/api/notifications/_notification.json.jbuilder +5 -0
  14. data/app/views/wupee/api/notifications/index.json.jbuilder +1 -0
  15. data/app/views/wupee/api/notifications/show.json.jbuilder +1 -0
  16. data/db/migrate/20151029113101_create_wupee_notification_type_configurations.rb +13 -0
  17. data/db/migrate/20151029113107_create_wupee_notifications.rb +14 -0
  18. data/db/migrate/20151029113122_create_wupee_notification_types.rb +10 -0
  19. data/lib/generators/wupee/install/USAGE +15 -0
  20. data/lib/generators/wupee/install/install_generator.rb +24 -0
  21. data/lib/generators/wupee/install/templates/notifications_mailer.rb +4 -0
  22. data/lib/generators/wupee/install/templates/wupee.rb +6 -0
  23. data/lib/generators/wupee/notification_type/USAGE +11 -0
  24. data/lib/generators/wupee/notification_type/notification_type_generator.rb +26 -0
  25. data/lib/tasks/wupee_tasks.rake +4 -0
  26. data/lib/wupee.rb +11 -0
  27. data/lib/wupee/engine.rb +12 -0
  28. data/lib/wupee/version.rb +3 -0
  29. data/spec/controllers/notifications_controller_spec.rb +65 -0
  30. data/spec/dummy/README.rdoc +28 -0
  31. data/spec/dummy/Rakefile +6 -0
  32. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  33. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  34. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  35. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  36. data/spec/dummy/app/mailers/notifications_mailer.rb +4 -0
  37. data/spec/dummy/app/models/message.rb +3 -0
  38. data/spec/dummy/app/models/user.rb +3 -0
  39. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/spec/dummy/app/views/notifications/_admin_user_created.html.erb +0 -0
  41. data/spec/dummy/app/views/notifications/_notify_new_message.html.erb +0 -0
  42. data/spec/dummy/app/views/notifications_mailer/notify_new_message.html.erb +0 -0
  43. data/spec/dummy/app/views/wupee/api/notifications/_notification.json.jbuilder +5 -0
  44. data/spec/dummy/app/views/wupee/api/notifications/_notify_new_message.json.jbuilder +3 -0
  45. data/spec/dummy/app/views/wupee/api/notifications/index.json.jbuilder +1 -0
  46. data/spec/dummy/app/views/wupee/api/notifications/show.json.jbuilder +1 -0
  47. data/spec/dummy/app/views/wupee/notifications/_notify_new_message.html.erb +0 -0
  48. data/spec/dummy/bin/bundle +3 -0
  49. data/spec/dummy/bin/rails +4 -0
  50. data/spec/dummy/bin/rake +4 -0
  51. data/spec/dummy/bin/setup +29 -0
  52. data/spec/dummy/config.ru +4 -0
  53. data/spec/dummy/config/application.rb +32 -0
  54. data/spec/dummy/config/boot.rb +5 -0
  55. data/spec/dummy/config/database.yml +25 -0
  56. data/spec/dummy/config/environment.rb +5 -0
  57. data/spec/dummy/config/environments/development.rb +41 -0
  58. data/spec/dummy/config/environments/production.rb +79 -0
  59. data/spec/dummy/config/environments/test.rb +42 -0
  60. data/spec/dummy/config/initializers/assets.rb +11 -0
  61. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  63. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  64. data/spec/dummy/config/initializers/inflections.rb +16 -0
  65. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  66. data/spec/dummy/config/initializers/session_store.rb +3 -0
  67. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/spec/dummy/config/initializers/wupee.rb +6 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +3 -0
  71. data/spec/dummy/config/secrets.yml +22 -0
  72. data/spec/dummy/db/development.sqlite3 +0 -0
  73. data/spec/dummy/db/migrate/20150213150625_create_users.rb +10 -0
  74. data/spec/dummy/db/migrate/20150213152846_create_messages.rb +9 -0
  75. data/spec/dummy/db/schema.rb +61 -0
  76. data/spec/dummy/db/test.sqlite3 +0 -0
  77. data/spec/dummy/log/development.log +1750 -0
  78. data/spec/dummy/log/test.log +76399 -0
  79. data/spec/dummy/public/404.html +67 -0
  80. data/spec/dummy/public/422.html +67 -0
  81. data/spec/dummy/public/500.html +66 -0
  82. data/spec/dummy/public/favicon.ico +0 -0
  83. data/spec/factories/message.rb +5 -0
  84. data/spec/factories/notification.rb +7 -0
  85. data/spec/factories/notification_type.rb +5 -0
  86. data/spec/factories/notification_type_configuration.rb +6 -0
  87. data/spec/factories/user.rb +9 -0
  88. data/spec/mailers/notifications_mailer_spec.rb +33 -0
  89. data/spec/models/concerns/attached_object_spec.rb +11 -0
  90. data/spec/models/concerns/receiver_spec.rb +37 -0
  91. data/spec/models/message_spec.rb +5 -0
  92. data/spec/models/notification_spec.rb +54 -0
  93. data/spec/models/notification_type_configuration_spec.rb +43 -0
  94. data/spec/models/notifier_spec.rb +42 -0
  95. data/spec/models/user_spec.rb +5 -0
  96. data/spec/rails_helper.rb +52 -0
  97. data/spec/spec_helper.rb +87 -0
  98. data/spec/support/factory_girl.rb +3 -0
  99. data/spec/wupee_spec.rb +15 -0
  100. metadata +291 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: af10fc3ae97b36dc7ac42eeb137b1efa44a6f7e3
4
+ data.tar.gz: e1b1f83375a63a78f51eb8da2ca172a3c4740e59
5
+ SHA512:
6
+ metadata.gz: b5fc93a9d6c9441262c26f4780b0eea850dea8a101b6743fac2077fcd7960d48f06338a5fcaf8cfe1c8e3e993ddb9b3ade0c52d45e388c03e6149ed1ab32d526
7
+ data.tar.gz: a3c6b5d420d0b52ccaf0b2a554868d1bccdff120ab5f25282ed2bd0297b53e431fd5a90bbe37fdd454cbe47729d9a9ddc6aa8f3c3d23dcd65e2ede94fec49bc4
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Peng DU
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,30 @@
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 = 'Wupee'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
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
+ Bundler::GemHelper.install_tasks
21
+
22
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
23
+
24
+ require 'rspec/core'
25
+ require 'rspec/core/rake_task'
26
+
27
+ desc "Run all specs in spec directory (excluding plugin specs)"
28
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
29
+
30
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ module Wupee
2
+ class Api::NotificationsController < ApplicationController
3
+ before_action :set_notification, only: [:show, :update]
4
+
5
+ def index
6
+ if params[:is_read]
7
+ @notifications = current_user.notifications.where(is_read: params[:is_read] == "true")
8
+ else
9
+ @notifications = current_user.notifications
10
+ end
11
+ end
12
+
13
+ def show
14
+ end
15
+
16
+ def update
17
+ @notification.mark_as_read
18
+ render :show
19
+ end
20
+
21
+ def update_all
22
+ current_user.notifications.where(is_read: false).find_each do |n|
23
+ n.mark_as_read
24
+ end
25
+ head :no_content
26
+ end
27
+
28
+ private
29
+ def set_notification
30
+ @notification = current_user.notifications.find(params[:id])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module Wupee
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,30 @@
1
+ module Wupee
2
+ class NotificationsMailer < ActionMailer::Base
3
+ after_action :mark_notification_as_sent
4
+
5
+ def send_mail_for(notification, subject_interpolations = {})
6
+ @notification = notification
7
+ @receiver = notification.receiver
8
+ @attached_object = notification.attached_object
9
+ @subject_interpolations = subject_interpolations
10
+
11
+ if !respond_to?(notification.notification_type.name)
12
+ class_eval %Q{
13
+ def #{notification.notification_type.name}
14
+ mail to: @receiver.email,
15
+ subject: t('wupee.email_subjects.#{notification.notification_type.name}', @subject_interpolations),
16
+ template_name: '#{notification.notification_type.name}',
17
+ content_type: 'text/html'
18
+ end
19
+ }
20
+ end
21
+
22
+ send(notification.notification_type.name)
23
+ end
24
+
25
+ private
26
+ def mark_notification_as_sent
27
+ @notification.mark_as_sent unless @notification.is_sent
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ module Wupee
2
+ module AttachedObject
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :notifications_as_attached_object, as: :attached_object, dependent: :destroy, class_name: "Wupee::Notification"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Wupee
2
+ module Receiver
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :notifications, as: :receiver, dependent: :destroy, class_name: "Wupee::Notification"
7
+ has_many :notification_type_configurations, as: :receiver, dependent: :destroy, class_name: "Wupee::NotificationTypeConfiguration"
8
+
9
+ after_create do
10
+ Wupee::NotificationType.pluck(:id).each do |notification_type_id|
11
+ Wupee::NotificationTypeConfiguration.create!(notification_type_id: notification_type_id, receiver: self)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ class Wupee::Notification < ActiveRecord::Base
2
+ belongs_to :receiver, polymorphic: true
3
+ belongs_to :attached_object, polymorphic: true
4
+ belongs_to :notification_type, class_name: "Wupee::NotificationType"
5
+
6
+ validates_presence_of :receiver,
7
+ :notification_type
8
+
9
+ def mark_as_read
10
+ update_columns(is_read: true)
11
+ end
12
+
13
+ def mark_as_sent
14
+ update_columns(is_sent: true)
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ class Wupee::NotificationType < ActiveRecord::Base
2
+ validates :name, presence: true
3
+ validates :name, uniqueness: true
4
+
5
+ has_many :notification_type_configurations, foreign_key: :notification_type_id, dependent: :destroy
6
+
7
+ def self.create_configurations_for(*receivers)
8
+ class_eval do
9
+ receivers.each do |receiver|
10
+ after_create do
11
+ receiver.to_s.constantize.pluck(:id).each do |receiver_id|
12
+ Wupee::NotificationTypeConfiguration.create!(notification_type: self, receiver_type: receiver, receiver_id: receiver_id)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ class Wupee::NotificationTypeConfiguration < ActiveRecord::Base
2
+ belongs_to :receiver, polymorphic: true
3
+ belongs_to :notification_type, class_name: "Wupee::NotificationType"
4
+
5
+ validates :notification_type, uniqueness: { scope: [:receiver] }
6
+ validates :receiver, :notification_type, presence: true
7
+
8
+ enum value: { both: 0, nothing: 1, email: 2, notification: 3 }
9
+
10
+ def wants_email?
11
+ ['both', 'email'].include?(value)
12
+ end
13
+
14
+ def wants_notification?
15
+ ['both', 'notification'].include?(value)
16
+ end
17
+ end
@@ -0,0 +1,73 @@
1
+ module Wupee
2
+ class Notifier
3
+ attr_reader :deliver_when, :attached_object, :receiver_s, :notification_type, :subject_vars
4
+
5
+ def initialize(opts = {})
6
+ @attached_object = opts[:attached_object]
7
+ @receiver_s = opts[:receiver] || opts[:receivers] || []
8
+ @subject_vars = opts[:subject_vars] || {}
9
+ @deliver_when = opts[:deliver]
10
+ notif_type(opts[:notif_type]) if opts[:notif_type]
11
+ end
12
+
13
+ def notif_type(notif_type)
14
+ if notif_type.is_a?(Wupee::NotificationType)
15
+ @notification_type = notif_type
16
+ else
17
+ @notification_type = Wupee::NotificationType.find_by!(name: notif_type)
18
+ end
19
+ end
20
+
21
+ def attached_object(attached_object)
22
+ @attached_object = attached_object
23
+ end
24
+
25
+ def receiver(receiver)
26
+ @receiver_s = receiver
27
+ end
28
+
29
+ def receivers(receivers)
30
+ @receiver_s = receivers
31
+ end
32
+
33
+ def deliver(deliver_method)
34
+ @deliver_when = deliver_method
35
+ end
36
+
37
+ def subject_vars(subject_vars = {})
38
+ @subject_vars = subject_vars
39
+ end
40
+
41
+ def execute
42
+ notif_type_configs = Wupee::NotificationTypeConfiguration.includes(:receiver).where(receiver: @receiver_s, notification_type: @notification_type)
43
+
44
+ notif_type_configs.each do |notif_type_config|
45
+ notification = Wupee::Notification.new(receiver: notif_type_config.receiver, notification_type: @notification_type, attached_object: @attached_object)
46
+ notification.is_read = true unless notif_type_config.wants_notification?
47
+ notification.save!
48
+
49
+ subject_interpolations = interpolate_subject_vars(notification)
50
+ send_email(notification, subject_interpolations) if notif_type_config.wants_notification?
51
+ end
52
+ end
53
+
54
+ private
55
+ def send_email(notification, subject_interpolations)
56
+ deliver_method = "deliver_#{@deliver_when || Wupee.deliver_when}"
57
+ Wupee.mailer.send_mail_for(notification, subject_interpolations).send(deliver_method)
58
+ end
59
+
60
+ def interpolate_subject_vars(notification)
61
+ subject_vars_interpolated = {}
62
+ @subject_vars.each do |key, value|
63
+ subject_vars_interpolated[key] = if value.kind_of?(Proc)
64
+ notification.instance_eval(&value)
65
+ else
66
+ value.to_s
67
+ end
68
+ end
69
+
70
+ subject_vars_interpolated
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ json.extract! notification, :id, :notification_type_id, :created_at, :is_read
2
+ json.attached_object notification.attached_object
3
+ json.message do
4
+ json.partial! "wupee/api/notifications/#{notification.notification_type.name}", notification: notification
5
+ end
@@ -0,0 +1 @@
1
+ json.array! @notifications, partial: 'wupee/api/notifications/notification', as: :notification
@@ -0,0 +1 @@
1
+ json.partial! 'wupee/api/notifications/notification', notification: @notification
@@ -0,0 +1,13 @@
1
+ class CreateWupeeNotificationTypeConfigurations < ActiveRecord::Migration
2
+ def change
3
+ create_table :wupee_notification_type_configurations do |t|
4
+ t.belongs_to :notification_type, foreign_key: true
5
+ t.belongs_to :receiver, polymorphic: true
6
+ t.integer :value, default: 0
7
+ t.timestamps null: false
8
+ end
9
+
10
+ add_index :wupee_notification_type_configurations, [:notification_type_id], name: "idx_wupee_notif_type_config_on_notification_type_id"
11
+ add_index :wupee_notification_type_configurations, [:receiver_type, :receiver_id], name: "idx_wupee_notif_typ_config_on_receiver_type_and_receiver_id"
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class CreateWupeeNotifications < ActiveRecord::Migration
2
+ def change
3
+ create_table :wupee_notifications do |t|
4
+ t.references :receiver, polymorphic: true
5
+ t.references :attached_object, polymorphic: true
6
+ t.belongs_to :notification_type, foreign_key: true
7
+ t.integer :notification_type_id
8
+ t.boolean :is_read, default: false
9
+ t.boolean :is_sent, default: false
10
+
11
+ t.timestamps null: false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ class CreateWupeeNotificationTypes < ActiveRecord::Migration
2
+ def change
3
+ create_table :wupee_notification_types do |t|
4
+ t.string :name
5
+ t.timestamps null: false
6
+ end
7
+
8
+ add_index :wupee_notification_types, :name, unique: true
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Generates model notification and notification_type, a mailer, an initializer
3
+
4
+ Example:
5
+ rails generate wupee:install
6
+
7
+ This will create:
8
+ db/migrate/created_notifcation.rb
9
+ app/models/notification.rb
10
+ app/models/notification_type.rb
11
+ app/mailers/notifications_mailer.rb
12
+ config/initializers/wupee.rb
13
+
14
+ this will update:
15
+ config/locales/{local}.yml
@@ -0,0 +1,24 @@
1
+ class Wupee::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_notifications_mailer
5
+ template "notifications_mailer.rb", "app/mailers/notifications_mailer.rb"
6
+ end
7
+
8
+ def add_wupee_routes
9
+ route 'mount Wupee::Engine, at: "/wupee"'
10
+ end
11
+
12
+ def copy_initializer
13
+ template "wupee.rb", "config/initializers/wupee.rb"
14
+ end
15
+
16
+ def add_subject_locale
17
+ append_file "config/locales/#{I18n.locale.to_s}.yml" do
18
+ <<-CODE
19
+ wupee:
20
+ email_subjects:
21
+ CODE
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ class NotificationsMailer < Wupee::NotificationsMailer
2
+ default from: 'contact@sleede.com'
3
+ layout false
4
+ end
@@ -0,0 +1,6 @@
1
+ Wupee.mailer = NotificationsMailer
2
+ Wupee.deliver_when = :now
3
+
4
+ Wupee::NotificationType.class_eval do
5
+ create_configurations_for 'User'
6
+ end
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Add a notification by name
3
+
4
+ Example:
5
+ rails generate notification name
6
+
7
+ This will create or modify:
8
+ insert app/models/notification_type.rb
9
+ create app/views/notifications_mailer/name.html.erb
10
+ insert app/mailers/notifications_mailer.rb
11
+ insert config/locales/en.yml
@@ -0,0 +1,26 @@
1
+ class Wupee::NotificationTypeGenerator < Rails::Generators::NamedBase
2
+ def add_notification_subject
3
+ inject_into_file "config/locales/#{I18n.locale.to_s}.yml", after: /email_subjects:\n/ do
4
+ <<-CODE
5
+ #{file_name}: "#{file_name}"
6
+ CODE
7
+ end
8
+ end
9
+
10
+ def create_notification_json_template_file
11
+ create_file "app/views/wupee/api/notifications/_#{file_name}.json.jbuilder", <<-FILE
12
+ json.subject "subject"
13
+ json.body "body"
14
+ json.url "url"
15
+ FILE
16
+ end
17
+
18
+ def create_notification_html_template_file
19
+ create_file "app/views/wupee/notifications/_#{file_name}.html.erb", <<-FILE
20
+ FILE
21
+ end
22
+
23
+ def create_notification_type_object
24
+ Wupee::NotificationType.create!(name: file_name)
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :wupee do
3
+ # # Task goes here
4
+ # end