activity_notification 0.0.8 → 0.0.9

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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +11 -1
  3. data/README.md +63 -28
  4. data/activity_notification.gemspec +4 -2
  5. data/app/controllers/activity_notification/notifications_controller.rb +1 -1
  6. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +10 -10
  7. data/app/views/activity_notification/notifications/default/_index.html.erb +9 -4
  8. data/lib/activity_notification.rb +7 -6
  9. data/lib/activity_notification/apis/notification_api.rb +14 -15
  10. data/lib/activity_notification/common.rb +15 -7
  11. data/lib/activity_notification/config.rb +2 -0
  12. data/lib/activity_notification/helpers/view_helpers.rb +5 -4
  13. data/lib/activity_notification/mailers/helpers.rb +9 -9
  14. data/lib/activity_notification/models.rb +16 -0
  15. data/lib/activity_notification/models/{notifiable.rb → concerns/notifiable.rb} +15 -10
  16. data/lib/activity_notification/models/{notifier.rb → concerns/notifier.rb} +6 -0
  17. data/lib/activity_notification/models/{target.rb → concerns/target.rb} +34 -17
  18. data/lib/activity_notification/renderable.rb +2 -1
  19. data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -23
  20. data/lib/activity_notification/roles/acts_as_notifier.rb +11 -0
  21. data/lib/activity_notification/roles/acts_as_target.rb +9 -18
  22. data/lib/activity_notification/version.rb +1 -1
  23. data/lib/generators/activity_notification/{migration → active_record}/migration_generator.rb +5 -4
  24. data/lib/generators/activity_notification/controllers_generator.rb +1 -1
  25. data/lib/generators/activity_notification/install_generator.rb +3 -6
  26. data/lib/generators/activity_notification/{notification → models}/notification_generator.rb +5 -4
  27. data/lib/generators/activity_notification/views_generator.rb +20 -22
  28. data/lib/generators/templates/active_record/migration.rb +1 -1
  29. data/lib/generators/templates/activity_notification.rb +13 -3
  30. data/{config → lib/generators/templates}/locales/en.yml +0 -0
  31. data/lib/generators/templates/notification/notification.rb +4 -1
  32. data/spec/concerns/{notification_api_spec.rb → apis/notification_api_spec.rb} +169 -45
  33. data/spec/concerns/common_spec.rb +150 -0
  34. data/spec/concerns/models/notifiable_spec.rb +435 -0
  35. data/spec/concerns/models/notifier_spec.rb +23 -0
  36. data/spec/concerns/models/target_spec.rb +579 -0
  37. data/spec/concerns/renderable_spec.rb +110 -0
  38. data/spec/controllers/notifications_controller_shared_examples.rb +457 -0
  39. data/spec/controllers/notifications_controller_spec.rb +12 -0
  40. data/spec/controllers/notifications_with_devise_controller_spec.rb +81 -0
  41. data/spec/factories/admins.rb +5 -0
  42. data/spec/factories/articles.rb +1 -1
  43. data/spec/factories/comments.rb +1 -1
  44. data/spec/factories/dummy/dummy_notifiable.rb +4 -0
  45. data/spec/factories/dummy/dummy_notifier.rb +4 -0
  46. data/spec/factories/dummy/dummy_target.rb +4 -0
  47. data/spec/factories/notifications.rb +1 -1
  48. data/spec/factories/users.rb +7 -1
  49. data/spec/generators/active_record/migration_generator_spec.rb +41 -0
  50. data/spec/generators/controllers_generator_spec.rb +62 -0
  51. data/spec/generators/install_generator_spec.rb +43 -0
  52. data/spec/generators/models/notification_generator_spec.rb +41 -0
  53. data/spec/generators/views_generator_spec.rb +111 -0
  54. data/spec/helpers/polymorphic_helpers_spec.rb +89 -0
  55. data/spec/helpers/view_helpers_spec.rb +258 -0
  56. data/spec/mailers/mailer_spec.rb +98 -0
  57. data/spec/models/dummy/dummy_notifiable_spec.rb +6 -0
  58. data/spec/models/dummy/dummy_notifier_spec.rb +6 -0
  59. data/spec/models/dummy/dummy_target_spec.rb +6 -0
  60. data/spec/models/notification_spec.rb +5 -4
  61. data/spec/rails_app/app/assets/javascripts/application.js +2 -0
  62. data/spec/rails_app/app/controllers/articles_controller.rb +62 -0
  63. data/spec/rails_app/app/controllers/comments_controller.rb +34 -0
  64. data/spec/rails_app/app/models/admin.rb +8 -0
  65. data/spec/rails_app/app/models/article.rb +6 -6
  66. data/spec/rails_app/app/models/comment.rb +2 -2
  67. data/spec/rails_app/app/models/dummy/dummy_base.rb +2 -0
  68. data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +4 -0
  69. data/spec/rails_app/app/models/dummy/dummy_notifier.rb +4 -0
  70. data/spec/rails_app/app/models/dummy/dummy_target.rb +4 -0
  71. data/spec/rails_app/app/models/user.rb +5 -5
  72. data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_path_test.html.erb +1 -0
  73. data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_test.html.erb +1 -0
  74. data/spec/rails_app/app/views/activity_notification/notifications/users/_custom_index.html.erb +1 -0
  75. data/spec/rails_app/app/views/activity_notification/notifications/users/custom/_test.html.erb +1 -0
  76. data/spec/rails_app/app/views/articles/_form.html.erb +20 -0
  77. data/spec/rails_app/app/views/articles/edit.html.erb +6 -0
  78. data/spec/rails_app/app/views/articles/index.html.erb +67 -0
  79. data/spec/rails_app/app/views/articles/new.html.erb +5 -0
  80. data/spec/rails_app/app/views/articles/show.html.erb +38 -0
  81. data/spec/rails_app/app/views/layouts/_header.html.erb +8 -0
  82. data/spec/rails_app/app/views/layouts/application.html.erb +3 -4
  83. data/spec/rails_app/config/initializers/activity_notification.rb +13 -3
  84. data/spec/rails_app/config/initializers/devise.rb +274 -274
  85. data/spec/rails_app/config/locales/activity_notification.en.yml +20 -0
  86. data/spec/rails_app/config/locales/devise.en.yml +62 -0
  87. data/spec/rails_app/config/routes.rb +6 -2
  88. data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +6 -2
  89. data/spec/rails_app/db/schema.rb +8 -0
  90. data/spec/rails_app/db/seeds.rb +43 -0
  91. data/spec/roles/acts_as_notifiable_spec.rb +32 -0
  92. data/spec/roles/acts_as_notifier_spec.rb +17 -0
  93. data/spec/roles/acts_as_target_spec.rb +40 -0
  94. data/spec/spec_helper.rb +18 -14
  95. metadata +136 -12
@@ -0,0 +1,6 @@
1
+ describe Dummy::DummyNotifier, type: :model do
2
+
3
+ it_behaves_like :notifier
4
+ it_behaves_like :common
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ describe Dummy::DummyTarget, type: :model do
2
+
3
+ it_behaves_like :target
4
+ it_behaves_like :common
5
+
6
+ end
@@ -1,10 +1,11 @@
1
1
  describe ActivityNotification::Notification, type: :model do
2
2
 
3
3
  it_behaves_like :notification_api
4
+ it_behaves_like :renderable
4
5
 
5
6
  describe "with association" do
6
7
  it "belongs to target" do
7
- target = create(:user)
8
+ target = create(:confirmed_user)
8
9
  notification = create(:notification, target: target)
9
10
  expect(notification.reload.target).to eq(target)
10
11
  end
@@ -34,7 +35,7 @@ describe ActivityNotification::Notification, type: :model do
34
35
  end
35
36
 
36
37
  it "belongs to notifier" do
37
- notifier = create(:user)
38
+ notifier = create(:confirmed_user)
38
39
  notification = create(:notification, notifier: notifier)
39
40
  expect(notification.reload.notifier).to eq(notifier)
40
41
  end
@@ -168,8 +169,8 @@ describe ActivityNotification::Notification, type: :model do
168
169
  context "to filter by association" do
169
170
  before do
170
171
  ActivityNotification::Notification.delete_all
