merges activities into notifications
This commit is contained in:
@@ -109,21 +109,4 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
def track_activity(trackable)
|
||||
if trackable.is_a? Comment
|
||||
action = trackable.root? ? "debate_comment" : "comment_reply"
|
||||
activity = current_user.activities.create! action: action, trackable: trackable
|
||||
add_notifications_for activity
|
||||
end
|
||||
end
|
||||
|
||||
def add_notifications_for(activity)
|
||||
case activity.action
|
||||
when "debate_comment"
|
||||
author = activity.trackable.debate.author
|
||||
when "comment_reply"
|
||||
author = activity.trackable.parent.author
|
||||
end
|
||||
author.notifications.create!(activity: activity) unless activity.made_by? author
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@ class CommentsController < ApplicationController
|
||||
def create
|
||||
if @comment.save
|
||||
CommentNotifier.new(comment: @comment).process
|
||||
track_activity @comment
|
||||
add_notification @comment
|
||||
else
|
||||
render :new
|
||||
end
|
||||
@@ -63,4 +63,13 @@ class CommentsController < ApplicationController
|
||||
["1", true].include?(comment_params[:as_moderator]) && can?(:comment_as_moderator, @commentable)
|
||||
end
|
||||
|
||||
def add_notification(comment)
|
||||
if comment.reply?
|
||||
author = comment.parent.author
|
||||
else
|
||||
author = comment.commentable.author
|
||||
end
|
||||
author.notifications.create!(notifiable: comment) unless comment.made_by? author
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,8 +3,7 @@ class NotificationsController < ApplicationController
|
||||
load_and_authorize_resource class: "User"
|
||||
|
||||
def index
|
||||
@notifications = current_user.notifications.unread.recent.for_render
|
||||
@notifications.each { |notification| notification.mark_as_read! }
|
||||
@notifications = current_user.notifications.recent.for_render
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
module NotificationsHelper
|
||||
|
||||
def notification_text_for(notification)
|
||||
case notification.activity.action
|
||||
when "debate_comment"
|
||||
t("comments.notifications.commented_on_your_debate")
|
||||
when "comment_reply"
|
||||
if notification.notifiable.reply?
|
||||
t("comments.notifications.replied_to_your_comment")
|
||||
else
|
||||
t("comments.notifications.commented_on_your_debate")
|
||||
end
|
||||
end
|
||||
|
||||
def notifications_class_for(user)
|
||||
user.notifications.unread.count > 0 ? "with_notifications" : "without_notifications"
|
||||
user.notifications.count > 0 ? "with_notifications" : "without_notifications"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -90,6 +90,10 @@ class Comment < ActiveRecord::Base
|
||||
!root?
|
||||
end
|
||||
|
||||
def made_by?(user)
|
||||
self.user == user
|
||||
end
|
||||
|
||||
def call_after_commented
|
||||
self.commentable.try(:after_commented)
|
||||
end
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
class Notification < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :activity
|
||||
belongs_to :notifiable, polymorphic: true
|
||||
|
||||
scope :unread, -> { where(read: false) }
|
||||
scope :unread, -> { all }
|
||||
scope :recent, -> { order(id: :desc) }
|
||||
scope :for_render, -> { includes(activity: [:user, :trackable]) }
|
||||
scope :for_render, -> { includes(notifiable: [:user]) }
|
||||
|
||||
def username
|
||||
notifiable.user.username
|
||||
end
|
||||
|
||||
def timestamp
|
||||
activity.trackable.created_at
|
||||
notifiable.created_at
|
||||
end
|
||||
|
||||
def mark_as_read!
|
||||
update_attribute :read, true
|
||||
end
|
||||
end
|
||||
@@ -3,8 +3,8 @@
|
||||
<% @notifications.each do |notification| %>
|
||||
<li>
|
||||
<time><%= l notification.timestamp, format: :datetime %></time> •
|
||||
<%= notification.activity.username %> <%= notification_text_for(notification) %>
|
||||
<%= link_to notification.activity.trackable.debate.title, notification.activity.trackable.debate %>
|
||||
<%= notification.username %> <%= notification_text_for(notification) %>
|
||||
<%= link_to notification.notifiable.commentable.title, notification.notifiable.commentable %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
class CreateActivities < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :activities do |t|
|
||||
t.belongs_to :user, index: true, foreign_key: true
|
||||
t.string :action
|
||||
t.belongs_to :trackable, polymorphic: true
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class MergeActivitiesAndNotifications < ActiveRecord::Migration
|
||||
def change
|
||||
change_table :notifications do |t|
|
||||
t.remove :read
|
||||
t.remove :activity_id
|
||||
t.references :notifiable, polymorphic: true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
11
db/schema.rb
11
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20151215165824) do
|
||||
ActiveRecord::Schema.define(version: 20160105170113) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -206,13 +206,10 @@ ActiveRecord::Schema.define(version: 20151215165824) do
|
||||
|
||||
create_table "notifications", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "activity_id"
|
||||
t.boolean "read", default: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "notifiable_id"
|
||||
t.string "notifiable_type"
|
||||
end
|
||||
|
||||
add_index "notifications", ["activity_id"], name: "index_notifications_on_activity_id", using: :btree
|
||||
add_index "notifications", ["user_id"], name: "index_notifications_on_user_id", using: :btree
|
||||
|
||||
create_table "organizations", force: :cascade do |t|
|
||||
@@ -407,7 +404,6 @@ ActiveRecord::Schema.define(version: 20151215165824) do
|
||||
add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope", using: :btree
|
||||
add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree
|
||||
|
||||
add_foreign_key "activities", "users"
|
||||
add_foreign_key "administrators", "users"
|
||||
add_foreign_key "annotations", "legislations"
|
||||
add_foreign_key "annotations", "users"
|
||||
@@ -416,7 +412,6 @@ ActiveRecord::Schema.define(version: 20151215165824) do
|
||||
add_foreign_key "identities", "users"
|
||||
add_foreign_key "locks", "users"
|
||||
add_foreign_key "moderators", "users"
|
||||
add_foreign_key "notifications", "activities"
|
||||
add_foreign_key "notifications", "users"
|
||||
add_foreign_key "organizations", "users"
|
||||
end
|
||||
|
||||
@@ -291,9 +291,8 @@ FactoryGirl.define do
|
||||
end
|
||||
|
||||
factory :notification do
|
||||
association :user, factory: :user
|
||||
association :activity, factory: :activity
|
||||
read false
|
||||
user
|
||||
association :notifiable, factory: :comment
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,20 +3,21 @@ require 'rails_helper'
|
||||
describe NotificationsHelper do
|
||||
|
||||
describe "#notification_text_for" do
|
||||
let(:comment_activity) { create :activity, action: "debate_comment" }
|
||||
let(:reply_activity) { create :activity, action: "comment_reply" }
|
||||
let(:debate) { create :debate }
|
||||
let(:debate_comment) { create :comment, commentable: debate }
|
||||
let(:comment_reply) { create :comment, commentable: debate, parent: debate_comment }
|
||||
|
||||
context "when action was comment on a debate" do
|
||||
it "returns 'commented_on_your_debate' locale text" do
|
||||
notification = create :notification, activity: comment_activity
|
||||
expect(notification_text_for(notification)).to eq t("comments.notifications.commented_on_your_debate")
|
||||
it "returns correct text when someone comments on your debate" do
|
||||
notification = create :notification, notifiable: debate_comment
|
||||
expect(notification_text_for(notification)).to eq "commented on your debate"
|
||||
end
|
||||
end
|
||||
|
||||
context "when action was comment on a debate" do
|
||||
it "returns 'replied_to_your_comment' locale text" do
|
||||
notification = create :notification, activity: reply_activity
|
||||
expect(notification_text_for(notification)).to eq t("comments.notifications.replied_to_your_comment")
|
||||
it "returns correct text when someone replies to your comment" do
|
||||
notification = create :notification, notifiable: comment_reply
|
||||
expect(notification_text_for(notification)).to eq "replied to your comment on"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -128,4 +128,19 @@ describe Comment do
|
||||
expect(Comment.not_as_admin_or_moderator.first).to eq(comment1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#made_by?" do
|
||||
let(:author) { create :user }
|
||||
let(:comment) { create :comment, user: author }
|
||||
|
||||
it "returns true if comment was made by user" do
|
||||
expect(comment.made_by?(author)).to be true
|
||||
end
|
||||
|
||||
it "returns false if comment was not made by user" do
|
||||
not_author = create :user
|
||||
expect(comment.made_by?(not_author)).to be false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -26,8 +26,8 @@ describe Notification do
|
||||
end
|
||||
|
||||
describe "#for_render (scope)" do
|
||||
it "returns notifications with including activity, user and trackable info" do
|
||||
expect(Notification).to receive(:includes).with(activity: [:user, :trackable]).exactly(:once)
|
||||
it "returns notifications including notifiable and user" do
|
||||
expect(Notification).to receive(:includes).with(notifiable: [:user]).exactly(:once)
|
||||
Notification.for_render
|
||||
end
|
||||
end
|
||||
@@ -35,8 +35,7 @@ describe Notification do
|
||||
describe "#timestamp" do
|
||||
it "returns the timestamp of the trackable object" do
|
||||
comment = create :comment
|
||||
activity = create :activity, trackable: comment
|
||||
notification = create :notification, activity: activity
|
||||
notification = create :notification, notifiable: comment
|
||||
|
||||
expect(notification.timestamp).to eq comment.created_at
|
||||
end
|
||||
@@ -51,4 +50,13 @@ describe Notification do
|
||||
expect(notification.read).to be true
|
||||
end
|
||||
end
|
||||
|
||||
describe "#username" do
|
||||
it "returns the username of the activity's author" do
|
||||
comment = create :comment
|
||||
notification = create :notification, notifiable: comment
|
||||
expect(notification.username).to eq comment.author.username
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user