activity_notification 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +56 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +28 -0
  6. data/Gemfile +8 -0
  7. data/Gemfile.lock +174 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +437 -0
  10. data/Rakefile +19 -0
  11. data/activity_notification.gemspec +33 -0
  12. data/app/controllers/activity_notification/notifications_controller.rb +119 -0
  13. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +29 -0
  14. data/app/mailers/activity_notification/mailer.rb +13 -0
  15. data/app/views/activity_notification/mailer/default/default.html.erb +7 -0
  16. data/app/views/activity_notification/notifications/default/_default.html.erb +36 -0
  17. data/app/views/activity_notification/notifications/default/_index.html.erb +9 -0
  18. data/app/views/activity_notification/notifications/default/destroy.js.erb +2 -0
  19. data/app/views/activity_notification/notifications/default/index.html.erb +17 -0
  20. data/app/views/activity_notification/notifications/default/open.js.erb +2 -0
  21. data/app/views/activity_notification/notifications/default/open_all.js.erb +2 -0
  22. data/app/views/activity_notification/notifications/default/show.html.erb +2 -0
  23. data/config/locales/en.yml +8 -0
  24. data/lib/activity_notification.rb +52 -0
  25. data/lib/activity_notification/apis/notification_api.rb +147 -0
  26. data/lib/activity_notification/common.rb +86 -0
  27. data/lib/activity_notification/config.rb +23 -0
  28. data/lib/activity_notification/controllers/store_controller.rb +30 -0
  29. data/lib/activity_notification/helpers/polymorphic_helpers.rb +32 -0
  30. data/lib/activity_notification/helpers/view_helpers.rb +108 -0
  31. data/lib/activity_notification/mailers/helpers.rb +97 -0
  32. data/lib/activity_notification/models/notifiable.rb +136 -0
  33. data/lib/activity_notification/models/notification.rb +50 -0
  34. data/lib/activity_notification/models/notifier.rb +11 -0
  35. data/lib/activity_notification/models/target.rb +104 -0
  36. data/lib/activity_notification/rails.rb +6 -0
  37. data/lib/activity_notification/rails/routes.rb +105 -0
  38. data/lib/activity_notification/renderable.rb +142 -0
  39. data/lib/activity_notification/roles/acts_as_notifiable.rb +37 -0
  40. data/lib/activity_notification/roles/acts_as_target.rb +30 -0
  41. data/lib/activity_notification/version.rb +3 -0
  42. data/lib/generators/activity_notification/controllers_generator.rb +44 -0
  43. data/lib/generators/activity_notification/install_generator.rb +45 -0
  44. data/lib/generators/activity_notification/migration/migration_generator.rb +17 -0
  45. data/lib/generators/activity_notification/notification/notification_generator.rb +17 -0
  46. data/lib/generators/activity_notification/views_generator.rb +44 -0
  47. data/lib/generators/templates/README +53 -0
  48. data/lib/generators/templates/active_record/migration.rb +18 -0
  49. data/lib/generators/templates/activity_notification.rb +18 -0
  50. data/lib/generators/templates/controllers/README +13 -0
  51. data/lib/generators/templates/controllers/notifications_controller.rb +66 -0
  52. data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +74 -0
  53. data/lib/generators/templates/notification/notification.rb +3 -0
  54. data/lib/tasks/activity_notification_tasks.rake +4 -0
  55. data/spec/concerns/notification_api_spec.rb +531 -0
  56. data/spec/factories/articles.rb +5 -0
  57. data/spec/factories/comments.rb +6 -0
  58. data/spec/factories/notifications.rb +7 -0
  59. data/spec/factories/users.rb +5 -0
  60. data/spec/models/notification_spec.rb +259 -0
  61. data/spec/rails_app/Rakefile +6 -0
  62. data/spec/rails_app/app/controllers/application_controller.rb +5 -0
  63. data/spec/rails_app/app/controllers/concerns/.keep +0 -0
  64. data/spec/rails_app/app/helpers/application_helper.rb +2 -0
  65. data/spec/rails_app/app/mailers/.keep +0 -0
  66. data/spec/rails_app/app/models/.keep +0 -0
  67. data/spec/rails_app/app/models/article.rb +12 -0
  68. data/spec/rails_app/app/models/comment.rb +18 -0
  69. data/spec/rails_app/app/models/concerns/.keep +0 -0
  70. data/spec/rails_app/app/models/user.rb +8 -0
  71. data/spec/rails_app/app/views/layouts/application.html.erb +14 -0
  72. data/spec/rails_app/bin/bundle +3 -0
  73. data/spec/rails_app/bin/rails +4 -0
  74. data/spec/rails_app/bin/rake +4 -0
  75. data/spec/rails_app/bin/setup +29 -0
  76. data/spec/rails_app/config.ru +4 -0
  77. data/spec/rails_app/config/application.rb +20 -0
  78. data/spec/rails_app/config/boot.rb +5 -0
  79. data/spec/rails_app/config/database.yml +25 -0
  80. data/spec/rails_app/config/environment.rb +12 -0
  81. data/spec/rails_app/config/environments/development.rb +44 -0
  82. data/spec/rails_app/config/environments/production.rb +79 -0
  83. data/spec/rails_app/config/environments/test.rb +45 -0
  84. data/spec/rails_app/config/initializers/activity_notification.rb +18 -0
  85. data/spec/rails_app/config/initializers/assets.rb +11 -0
  86. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  87. data/spec/rails_app/config/initializers/cookies_serializer.rb +3 -0
  88. data/spec/rails_app/config/initializers/devise.rb +274 -0
  89. data/spec/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
  90. data/spec/rails_app/config/initializers/inflections.rb +16 -0
  91. data/spec/rails_app/config/initializers/mime_types.rb +4 -0
  92. data/spec/rails_app/config/initializers/session_store.rb +3 -0
  93. data/spec/rails_app/config/initializers/wrap_parameters.rb +14 -0
  94. data/spec/rails_app/config/routes.rb +5 -0
  95. data/spec/rails_app/config/secrets.yml +22 -0
  96. data/spec/rails_app/db/migrate/20160715050420_create_notifications.rb +18 -0
  97. data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +36 -0
  98. data/spec/rails_app/db/schema.rb +73 -0
  99. data/spec/rails_app/public/404.html +67 -0
  100. data/spec/rails_app/public/422.html +67 -0
  101. data/spec/rails_app/public/500.html +66 -0
  102. data/spec/rails_app/public/favicon.ico +0 -0
  103. data/spec/spec_helper.rb +34 -0
  104. metadata +309 -0