171
- @target_1, @notifiable_1, @group_1, @key_1 = create(:user), create(:article), nil, "key.1"
172
- @target_2, @notifiable_2, @group_2, @key_2 = create(:user), create(:comment), @notifiable_1, "key.2"
172
+ @target_1, @notifiable_1, @group_1, @key_1 = create(:confirmed_user), create(:article), nil, "key.1"
173
+ @target_2, @notifiable_2, @group_2, @key_2 = create(:confirmed_user), create(:comment), @notifiable_1, "key.2"
173
174
  @notification_1 = create(:notification, target: @target_1, notifiable: @notifiable_1, group: @group_1, key: @key_1)
174
175
  @notification_2 = create(:notification, target: @target_2, notifiable: @notifiable_2, group: @group_2, key: @key_2)
175
176
  end
@@ -0,0 +1,2 @@
1
+ //= require jquery
2
+ //= require jquery_ujs
@@ -0,0 +1,62 @@
1
+ class ArticlesController < ApplicationController
2
+ before_action :authenticate_user!, except: [:index, :show]
3
+ before_action :set_article, only: [:show, :edit, :update, :destroy]
4
+
5
+ # GET /articles
6
+ def index
7
+ @articles = Article.all.includes(:user)
8
+ end
9
+
10
+ # GET /articles/1
11
+ def show
12
+ @comment = Comment.new
13
+ end
14
+
15
+ # GET /articles/new
16
+ def new
17
+ @article = Article.new
18
+ end
19
+
20
+ # GET /articles/1/edit
21
+ def edit
22
+ end
23
+
24
+ # POST /articles
25
+ def create
26
+ @article = Article.new(article_params)
27
+ @article.user = current_user
28
+
29
+ if @article.save
30
+ @article.notify :users
31
+ redirect_to @article, notice: 'Article was successfully created.'
32
+ else
33
+ render :new
34
+ end
35
+ end
36
+
37
+ # PATCH/PUT /articles/1
38
+ def update
39
+ if @article.update(article_params)
40
+ redirect_to @article, notice: 'Article was successfully updated.'
41
+ else
42
+ render :edit
43
+ end
44
+ end
45
+
46
+ # DELETE /articles/1
47
+ def destroy
48
+ @article.destroy
49
+ redirect_to articles_url, notice: 'Article was successfully destroyed.'
50
+ end
51
+
52
+ private
53
+ # Use callbacks to share common setup or constraints between actions.
54
+ def set_article
55
+ @article = Article.find(params[:id])
56
+ end
57
+
58
+ # Only allow a trusted parameter "white list" through.
59
+ def article_params
60
+ params.require(:article).permit(:title, :body)
61
+ end
62
+ end
@@ -0,0 +1,34 @@
1
+ class CommentsController < ApplicationController
2
+ before_action :set_comment, only: [:destroy]
3
+
4
+ # POST /comments
5
+ def create
6
+ @comment = Comment.new(comment_params)
7
+ @comment.user = current_user
8
+
9
+ if @comment.save
10
+ @comment.notify :users
11
+ redirect_to @comment.article, notice: 'Comment was successfully created.'
12
+ else
13
+ redirect_to @comment.article
14
+ end
15
+ end
16
+
17
+ # DELETE /comments/1
18
+ def destroy
19
+ article = @comment.article
20
+ @comment.destroy
21
+ redirect_to article, notice: 'Comment was successfully destroyed.'
22
+ end
23
+
24
+ private
25
+ # Use callbacks to share common setup or constraints between actions.
26
+ def set_comment
27
+ @comment = Comment.find(params[:id])
28
+ end
29
+
30
+ # Only allow a trusted parameter "white list" through.
31
+ def comment_params
32
+ params.require(:comment).permit(:article_id, :body)
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ class Admin < ActiveRecord::Base
2
+ belongs_to :user
3
+ validates :user, presence: true
4
+
5
+ acts_as_notification_target email: :email,
6
+ email_allowed: ->(admin, key) { admin.user.confirmed_at.present? },
7
+ devise_resource: :user
8
+ end
@@ -1,12 +1,12 @@
1
1
  class Article < ActiveRecord::Base
2
- include ActivityNotification::Notifiable
2
+ belongs_to :user
3
+ has_many :comments, dependent: :delete_all
4
+ has_many :commented_users, through: :comments, source: :user
5
+ validates :user, presence: true
6
+
3
7
  acts_as_notifiable :users,
4
- targets: ->(article, key) { [article.user] },
8
+ targets: ->(article, key) { User.all.to_a - [article.user] },
5
9
  notifier: :user,
6
10
  email_allowed: true#,
7
11
  #notifiable_path: ->(article) { concept_issue_path(issue.concept, issue) }
8
-
9
- belongs_to :user
10
- has_many :comments, dependent: :delete_all
11
- has_many :commented_users, through: :comments, source: :user
12
12
  end
@@ -1,8 +1,9 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  belongs_to :article
3
3
  belongs_to :user
4
+ validates :article, presence: true
5
+ validates :user, presence: true
4
6
 
5
- include ActivityNotification::Notifiable
6
7
  acts_as_notifiable :users,
7
8
  targets: ->(comment, key) { (comment.article.commented_users.to_a - [comment.user] + [comment.article.user]).uniq },
8
9
  group: :article,
@@ -14,5 +15,4 @@ class Comment < ActiveRecord::Base
14
15
  def custom_notification_email_to_users_allowed?(user, key)
15
16
  true
16
17
  end
17
-
18
18
  end
@@ -0,0 +1,2 @@
1
+ class Dummy::DummyBase < ActiveRecord::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class Dummy::DummyNotifiable < ActiveRecord::Base
2
+ self.table_name = :articles
3
+ include ActivityNotification::Notifiable
4
+ end
@@ -0,0 +1,4 @@
1
+ class Dummy::DummyNotifier < ActiveRecord::Base
2
+ self.table_name = :users
3
+ include ActivityNotification::Notifier
4
+ end
@@ -0,0 +1,4 @@
1
+ class Dummy::DummyTarget < ActiveRecord::Base
2
+ self.table_name = :users
3
+ include ActivityNotification::Target
4
+ end
@@ -1,8 +1,8 @@
1
1
  class User < ActiveRecord::Base
2
- # devise :database_authenticatable, :registerable, :confirmable
3
-
4
- include ActivityNotification::Target
5
- acts_as_target email: :email, email_allowed: :confirmed_at
6
-
2
+ devise :database_authenticatable, :registerable, :confirmable
7
3
  validates :email, presence: true
4
+ has_many :articles, dependent: :delete_all
5
+ has_many :comments, through: :articles, dependent: :delete_all
6
+
7
+ acts_as_notification_target email: :email, email_allowed: :confirmed_at
8
8
  end
