activity_notification 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +33 -0
  3. data/.rubocop.yml +1157 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG.md +25 -0
  6. data/Gemfile.lock +15 -17
  7. data/README.md +154 -27
  8. data/activity_notification.gemspec +1 -1
  9. data/app/controllers/activity_notification/notifications_controller.rb +30 -104
  10. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +1 -33
  11. data/app/controllers/activity_notification/subscriptions_controller.rb +184 -0
  12. data/app/controllers/activity_notification/subscriptions_with_devise_controller.rb +6 -0
  13. data/app/mailers/activity_notification/mailer.rb +3 -3
  14. data/app/views/activity_notification/notifications/default/_index.html.erb +3 -0
  15. data/app/views/activity_notification/notifications/default/index.html.erb +8 -10
  16. data/app/views/activity_notification/notifications/default/show.html.erb +24 -2
  17. data/app/views/activity_notification/subscriptions/default/_form.html.erb +52 -0
  18. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +89 -0
  19. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +73 -0
  20. data/app/views/activity_notification/subscriptions/default/_subscriptions.html.erb +13 -0
  21. data/app/views/activity_notification/subscriptions/default/create.js.erb +5 -0
  22. data/app/views/activity_notification/subscriptions/default/destroy.js.erb +5 -0
  23. data/app/views/activity_notification/subscriptions/default/index.html.erb +197 -0
  24. data/app/views/activity_notification/subscriptions/default/show.html.erb +177 -0
  25. data/app/views/activity_notification/subscriptions/default/subscribe.js.erb +8 -0
  26. data/app/views/activity_notification/subscriptions/default/subscribe_to_email.js.erb +6 -0
  27. data/app/views/activity_notification/subscriptions/default/unsubscribe.js.erb +8 -0
  28. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_email.js.erb +6 -0
  29. data/gemfiles/Gemfile.rails-4.2.lock +18 -20
  30. data/gemfiles/Gemfile.rails-5.0.lock +18 -20
  31. data/lib/activity_notification.rb +7 -3
  32. data/lib/activity_notification/apis/notification_api.rb +95 -61
  33. data/lib/activity_notification/apis/subscription_api.rb +51 -0
  34. data/lib/activity_notification/common.rb +1 -1
  35. data/lib/activity_notification/config.rb +65 -17
  36. data/lib/activity_notification/controllers/common_controller.rb +118 -0
  37. data/lib/activity_notification/controllers/devise_authentication_controller.rb +41 -0
  38. data/lib/activity_notification/helpers/view_helpers.rb +131 -3
  39. data/lib/activity_notification/mailers/helpers.rb +6 -8
  40. data/lib/activity_notification/models/concerns/notifiable.rb +45 -27
  41. data/lib/activity_notification/models/concerns/subscriber.rb +149 -0
  42. data/lib/activity_notification/models/concerns/target.rb +100 -66
  43. data/lib/activity_notification/models/notification.rb +7 -5
  44. data/lib/activity_notification/models/subscription.rb +93 -0
  45. data/lib/activity_notification/rails/routes.rb +148 -33
  46. data/lib/activity_notification/renderable.rb +3 -4
  47. data/lib/activity_notification/roles/acts_as_notifiable.rb +14 -1
  48. data/lib/activity_notification/roles/acts_as_target.rb +11 -8
  49. data/lib/activity_notification/version.rb +1 -1
  50. data/lib/generators/activity_notification/controllers_generator.rb +2 -2
  51. data/lib/generators/activity_notification/install_generator.rb +0 -1
  52. data/lib/generators/activity_notification/migration/migration_generator.rb +8 -2
  53. data/lib/generators/activity_notification/models_generator.rb +53 -0
  54. data/lib/generators/activity_notification/views_generator.rb +7 -7
  55. data/lib/generators/templates/activity_notification.rb +17 -3
  56. data/lib/generators/templates/controllers/notifications_controller.rb +18 -17
  57. data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +18 -17
  58. data/lib/generators/templates/controllers/subscriptions_controller.rb +79 -0
  59. data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +87 -0
  60. data/lib/generators/templates/migrations/migration.rb +57 -0
  61. data/lib/generators/templates/models/README +10 -0
  62. data/lib/generators/templates/{notification → models}/notification.rb +1 -3
  63. data/lib/generators/templates/models/subscription.rb +4 -0
  64. data/spec/concerns/apis/notification_api_spec.rb +48 -11
  65. data/spec/concerns/apis/subscription_api_spec.rb +167 -0
  66. data/spec/concerns/models/notifiable_spec.rb +60 -0
  67. data/spec/concerns/models/subscriber_spec.rb +525 -0
  68. data/spec/concerns/models/target_spec.rb +271 -42
  69. data/spec/controllers/common_controller_spec.rb +25 -0
  70. data/spec/controllers/dummy_common_controller.rb +5 -0
  71. data/spec/controllers/notifications_controller_shared_examples.rb +2 -6
  72. data/spec/controllers/subscriptions_controller_shared_examples.rb +735 -0
  73. data/spec/controllers/subscriptions_controller_spec.rb +12 -0
  74. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +91 -0
  75. data/spec/factories/dummy/dummy_subscriber.rb +4 -0
  76. data/spec/factories/subscriptions.rb +8 -0
  77. data/spec/generators/controllers_generator_spec.rb +25 -2
  78. data/spec/generators/migration/migration_generator_spec.rb +3 -3
  79. data/spec/generators/models_generator_spec.rb +96 -0
  80. data/spec/generators/views_generator_spec.rb +84 -0
  81. data/spec/helpers/view_helpers_spec.rb +143 -0
  82. data/spec/mailers/mailer_spec.rb +5 -4
  83. data/spec/models/dummy/dummy_subscriber_spec.rb +5 -0
  84. data/spec/models/notification_spec.rb +7 -7
  85. data/spec/models/subscription_spec.rb +158 -0
  86. data/spec/rails_app/app/controllers/users/notifications_controller.rb +67 -0
  87. data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +75 -0
  88. data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +79 -0
  89. data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +87 -0
  90. data/spec/rails_app/app/models/admin.rb +1 -0
  91. data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +4 -0
  92. data/spec/rails_app/app/models/user.rb +2 -1
  93. data/spec/rails_app/app/views/activity_notification/mailer/dummy_subscribers/test_key.text.erb +1 -0
  94. data/spec/rails_app/app/views/articles/index.html.erb +6 -0
  95. data/spec/rails_app/config/initializers/activity_notification.rb +17 -3
  96. data/spec/rails_app/config/routes.rb +2 -2
  97. data/spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb +33 -0
  98. data/spec/rails_app/db/schema.rb +18 -0
  99. data/spec/roles/acts_as_notifiable_spec.rb +1 -1
  100. data/spec/roles/acts_as_target_spec.rb +1 -1
  101. metadata +70 -11
  102. data/lib/generators/activity_notification/notification/notification_generator.rb +0 -20
  103. data/lib/generators/templates/active_record/migration.rb +0 -18
  104. data/spec/generators/notification/notification_generator_spec.rb +0 -41
  105. data/spec/rails_app/db/migrate/20160715050420_create_notifications.rb +0 -18
@@ -1,5 +1,4 @@
1
1
  require 'rails/generators/base'
2
- require 'securerandom'
3
2
 
4
3
  module ActivityNotification
5
4
  module Generators #:nodoc:
@@ -6,13 +6,19 @@ module ActivityNotification
6
6
  # @example Run migration generator
7
7
  # rails generate activity_notification:migration
8
8
  class MigrationGenerator < ActiveRecord::Generators::Base
9
- source_root File.expand_path("../../../templates/active_record", __FILE__)
9
+ MIGRATION_TABLES = ['notifications', 'subscriptions'].freeze
10
10
 
11
- argument :name, type: :string, default: 'CreateNotifications'
11
+ source_root File.expand_path("../../../templates/migrations", __FILE__)
12
+
13
+ argument :name, type: :string, default: 'CreateActivityNotificationTables',
14
+ desc: "The migration name to create tables"
15
+ class_option :tables, aliases: "-t", type: :array,
16
+ desc: "Select specific tables to generate (#{MIGRATION_TABLES.join(', ')})"
12
17
 
13
18
  # Create migration files in application directory
14
19
  def create_migrations
15
20
  @migration_name = name
21
+ @migration_tables = options[:tables] || MIGRATION_TABLES
16
22
  migration_template 'migration.rb', "db/migrate/#{name.underscore}.rb"
17
23
  end
18
24
  end
@@ -0,0 +1,53 @@
1
+ require 'rails/generators/base'
2
+
3
+ module ActivityNotification
4
+ module Generators
5
+ # Notification generator to create customizable notification model from templates.
6
+ # @example Run notification generator to create customizable notification model
7
+ # rails generate activity_notification:models users
8
+ class ModelsGenerator < Rails::Generators::Base
9
+ MODELS = ['notification', 'subscription'].freeze
10
+
11
+ desc <<-DESC.strip_heredoc
12
+ Create inherited ActivityNotification models in your app/models folder.
13
+
14
+ Use -m to specify which model you want to overwrite.
15
+ If you do no specify a model, all models will be created.
16
+ For example:
17
+
18
+ rails generate activity_notification:models users -m notification
19
+
20
+ This will create a model class at app/models/users/notification.rb like this:
21
+
22
+ class Users::Notification < ActivityNotification::Notification
23
+ content...
24
+ end
25
+ DESC
26
+
27
+ source_root File.expand_path("../../templates/models", __FILE__)
28
+ argument :target, required: true,
29
+ desc: "The target to create models in, e.g. users, admins"
30
+ class_option :models, aliases: "-m", type: :array,
31
+ desc: "Select specific models to generate (#{MODELS.join(', ')})"
32
+ class_option :names, aliases: "-n", type: :array,
33
+ desc: "Select model names to generate (#{MODELS.join(', ')})"
34
+
35
+ # Create notification model in application directory
36
+ def create_models
37
+ @target_prefix = target.blank? ? '' : (target.camelize + '::')
38
+ models = options[:models] || MODELS
39
+ model_names = options[:names] || MODELS
40
+ models.zip(model_names).each do |original_name, new_name|
41
+ @model_name = new_name.camelize
42
+ template "#{original_name}.rb",
43
+ "app/models/#{target}/#{@model_name.underscore}.rb"
44
+ end
45
+ end
46
+
47
+ # Shows readme to console
48
+ def show_readme
49
+ readme "README" if behavior == :invoke
50
+ end
51
+ end
52
+ end
53
+ end
@@ -14,7 +14,7 @@ module ActivityNotification
14
14
  # @example Run view generator to create only notification email views
15
15
  # rails generate activity_notification:views -v mailer
16
16
  class ViewsGenerator < Rails::Generators::Base
17
- VIEWS = [:notifications, :mailer].freeze
17
+ VIEWS = [:notifications, :mailer, :subscriptions].freeze
18
18
 
19
19
  source_root File.expand_path("../../../../app/views/activity_notification", __FILE__)
20
20
  desc "Copies default ActivityNotification views to your application."
@@ -22,7 +22,7 @@ module ActivityNotification
22
22
  argument :target, required: false, default: nil,
23
23
  desc: "The target to copy views to"
24
24
  class_option :views, aliases: "-v", type: :array,
25
- desc: "Select specific view directories to generate (notifications, mailer)"
25
+ desc: "Select specific view directories to generate (notifications, mailer, subscriptions)"
26
26
  public_task :copy_views
27
27
 
28
28
  # Copies view files in application directory
@@ -37,15 +37,15 @@ module ActivityNotification
37
37
 
38
38
  # Copies view files to target directory
39
39
  # @api protected
40
- # @param [String] name Set name of views (notifications or mailer)
41
- # @param [String] _target_path Target path to create views
42
- def view_directory(name, _target_path = nil)
43
- directory "#{name.to_s}/default", _target_path || "#{target_path}/#{name}/#{plural_target || :default}"
40
+ # @param [String] name Set name of views (notifications or mailer)
41
+ # @param [String] view_target_path Target path to create views
42
+ def view_directory(name, view_target_path = nil)
43
+ directory "#{name}/default", view_target_path || "#{target_path}/#{name}/#{plural_target || :default}"
44
44
  end
45
45
 
46
46
  # Gets target_path from an argument or default value
47
47
  # @api protected
48
- # @return [String ] target_path from an argument or default value
48
+ # @return [String] target_path from an argument or default value
49
49
  def target_path
50
50
  @target_path ||= "app/views/activity_notification"
51
51
  end
@@ -5,13 +5,27 @@ ActivityNotification.configure do |config|
5
5
  config.enabled = true
6
6
 
7
7
  # Configure table name to store notification data
8
- config.table_name = "notifications"
8
+ config.notification_table_name = "notifications"
9
9
 
10
- # Configure if email notification is enabled as default
10
+ # Configure table name to store subscription data
11
+ config.subscription_table_name = "subscriptions"
12
+
13
+ # Configure if email notification is enabled as default.
11
14
  # Note that you can configure them for each model by acts_as roles.
12
- # Set true when you want to turn on email notifications as default
15
+ # Set true when you want to turn on email notifications as default.
13
16
  config.email_enabled = false
14
17
 
18
+ # Configure if subscription is managed.
19
+ # Note that this parameter must be true when you want use subscription management.
20
+ # However, you can also configure them for each model by acts_as roles.
21
+ # Set true when you want to turn on subscription management as default.
22
+ config.subscription_enabled = false
23
+
24
+ # Configure default subscription value to use when the subscription record does not configured.
25
+ # Note that you can configure them for each method calling as default argument.
26
+ # Set false when you want to unsubscribe to any notifications as default.
27
+ config.subscribe_as_default = true
28
+
15
29
  # Configure the e-mail address which will be shown in ActivityNotification::Mailer,
16
30
  # note that it will be overwritten if you use your own mailer class with default "from" parameter.
17
31
  config.mailer_sender = 'please-change-me-at-config-initializers-activity_notification@example.com'
@@ -1,41 +1,34 @@
1
1
  class <%= @target_prefix %>NotificationsController < ActivityNotification::NotificationsController
2
- # GET /:target_type/:target_id/notifcations
2
+ # GET /:target_type/:target_id/notifications
3
3
  # def index
4
4
  # super
5
5
  # end
6
6
 
7
- # POST /:target_type/:target_id/notifcations/open_all
7
+ # POST /:target_type/:target_id/notifications/open_all
8
8
  # def open_all
9
9
  # super
10
10
  # end
11
11
 
12
- # GET /:target_type/:target_id/notifcations/:id
12
+ # GET /:target_type/:target_id/notifications/:id
13
13
  # def show
14
14
  # super
15
15
  # end
16
16
 
17
- # DELETE /:target_type/:target_id/notifcations/:id
17
+ # DELETE /:target_type/:target_id/notifications/:id
18
18
  # def destroy
19
19
  # super
20
20
  # end
21
21
 
22
- # POST /:target_type/:target_id/notifcations/:id/open
22
+ # POST /:target_type/:target_id/notifications/:id/open
23
23
  # def open
24
24
  # super
25
25
  # end
26
26
 
27
- # GET /:target_type/:target_id/notifcations/:id/move
27
+ # GET /:target_type/:target_id/notifications/:id/move
28
28
  # def move
29
29
  # super
30
30
  # end
31
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
32
  # No action routing
40
33
  # This method needs to be public since it is called from view helper
41
34
  # def target_view_path
@@ -52,15 +45,23 @@ class <%= @target_prefix %>NotificationsController < ActivityNotification::Notif
52
45
  # super
53
46
  # end
54
47
 
55
- # def load_notification_index(filter, limit)
56
- # super(filter, limit)
48
+ # def set_index_options
49
+ # super
50
+ # end
51
+
52
+ # def load_index
53
+ # super
54
+ # end
55
+
56
+ # def controller_path
57
+ # super
57
58
  # end
58
59
 
59
60
  # def set_view_prefixes
60
61
  # super
61
62
  # end
62
63
 
63
- # def return_back_or_ajax(filter, limit)
64
- # super(filter, limit)
64
+ # def return_back_or_ajax
65
+ # super
65
66
  # end
66
67
  end
@@ -1,41 +1,34 @@
1
1
  class <%= @target_prefix %>NotificationsWithDeviseController < ActivityNotification::NotificationsWithDeviseController
2
- # GET /:target_type/:target_id/notifcations
2
+ # GET /:target_type/:target_id/notifications
3
3
  # def index
4
4
  # super
5
5
  # end
6
6
 
7
- # POST /:target_type/:target_id/notifcations/open_all
7
+ # POST /:target_type/:target_id/notifications/open_all
8
8
  # def open_all
9
9
  # super
10
10
  # end
11
11
 
12
- # GET /:target_type/:target_id/notifcations/:id
12
+ # GET /:target_type/:target_id/notifications/:id
13
13
  # def show
14
14
  # super
15
15
  # end
16
16
 
17
- # DELETE /:target_type/:target_id/notifcations/:id
17
+ # DELETE /:target_type/:target_id/notifications/:id
18
18
  # def destroy
19
19
  # super
20
20
  # end
21
21
 
22
- # POST /:target_type/:target_id/notifcations/:id/open
22
+ # POST /:target_type/:target_id/notifications/:id/open
23
23
  # def open
24
24
  # super
25
25
  # end
26
26
 
27
- # GET /:target_type/:target_id/notifcations/:id/move
27
+ # GET /:target_type/:target_id/notifications/:id/move
28
28
  # def move
29
29
  # super
30
30
  # end
31
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
32
  # No action routing
40
33
  # This method needs to be public since it is called from view helper
41
34
  # def target_view_path
@@ -52,16 +45,24 @@ class <%= @target_prefix %>NotificationsWithDeviseController < ActivityNotificat
52
45
  # super
53
46
  # end
54
47
 
55
- # def load_notification_index(filter, limit)
56
- # super(filter, limit)
48
+ # def set_index_options
49
+ # super
50
+ # end
51
+
52
+ # def load_index
53
+ # super
54
+ # end
55
+
56
+ # def controller_path
57
+ # super
57
58
  # end
58
59
 
59
60
  # def set_view_prefixes