@@ -0,0 +1,30 @@
1
+ module ActivityNotification
2
+ module ActsAsTarget
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def acts_as_target(opts = {})
7
+ options = opts.clone
8
+ if options[:skip_email] == true
9
+ self.send("_notification_email_allowed=".to_sym, false)
10
+ end
11
+
12
+ assign_globals options
13
+ nil
14
+ end
15
+ alias_method :acts_as_notification_target, :acts_as_target
16
+
17
+ def available_options
18
+ [:skip_email, :email, :email_allowed].freeze
19
+ end
20
+
21
+ def assign_globals(options)
22
+ [:email, :email_allowed].each do |key|
23
+ if options[key]
24
+ self.send("_notification_#{key}=".to_sym, options.delete(key))
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module ActivityNotification
2
+ VERSION = "0.0.8"
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ActivityNotification
4
+ module Generators
5
+ class ControllersGenerator < Rails::Generators::Base
6
+ CONTROLLERS = %w(notifications notifications_with_devise).freeze
7
+
8
+ desc <<-DESC.strip_heredoc
9
+ Create inherited ActivityNotification controllers in your app/controllers folder.
10
+
11
+ Use -c to specify which controller you want to overwrite.
12
+ If you do no specify a controller, all controllers will be created.
13
+ For example:
14
+
15
+ rails generate activity_notification:controllers users -c=notifications
16
+
17
+ This will create a controller class at app/controllers/users/notifications_controller.rb like this:
18
+
19
+ class Users::NotificationsController < ActivityNotification::NotificationsController
20
+ content...
21
+ end
22
+ DESC
23
+
24
+ source_root File.expand_path("../../templates/controllers", __FILE__)
25
+ argument :target, required: true,
26
+ desc: "The target to create controllers in, e.g. users, admins"
27
+ class_option :controllers, aliases: "-c", type: :array,
28
+ desc: "Select specific controllers to generate (#{CONTROLLERS.join(', ')})"
29
+
30
+ def create_controllers
31
+ @target_prefix = target.blank? ? '' : (target.camelize + '::')
32
+ controllers = options[:controllers] || CONTROLLERS
33
+ controllers.each do |name|
34
+ template "#{name}_controller.rb",
35
+ "app/controllers/#{target}/#{name}_controller.rb"
36
+ end
37
+ end
38
+
39
+ def show_readme
40
+ readme "README" if behavior == :invoke
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ require 'rails/generators/base'
2
+ require 'securerandom'
3
+
4
+ module ActivityNotification
5
+ module Generators
6
+
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+ ``
10
+ desc "Creates a ActivityNotification initializer and copy locale files to your application."
11
+ class_option :orm
12
+
13
+ def copy_initializer
14
+
15
+ #TODO suport other orm e.g. mongoid
16
+ unless options[:orm] == :active_record
17
+ raise MissingORMError, <<-ERROR.strip_heredoc
18
+ Currently ActivityNotification is only supported with Active Record ORM.
19
+
20
+ Be sure to have an Active Record ORM loaded in your
21
+ app or configure your own at `config/application.rb`.
22
+
23
+ config.generators do |g|
24
+ g.orm :active_record
25
+ end
26
+ ERROR
27
+ end
28
+
29
+ template "activity_notification.rb", "config/initializers/activity_notification.rb"
30
+ end
31
+
32
+ def copy_locale
33
+ copy_file "../../../config/locales/en.yml", "config/locales/activity_notification.en.yml"
34
+ end
35
+
36
+ def show_readme
37
+ readme "README" if behavior == :invoke
38
+ end
39
+
40
+ def rails_4?
41
+ Rails::VERSION::MAJOR == 4
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActivityNotification
4
+ module Generators
5
+ # Migration generator that creates migration file from template
6
+ class MigrationGenerator < ActiveRecord::Generators::Base
7
+ source_root File.expand_path("../../../templates/active_record", __FILE__)
8
+
9
+ argument :name, type: :string, default: 'create_notifications'
10
+
11
+ # Create migration in project's folder
12
+ def generate_files
13
+ migration_template 'migration.rb', "db/migrate/#{name}.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActivityNotification
4
+ module Generators
5
+ # Notification generator that creates notification model file from template
6
+ class NotificationGenerator < ActiveRecord::Generators::Base
7
+ source_root File.expand_path("../../../templates/notification", __FILE__)
8
+
9
+ argument :name, type: :string, default: 'notification'
10
+
11
+ # Create model in project's folder
12
+ def generate_files
13
+ copy_file 'notification.rb', "app/models/#{name}.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ActivityNotification
4
+ module Generators
5
+ # Include this module in your generator to generate ActivityNotification views.
6
+ # `copy_views` is the main method and by default copies all views
7
+ # with forms.
8
+ class ViewsGenerator < Rails::Generators::Base
9
+ source_root File.expand_path("../../../../app/views/activity_notification", __FILE__)
10
+ desc "Copies default ActivityNotification views to your application."
11
+
12
+ argument :target, required: false, default: nil,
13
+ desc: "The target to copy views to"
14
+ class_option :views, aliases: "-v", type: :array, desc: "Select specific view directories to generate (notifications, mailer)"
15
+ public_task :copy_views
16
+
17
+ def copy_views
18
+ if options[:views]
19
+ options[:views].each do |directory|
20
+ view_directory directory.to_sym
21
+ end
22
+ else
23
+ view_directory :notifications
24
+ view_directory :mailer
25
+ end
26
+ end
27
+
28
+ protected
29
+
30
+ def view_directory(name, _target_path = nil)
31
+ directory "#{name.to_s}/default", _target_path || "#{target_path}/#{name}/#{plural_target || :default}"
32
+ end
33
+
34
+ def target_path
35
+ @target_path ||= "app/views/activity_notification"
36
+ end
37
+
38
+ def plural_target
39
+ @plural_target ||= target.presence && target.to_s.underscore.pluralize
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ ===============================================================================
2
+
3
+ Some setup you must do manually if you haven't yet:
4
+
5
+ 1. Ensure you have defined default url options in your environments files. Here
6
+ is an example of default_url_options appropriate for a development environment
7
+ in config/environments/development.rb:
8
+
9
+ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
10
+
11
+ In production, :host should be set to the actual host of your application.
12
+
13
+ 2. Setup your target model (e.g. app/models/user.rb)
14
+
15
+ - Add including statement and acts_as_target definition to your target model
16
+
17
+ include ActivityNotification::Target
18
+ acts_as_target email: :email, email_allowed: :confirmed_at
19
+
20
+ - Add notification routing to config/routes.rb
21
+
22
+ (simply) notify_to :users
23
+ (with devise) notify_to :users, with_devise: :users
24
+
25
+ - You can override several methods in your target model
26
+
27
+ e.g. notification_index, notification_email_allowed?
28
+
29
+ 3. Setup your notifiable model (e.g. app/models/comment.rb)
30
+
31
+ - Add including statement and acts_as_notifiable definition to your notifiable model
32
+
33
+ include ActivityNotification::Notifiable
34
+ acts_as_notifiable :users,
35
+ targets: :custom_notification_users,
36
+ group: :article,
37
+ notifier: :user,
38
+ email_allowed: :custom_notification_email_to_users_allowed?,
39
+ notifiable_path: :custom_notifiable_path
40
+
41
+ - You can override several methods in your notifiable model
42
+
43
+ e.g. notifiable_path, notification_email_allowed?
44
+
45
+ 4. You can copy ActivityNotification views (for customization) to your app by running:
46
+
47
+ rails g activity_notification:views
48
+
49
+ 5. You can customize locale file which is generated as following file:
50
+
51
+ config/locals/activity_notification.en.yml
52
+
53
+ ===============================================================================
@@ -0,0 +1,18 @@
1
+ # Migration responsible for creating a table with notifications
2
+ class CreateNotifications < ActiveRecord::Migration
3
+ # Create table
4
+ def change
5
+ create_table :notifications do |t|
6
+ t.belongs_to :target, polymorphic: true, index: true, null: false
7
+ t.belongs_to :notifiable, polymorphic: true, index: true, null: false
8
+ t.string :key , null: false
9
+ t.belongs_to :group, polymorphic: true, index: true
10
+ t.integer :group_owner_id , index: true
11
+ t.belongs_to :notifier, polymorphic: true, index: true
12
+ t.text :parameters
13
+ t.datetime :opened_at
14
+
15
+ t.timestamps
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ ActivityNotification.configure do |config|
2
+
3
+ # Table name to store notification data
4
+ config.table_name = "notifications"
5
+
6
+ # ==> Mailer Configuration
7
+ # Configure the e-mail address which will be shown in ActivityNotification::Mailer,
8
+ # note that it will be overwritten if you use your own mailer class with default "from" parameter.
9
+ config.mailer_sender = 'please-change-me-at-config-initializers-activity_notification@example.com'
10
+
11
+ # Configure the class responsible to send e-mails.
12
+ # config.mailer = "ActivityNotification::Mailer"
13
+
14
+ # Configure the parent class responsible to send e-mails.
15
+ # config.parent_mailer = 'ActionMailer::Base'
16
+
17
+ config.opened_limit = 10
18
+ end
@@ -0,0 +1,13 @@
1
+ ===============================================================================
2
+
3
+ Some setup you must do manually if you haven't yet:
4
+
5
+ Ensure you have overridden routes for generated controllers in your routes.rb.
6
+ For example:
7
+
8
+ Rails.application.routes.draw do
9
+ notify_to :users, controllers: 'users/notifications'
10
+ notify_to :admins, with_devise: :users, controllers: 'admins/notifications_with_devise'
11
+ end
12
+
13
+ ===============================================================================
@@ -0,0 +1,66 @@
1
+ class <%= @target_prefix %>NotificationsController < ActivityNotification::NotificationsController
2
+ # GET /:target_type/:target_id/notifcations
3
+ # def index
4
+ # super
5
+ # end
6
+
7
+ # POST /:target_type/:target_id/notifcations/open_all
8
+ # def open_all
9
+ # super
10
+ # end
11
+
12
+ # GET /:target_type/:target_id/notifcations/:id
13
+ # def show
14
+ # super
15
+ # end
16
+
17
+ # DELETE /:target_type/:target_id/notifcations/:id
18
+ # def destroy
19
+ # super
20
+ # end
21
+
22
+ # POST /:target_type/:target_id/notifcations/:id/open
23
+ # def open
24
+ # super
25
+ # end
26
+
27
+ # GET /:target_type/:target_id/notifcations/:id/move
28
+ # def move
29
+ # super
30
+ # end
31
+
32
+ # No action routing
33
+ # This method is called from target_view_path method
34
+ # This method can be overriden
35
+ # def controller_path
36
+ # super
37
+ # end
38
+
39
+ # No action routing
40
+ # This method needs to be public since it is called from view helper
41
+ # def target_view_path
42
+ # super
43
+ # end
44
+
45
+ # protected
46
+
47
+ # def set_target
48
+ # super
49
+ # end
50
+
51
+ # def set_notification
52
+ # super
53
+ # end
54
+
55
+ # def load_notification_index(filter, limit)
56
+ # super(filter, limit)
57
+ # end
58
+
59
+ # def set_view_prefixes
60
+ # super
61
+ # end
62
+
63
+ # def return_back_or_ajax(filter, limit)
64
+ # super(filter, limit)
65
+ # end
66
+ end
@@ -0,0 +1,74 @@
1
+ class <%= @target_prefix %>NotificationsWithDeviseController < ActivityNotification::NotificationsWithDeviseController
2
+ # GET /:target_type/:target_id/notifcations
3
+ # def index
4
+ # super
5
+ # end
6
+
7
+ # POST /:target_type/:target_id/notifcations/open_all
8
+ # def open_all
9
+ # super
10
+ # end
11
+
12
+ # GET /:target_type/:target_id/notifcations/:id
13
+ # def show
14
+ # super
15
+ # end
16
+
17
+ # DELETE /:target_type/:target_id/notifcations/:id
18
+ # def destroy
19
+ # super
20
+ # end
21
+
22
+ # POST /:target_type/:target_id/notifcations/:id/open
23
+ # def open
24
+ # super
25
+ # end
26
+
27
+ # GET /:target_type/:target_id/notifcations/:id/move
28
+ # def move
29
+ # super
30
+ # end
31
+
32
+ # No action routing
33
+ # This method is called from target_view_path method
34
+ # This method can be overriden
35
+ # def controller_path
36
+ # super
37
+ # end
38
+
39
+ # No action routing
40
+ # This method needs to be public since it is called from view helper
41
+ # def target_view_path
42
+ # super
43
+ # end
44
+
45
+ # protected
46
+
47
+ # def set_target
48
+ # super
49
+ # end
50
+
51
+ # def set_notification
52
+ # super
53
+ # end
54
+
55
+ # def load_notification_index(filter, limit)
56
+ # super(filter, limit)
57
+ # end
58
+
59
+ # def set_view_prefixes
60
+ # super
61
+ # end
62
+
63
+ # def return_back_or_ajax(filter, limit)
64
+ # super(filter, limit)
65
+ # end
66
+
67
+ # def authenticate_devise_resource!
68
+ # super
69
+ # end
70
+
71
+ # def authenticate_target!
72
+ # super
73
+ # end
74
+ end