diff --git a/app/models/concerns/notifiable.rb b/app/models/concerns/notifiable.rb index d9ba2f914..dcc04dc36 100644 --- a/app/models/concerns/notifiable.rb +++ b/app/models/concerns/notifiable.rb @@ -17,20 +17,20 @@ module Notifiable end def notifiable_available? - case self.class.name - when "ProposalNotification" - check_availability(proposal) - when "Comment" - check_availability(commentable) - else - check_availability(self) - end + notifiable_resource.present? && + !(notifiable_resource.respond_to?(:hidden?) && notifiable_resource.hidden?) && + !(notifiable_resource.respond_to?(:retired?) && notifiable_resource.retired?) end - def check_availability(resource) - resource.present? && - !(resource.respond_to?(:hidden?) && resource.hidden?) && - !(resource.respond_to?(:retired?) && resource.retired?) + def notifiable_resource + case self.class.name + when "ProposalNotification" + proposal + when "Comment" + commentable + else + self + end end def linkable_resource diff --git a/app/models/notification.rb b/app/models/notification.rb index 10d045350..b4d7739a1 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -10,8 +10,7 @@ class Notification < ApplicationRecord scope :recent, -> { order(id: :desc) } scope :for_render, -> { includes(:notifiable) } - delegate :notifiable_title, :notifiable_body, :notifiable_available?, - :check_availability, :linkable_resource, + delegate :notifiable_title, :notifiable_body, :notifiable_available?, :linkable_resource, to: :notifiable, allow_nil: true def mark_as_read diff --git a/spec/models/proposal_notification_spec.rb b/spec/models/proposal_notification_spec.rb index 179ba2876..3f9447ced 100644 --- a/spec/models/proposal_notification_spec.rb +++ b/spec/models/proposal_notification_spec.rb @@ -126,27 +126,21 @@ describe ProposalNotification do expect(notification.notifiable_available?).to be false end - end - - describe "check_availability" do - it "returns true if the resource is present, not hidden, nor retired" do - notification = create(:notification, notifiable: notifiable) - - expect(notification.check_availability(proposal)).to be true - end it "returns false if the resource is not present" do notification = create(:notification, notifiable: notifiable) notifiable.proposal.really_destroy! - expect(notification.check_availability(proposal)).to be false + + expect(notification.notifiable_available?).to be false end it "returns false if the resource is hidden" do notification = create(:notification, notifiable: notifiable) notifiable.proposal.hide - expect(notification.check_availability(proposal)).to be false + + expect(notification.notifiable_available?).to be false end it "returns false if the resource is retired" do @@ -155,7 +149,8 @@ describe ProposalNotification do notifiable.proposal.update!(retired_at: Time.current, retired_explanation: "Unfeasible reason explanation", retired_reason: "unfeasible") - expect(notification.check_availability(proposal)).to be false + + expect(notification.notifiable_available?).to be false end end diff --git a/spec/shared/models/notifiable.rb b/spec/shared/models/notifiable.rb index ac40462a0..a290ad9a5 100644 --- a/spec/shared/models/notifiable.rb +++ b/spec/shared/models/notifiable.rb @@ -17,7 +17,7 @@ shared_examples "notifiable" do end describe "notifiable_available?" do - it "returns true when it's a root comment and the notifiable is available" do + it "returns true if the resource is present, not hidden, nor retired" do notification = create(:notification, notifiable: notifiable) expect(notification.notifiable_available?).to be true @@ -30,7 +30,7 @@ shared_examples "notifiable" do expect(notification.notifiable_available?).to be true end - it "returns false when it's a root comment and the notifiable has been hidden" do + it "returns false if the resource is hidden" do notification = create(:notification, notifiable: notifiable) notifiable.hide @@ -48,24 +48,12 @@ shared_examples "notifiable" do expect(notification.notifiable_available?).to be false end - end - - describe "check_availability" do - it "returns true if the resource is present, not hidden, nor retired" do - notification = create(:notification, notifiable: notifiable) - expect(notification.check_availability(notifiable)).to be true - end it "returns false if the resource is not present" do notification = create(:notification, notifiable: notifiable) notifiable.really_destroy! - expect(notification.check_availability(notifiable)).to be false - end - it "returns false if the resource is not hidden" do - notification = create(:notification, notifiable: notifiable) - notifiable.hide - expect(notification.check_availability(notifiable)).to be false + expect(notification.notifiable_available?).to be false end it "returns false if the resource is retired" do @@ -74,7 +62,7 @@ shared_examples "notifiable" do if notifiable.respond_to?(:retired_at) notifiable.update!(retired_at: Time.current, retired_reason: "unfeasible", retired_explanation: "Unfeasibility explanation ...") - expect(notification.check_availability(notifiable)).to be false + expect(notification.notifiable_available?).to be false end end end