@@ -0,0 +1 @@
1
+ Custom template root for path test: <%= notification.id %>
@@ -0,0 +1 @@
1
+ Custom template root for default target: <%= notification.id %>
@@ -0,0 +1 @@
1
+ Custom index: <%= yield :notification_index %>
@@ -0,0 +1 @@
1
+ Custom template root for user target: <%= notification.id %>
@@ -0,0 +1,20 @@
1
+ <%= form_for(@article) do |f| %>
2
+ <% if @article.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @article.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <%= f.text_field :title %>
15
+ <%= f.text_area :body %>
16
+
17
+ <div class="actions">
18
+ <%= f.submit %>
19
+ </div>
20
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing Article</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @article %> |
6
+ <%= link_to 'Back', articles_path %>
@@ -0,0 +1,67 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <h1>Listing Users</h1>
4
+
5
+ <table>
6
+ <thead>
7
+ <tr>
8
+ <th colspan="3"></th>
9
+ </tr>
10
+ </thead>
11
+
12
+ <tbody>
13
+ <% User.all.each do |user| %>
14
+ <tr>
15
+ <td><%= user.email %></td>
16
+ <td><%= user.name %></td>
17
+ <td><%= link_to 'Notifications', user_notifications_path(user) %></td>
18
+ </tr>
19
+ <% end %>
20
+ </tbody>
21
+ </table>
22
+
23
+ <h1>Listing Admins</h1>
24
+
25
+ <table>
26
+ <thead>
27
+ <tr>
28
+ <th colspan="3"></th>
29
+ </tr>
30
+ </thead>
31
+
32
+ <tbody>
33
+ <% Admin.all.each do |admin| %>
34
+ <tr>
35
+ <td><%= admin.user.email %></td>
36
+ <td><%= admin.user.name %></td>
37
+ <td><%= link_to 'Notifications', admin_notifications_path(admin) %></td>
38
+ </tr>
39
+ <% end %>
40
+ </tbody>
41
+ </table>
42
+
43
+ <h1>Listing Articles</h1>
44
+
45
+ <table>
46
+ <thead>
47
+ <tr>
48
+ <th colspan="5"></th>
49
+ </tr>
50
+ </thead>
51
+
52
+ <tbody>
53
+ <% @articles.each do |article| %>
54
+ <tr>
55
+ <td><%= article.user.name %></td>
56
+ <td><%= article.title %></td>
57
+ <td><%= link_to 'Show', article %></td>
58
+ <td><%= link_to 'Edit', edit_article_path(article) if user_signed_in? and current_user == article.user %></td>
59
+ <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
60
+ </tr>
61
+ <% end %>
62
+ </tbody>
63
+ </table>
64
+
65
+ <br>
66
+
67
+ <%= link_to 'New Article', new_article_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New Article</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', articles_path %>
@@ -0,0 +1,38 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <h1><%= @article.title %></h1>
4
+ <p><%= @article.user.name %></p>
5
+ <p><%= @article.body %></p>
6
+ <%= link_to 'Edit', edit_article_path(@article) %> |
7
+ <%= link_to 'Destroy', @article, method: :delete, data: { confirm: 'Are you sure?' } %>
8
+ <%= link_to 'Back', articles_path %>
9
+
10
+ <h2>Listing Comments</h2>
11
+
12
+ <table>
13
+ <thead>
14
+ <tr>
15
+ <th colspan="3"></th>
16
+ </tr>
17
+ </thead>
18
+
19
+ <tbody>
20
+ <% @article.comments.each do |comment| %>
21
+ <tr>
22
+ <td><%= comment.user.name %></td>
23
+ <td><%= comment.body %></td>
24
+ <td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
25
+ </tr>
26
+ <% end %>
27
+ </tbody>
28
+ </table>
29
+
30
+ <h2>New Comment</h2>
31
+ <%= form_for(@comment) do |f| %>
32
+ <%= f.hidden_field :article_id, value: @article.id %>
33
+ <%= f.text_area :body %>
34
+
35
+ <div class="actions">
36
+ <%= f.submit %>
37
+ </div>
38
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <%= link_to 'Root', root_path %>
2
+ <% if user_signed_in? %>
3
+ <%= "Login as #{current_user.name}" %>
4
+ <%= link_to 'Logout', destroy_user_session_path, method: :delete %>
5
+ <%= render_notifications_of current_user, fallback: :default, index_content: :with_attributes %>
6
+ <% else %>
7
+ <%= link_to 'Login', new_user_session_path %>
8
+ <% end %>
@@ -2,13 +2,12 @@
2
2
  <html>
3
3
  <head>
4
4
  <title>Dummy</title>
5
- <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
- <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
5
+ <%#= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track': true %>
7
7
  <%= csrf_meta_tags %>
8
8
  </head>
9
9
  <body>
10
-
10
+ <%= render 'layouts/header' %>
11
11
  <%= yield %>
12
-
13
12
  </body>
14
13
  </html>
@@ -1,9 +1,17 @@
1
1
  ActivityNotification.configure do |config|
2
2
 
3
- # Table name to store notification data
3
+ # Configure if all activity notifications are enabled
4
+ # Set false when you want to turn off activity notifications
5
+ config.enabled = true
6
+
7
+ # Configure table name to store notification data
4
8
  config.table_name = "notifications"
5
9
 
6
- # ==> Mailer Configuration
10
+ # Configure if email notification is enabled as default
11
+ # 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
13
+ config.email_enabled = false
14
+
7
15
  # Configure the e-mail address which will be shown in ActivityNotification::Mailer,
8
16
  # note that it will be overwritten if you use your own mailer class with default "from" parameter.
9
17
  config.mailer_sender = 'please-change-me-at-config-initializers-activity_notification@example.com'
@@ -13,6 +21,8 @@ ActivityNotification.configure do |config|
13
21
 
14
22
  # Configure the parent class responsible to send e-mails.
15
23
  # config.parent_mailer = 'ActionMailer::Base'
16
-
24
+
25
+ # Configure default limit number of opened notifications you can get from opened* scope
17
26
  config.opened_limit = 10
27
+
18
28
  end