Add reference source and target behaviour
This commit is contained in:
parent
28d3271933
commit
c82e891c03
8 changed files with 61 additions and 0 deletions
|
|
@ -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")
|
||||
|
|
|
|||
33
spec/shared_behaviors/references.rb
Normal file
33
spec/shared_behaviors/references.rb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# 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
|
||||
Loading…
Reference in a new issue