Commit 28d32719 authored by Benjamin Neff's avatar Benjamin Neff

Create refereces model

parent 35711606
# 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
# 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
...@@ -258,6 +258,11 @@ FactoryGirl.define do ...@@ -258,6 +258,11 @@ FactoryGirl.define do
end end
end end
factory :reference do
association :source, factory: :status_message
association :target, factory: :status_message
end
factory(:notification, class: Notifications::AlsoCommented) do factory(:notification, class: Notifications::AlsoCommented) do
association :recipient, :factory => :user association :recipient, :factory => :user
association :target, :factory => :comment association :target, :factory => :comment
......
# 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
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