Commit c82e891c authored by Benjamin Neff's avatar Benjamin Neff

Add reference source and target behaviour

parent 28d32719
......@@ -14,6 +14,7 @@ class Comment < ApplicationRecord
include Diaspora::Taggable
include Diaspora::Likeable
include Diaspora::MentionsContainer
include Reference::Source
acts_as_taggable_on :tags
extract_tags_from :text
......
......@@ -5,6 +5,8 @@ class Message < ApplicationRecord
include Diaspora::Fields::Guid
include Diaspora::Fields::Author
include Reference::Source
belongs_to :conversation, touch: true
delegate :name, to: :author, prefix: true
......
......@@ -4,4 +4,20 @@ class Reference < ApplicationRecord
belongs_to :source, polymorphic: true
belongs_to :target, polymorphic: true
validates :target_id, uniqueness: {scope: %i[target_type source_id source_type]}
module Source
extend ActiveSupport::Concern
included do
has_many :references, as: :source, dependent: :destroy
end
end
module Target
extend ActiveSupport::Concern
included do
has_many :referenced_by, as: :target, class_name: "Reference", dependent: :destroy
end
end
end
......@@ -7,6 +7,9 @@
class StatusMessage < Post
include Diaspora::Taggable
include Reference::Source
include Reference::Target
include PeopleHelper
acts_as_taggable_on :tags
......
......@@ -10,6 +10,7 @@ describe Comment, type: :model do
let(:comment_alice) { alice.comment!(status_bob, "why so formal?") }
it_behaves_like "it is mentions container"
it_behaves_like "a reference source"
describe "#destroy" do
it "should delete a participation" do
......
......@@ -52,4 +52,6 @@ describe Message, type: :model do
expect(conf.reload.unread).to eq(1)
end
end
it_behaves_like "a reference source"
end
......@@ -147,6 +147,9 @@ describe StatusMessage, type: :model do
end
end
it_behaves_like "a reference source"
it_behaves_like "a reference target"
describe "#nsfw" do
it "returns MatchObject (true) if the post contains #nsfw (however capitalised)" do
status = FactoryGirl.build(:status_message, text: "This message is #nSFw")
......
# frozen_string_literal: true
shared_examples_for "a reference source" do
let!(:source) { FactoryGirl.create(described_class.to_s.underscore.to_sym) }
let!(:reference) { FactoryGirl.create(:reference, source: source) }
describe "references" do
it "returns the references" do
expect(source.references).to match_array([reference])
end
it "destroys the reference when the source is destroyed" do
source.destroy
expect(Reference.where(id: reference.id)).not_to exist
end
end
end
shared_examples_for "a reference target" do
let!(:target) { FactoryGirl.create(described_class.to_s.underscore.to_sym) }
let!(:reference) { FactoryGirl.create(:reference, target: target) }
describe "referenced_by" do
it "returns the references where the target is referenced" do
expect(target.referenced_by).to match_array([reference])
end
it "destroys the reference when the target is destroyed" do
target.destroy
expect(Reference.where(id: reference.id)).not_to exist
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment