Create refereces model
This commit is contained in:
parent
35711606e4
commit
28d3271933
4 changed files with 50 additions and 0 deletions
7
app/models/reference.rb
Normal file
7
app/models/reference.rb
Normal file
|
|
@ -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
|
||||||
16
db/migrate/20170920214158_create_references_table.rb
Normal file
16
db/migrate/20170920214158_create_references_table.rb
Normal file
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
22
spec/models/reference_spec.rb
Normal file
22
spec/models/reference_spec.rb
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue