refactors notification helpers

This commit is contained in:
rgarcia
2016-06-15 12:12:56 +02:00
parent 95d244003e
commit e2689d640c
5 changed files with 44 additions and 36 deletions

View File

@@ -1,13 +1,4 @@
module NotificationsHelper module NotificationsHelper
def notification_action(notification)
case notification.notifiable_type
when "ProposalNotification"
"proposal_notification"
when "Comment"
"replies_to"
else
"comments_on"
end
end
end end

View File

@@ -33,6 +33,17 @@ class Notification < ActiveRecord::Base
end end
end end
def notifiable_action
case notifiable_type
when "ProposalNotification"
"proposal_notification"
when "Comment"
"replies_to"
else
"comments_on"
end
end
def linkable_resource def linkable_resource
notifiable.is_a?(ProposalNotification) ? notifiable.proposal : notifiable notifiable.is_a?(ProposalNotification) ? notifiable.proposal : notifiable
end end

View File

@@ -2,7 +2,7 @@
<%= link_to notification do %> <%= link_to notification do %>
<p> <p>
<em> <em>
<%= t("notifications.index.#{notification_action(notification)}", <%= t("notifications.index.#{notification.notifiable_action}",
count: notification.counter) %> count: notification.counter) %>
</em> </em>
<strong><%= notification.notifiable_title %></strong> <strong><%= notification.notifiable_title %></strong>

View File

@@ -1,25 +0,0 @@
require 'rails_helper'
describe NotificationsHelper do
describe "#notification_action" do
let(:debate) { create :debate }
let(:debate_comment) { create :comment, commentable: debate }
context "when action was comment on a debate" do
it "returns correct text when someone comments on your debate" do
notification = create :notification, notifiable: debate
expect(notification_action(notification)).to eq "comments_on"
end
end
context "when action was comment on a debate" do
it "returns correct text when someone replies to your comment" do
notification = create :notification, notifiable: debate_comment
expect(notification_action(notification)).to eq "replies_to"
end
end
end
end

View File

@@ -47,4 +47,35 @@ describe Notification do
end end
end end
describe "#notification_action" do
context "when action was comment on a debate" do
it "returns correct text when someone comments on your debate" do
debate = create(:debate)
notification = create :notification, notifiable: debate
expect(notification.notifiable_action).to eq "comments_on"
end
end
context "when action was comment on a debate" do
it "returns correct text when someone replies to your comment" do
debate = create(:debate)
debate_comment = create :comment, commentable: debate
notification = create :notification, notifiable: debate_comment
expect(notification.notifiable_action).to eq "replies_to"
end
end
context "when action was proposal notification" do
it "returns correct text when the author created a proposal notification" do
proposal_notification = create(:proposal_notification)
notification = create :notification, notifiable: proposal_notification
expect(notification.notifiable_action).to eq "proposal_notification"
end
end
end
end end