60
61
  # super
61
62
  # end
62
63
 
63
- # def return_back_or_ajax(filter, limit)
64
- # super(filter, limit)
64
+ # def return_back_or_ajax
65
+ # super
65
66
  # end
66
67
 
67
68
  # def authenticate_devise_resource!
@@ -0,0 +1,79 @@
1
+ class <%= @target_prefix %>SubscriptionsController < ActivityNotification::SubscriptionsController
2
+ # GET /:target_type/:target_id/subscriptions
3
+ # def index
4
+ # super
5
+ # end
6
+
7
+ # POST /:target_type/:target_id/subscriptions
8
+ # def create
9
+ # super
10
+ # end
11
+
12
+ # GET /:target_type/:target_id/subscriptions/:id
13
+ # def show
14
+ # super
15
+ # end
16
+
17
+ # DELETE /:target_type/:target_id/subscriptions/:id
18
+ # def destroy
19
+ # super
20
+ # end
21
+
22
+ # POST /:target_type/:target_id/subscriptions/:id/subscribe
23
+ # def subscribe
24
+ # super
25
+ # end
26
+
27
+ # POST /:target_type/:target_id/subscriptions/:id/unsubscribe
28
+ # def unsubscribe
29
+ # super
30
+ # end
31
+
32
+ # POST /:target_type/:target_id/subscriptions/:id/subscribe_to_email
33
+ # def subscribe_to_email
34
+ # super
35
+ # end
36
+
37
+ # POST /:target_type/:target_id/subscriptions/:id/unsubscribe_to_email
38
+ # def unsubscribe_to_email
39
+ # super
40
+ # end
41
+
42
+ # protected
43
+
44
+ # def set_target
45
+ # super
46
+ # end
47
+
48
+ # def set_subscription
49
+ # super
50
+ # end
51
+
52
+ # def subscription_params
53
+ # super
54
+ # end
55
+
56
+ # def set_index_options
57
+ # super
58
+ # end
59
+
60
+ # def load_index
61
+ # super
62
+ # end
63
+
64
+ # def controller_path
65
+ # super
66
+ # end
67
+
68
+ # def target_view_path
69
+ # super
70
+ # end
71
+
72
+ # def set_view_prefixes
73
+ # super
74
+ # end
75
+
76
+ # def return_back_or_ajax
77
+ # super
78
+ # end
79
+ end
@@ -0,0 +1,87 @@
1
+ class <%= @target_prefix %>SubscriptionsWithDeviseController < ActivityNotification::SubscriptionsWithDeviseController
2
+ # GET /:target_type/:target_id/subscriptions
3
+ # def index
4
+ # super
5
+ # end
6
+
7
+ # POST /:target_type/:target_id/subscriptions
8
+ # def create
9
+ # super
10
+ # end
11
+
12
+ # GET /:target_type/:target_id/subscriptions/:id
13
+ # def show
14
+ # super
15
+ # end
16
+
17
+ # DELETE /:target_type/:target_id/subscriptions/:id
18
+ # def destroy
19
+ # super
20
+ # end
21
+
22
+ # POST /:target_type/:target_id/subscriptions/:id/subscribe
23
+ # def subscribe
24
+ # super
25
+ # end
26
+
27
+ # POST /:target_type/:target_id/subscriptions/:id/unsubscribe
28
+ # def unsubscribe
29
+ # super
30
+ # end
31
+
32
+ # POST /:target_type/:target_id/subscriptions/:id/subscribe_to_email
33
+ # def subscribe_to_email
34
+ # super
35
+ # end
36
+
37
+ # POST /:target_type/:target_id/subscriptions/:id/unsubscribe_to_email
38
+ # def unsubscribe_to_email
39
+ # super
40
+ # end
41
+
42
+ # protected
43
+
44
+ # def set_target
45
+ # super
46
+ # end
47
+
48
+ # def set_subscription
49
+ # super
50
+ # end
51
+
52
+ # def subscription_params
53
+ # super
54
+ # end
55
+
56
+ # def set_index_options
57
+ # super
58
+ # end
59
+
60
+ # def load_index
61
+ # super
62
+ # end
63
+
64
+ # def controller_path
65
+ # super
66
+ # end
67
+
68
+ # def target_view_path
69
+ # super
70
+ # end
71
+
72
+ # def set_view_prefixes
73
+ # super
74
+ # end
75
+
76
+ # def return_back_or_ajax
77
+ # super
78
+ # end
79
+
80
+ # def authenticate_devise_resource!
81
+ # super
82
+ # end
83
+
84
+ # def authenticate_target!
85
+ # super
86
+ # end
87
+ end