Skip to content

Commit 55ddcfa

Browse files
committed
Including contamination reports to enable survivors report contamination of other survivors
1 parent 80f40ef commit 55ddcfa

11 files changed

+154
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ContaminationReportBlueprint < Blueprinter::Base
2+
#== ASSOCIATIONS =========================================
3+
association :accuser, blueprint: SurvivorBlueprint
4+
association :suspect, blueprint: SurvivorBlueprint
5+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Api::V1
2+
class ContaminationReportsController < ApplicationController
3+
# POST /contamination_report/
4+
def create
5+
@contamination_report = ContaminationReport.new(contamination_report_params)
6+
7+
if @contamination_report.save
8+
render json: ContaminationReportBlueprint.render(@contamination_report), status: :created
9+
else
10+
render json: @contamination_report.errors, status: :unprocessable_entity
11+
end
12+
end
13+
14+
private
15+
16+
def contamination_report_params
17+
params.require(:contamination_report).permit(:accuser_id, :suspect_id)
18+
end
19+
end
20+
end

app/models/contamination_report.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class ContaminationReport < ApplicationRecord
2+
#== ASSOCIATIONS =========================================
3+
belongs_to :accuser, class_name: 'Survivor'
4+
belongs_to :suspect, class_name: 'Survivor'
5+
6+
#== VALIDATIONS ==========================================
7+
validates :accuser, :suspect, presence: true
8+
validates :suspect, uniqueness: { scope: :accuser_id }
9+
10+
validate :accusations_count
11+
12+
#== CALLBACKS ============================================
13+
after_save :update_survivor
14+
15+
def accusations_count
16+
total_accusations = suspect&.accusers&.count || 0
17+
if total_accusations > 2
18+
errors.add(:suspect, "have 3 accuations limit")
19+
end
20+
end
21+
22+
def update_survivor
23+
total_accusations = suspect&.accusers&.count || 0
24+
if total_accusations > 2
25+
suspect.update(infected: true)
26+
end
27+
end
28+
end

app/models/survivor.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ class Survivor < ApplicationRecord
33
has_one :last_location, class_name: "Location", foreign_key: :survivor_id
44
has_many :inventory_items
55
has_many :items, through: :inventory_items
6+
7+
has_many :accusers, class_name: 'ContaminationReport', foreign_key: :suspect_id
8+
has_many :suspects, class_name: 'ContaminationReport', foreign_key: :accuser_id
69

710
#== ACCEPTED ATTRIBUTES ==================================
811
enum gender: { female: 'female', male: 'male' }

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
put :last_location, on: :member
77
post :trade, on: :collection
88
end
9+
10+
resources :contamination_reports, only: :create
911
end
1012
end
1113

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateContaminationReports < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :contamination_reports do |t|
4+
t.references :accuser, null: false, foreign_key: { to_table: :survivors }
5+
t.references :suspect, null: false, foreign_key: { to_table: :survivors }
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddInfectedFlagToSurvivors < ActiveRecord::Migration[6.0]
2+
def change
3+
add_column :survivors, :infected, :boolean, default: false
4+
add_index :contamination_reports, [:accuser_id, :suspect_id], unique: true
5+
end
6+
end

db/schema.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_01_17_140644) do
13+
ActiveRecord::Schema.define(version: 2020_01_20_035617) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
1717

18+
create_table "contamination_reports", force: :cascade do |t|
19+
t.bigint "accuser_id", null: false
20+
t.bigint "suspect_id", null: false
21+
t.datetime "created_at", precision: 6, null: false
22+
t.datetime "updated_at", precision: 6, null: false
23+
t.index ["accuser_id", "suspect_id"], name: "index_contamination_reports_on_accuser_id_and_suspect_id", unique: true
24+
t.index ["accuser_id"], name: "index_contamination_reports_on_accuser_id"
25+
t.index ["suspect_id"], name: "index_contamination_reports_on_suspect_id"
26+
end
27+
1828
create_table "inventory_items", force: :cascade do |t|
1929
t.integer "quantity"
2030
t.bigint "survivor_id", null: false
@@ -45,8 +55,11 @@
4555
t.integer "age"
4656
t.datetime "created_at", precision: 6, null: false
4757
t.datetime "updated_at", precision: 6, null: false
58+
t.boolean "infected", default: false
4859
end
4960

61+
add_foreign_key "contamination_reports", "survivors", column: "accuser_id"
62+
add_foreign_key "contamination_reports", "survivors", column: "suspect_id"
5063
add_foreign_key "inventory_items", "items"
5164
add_foreign_key "inventory_items", "survivors"
5265
add_foreign_key "locations", "survivors"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :contamination_report do
3+
accuser { create(:survivor) }
4+
suspect { create(:survivor) }
5+
end
6+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe ContaminationReport, type: :model do
4+
describe "associations" do
5+
it { should belong_to(:accuser).class_name("Survivor") }
6+
it { should belong_to(:suspect).class_name("Survivor") }
7+
end
8+
9+
describe "validations" do
10+
subject { create(:contamination_report) }
11+
12+
it { should validate_presence_of(:accuser) }
13+
it { should validate_presence_of(:suspect) }
14+
15+
it { should validate_uniqueness_of(:suspect).scoped_to(:accuser_id) }
16+
17+
describe "custom validations" do
18+
before(:each) do
19+
@survivor = create(:survivor)
20+
create_list(:contamination_report, 3, suspect: @survivor)
21+
end
22+
23+
it "accusers limit" do
24+
contamination_report = ContaminationReport.new(accuser: create(:survivor), suspect: @survivor)
25+
expect(contamination_report.valid?).to eq(false)
26+
expect(contamination_report.errors.messages).to eq({ suspect: ["have 3 accuations limit"] })
27+
end
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)