diff --git a/app/models/reference.rb b/app/models/reference.rb new file mode 100644 index 000000000..926915a7c --- /dev/null +++ b/app/models/reference.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +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]} +end diff --git a/db/migrate/20170920214158_create_references_table.rb b/db/migrate/20170920214158_create_references_table.rb new file mode 100644 index 000000000..75ce6c109 --- /dev/null +++ b/db/migrate/20170920214158_create_references_table.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class CreateReferencesTable < ActiveRecord::Migration[5.1] + def change + create_table :references do |t| + t.integer :source_id, null: false + t.string :source_type, limit: 60, null: false + t.integer :target_id, null: false + t.string :target_type, limit: 60, null: false + end + + add_index :references, %i[source_id source_type target_id target_type], + name: :index_references_on_source_and_target, unique: true + add_index :references, %i[source_id source_type], name: :index_references_on_source_id_and_source_type + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 0c246e7c1..0c08448d0 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -258,6 +258,11 @@ FactoryGirl.define do end end + factory :reference do + association :source, factory: :status_message + association :target, factory: :status_message + end + factory(:notification, class: Notifications::AlsoCommented) do association :recipient, :factory => :user association :target, :factory => :comment diff --git a/spec/models/reference_spec.rb b/spec/models/reference_spec.rb new file mode 100644 index 000000000..6d5514e32 --- /dev/null +++ b/spec/models/reference_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Reference, type: :model do + context "validation" do + it "validates a valid reference" do + expect(FactoryGirl.build(:reference)).to be_valid + end + + it "requires a source" do + expect(FactoryGirl.build(:reference, source: nil)).not_to be_valid + end + + it "requires a target" do + expect(FactoryGirl.build(:reference, target: nil)).not_to be_valid + end + + it "disallows to link the same target twice from one source" do + reference = FactoryGirl.create(:reference) + expect(FactoryGirl.build(:reference, source: reference.source, target: reference.target)).not_to be_valid + end + end +end