Skip to content

Commit

Permalink
Merge pull request #38 from Code-the-Dream-School/HER-38-create-avail…
Browse files Browse the repository at this point in the history
…ability-model

“HER-38_CREATE_AVAILABILITY_MODEL”
  • Loading branch information
olesiamironenko authored Jan 4, 2025
2 parents aeedcef + 94a31ca commit 6c8a786
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/models/availability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Availability < ApplicationRecord
# Associations
belongs_to :speaker, -> { where(role: "speaker") }, class_name: "User"
belongs_to :recurring_availability, optional: true

# Validations
validates :start_time, :end_time, :speaker, presence: true

# Validation for time range
validate :start_time_before_end_time

private

def start_time_before_end_time
if start_time.present? && end_time.present? && start_time >= end_time
errors.add(:start_time, "must be before the end time")
end
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class User < ApplicationRecord
has_many :contacts
has_many :addresses, as: :addressable
has_many :events, foreign_key: :speaker_id, dependent: :nullify
has_many :availabilities, foreign_key: :speaker_id, dependent: :destroy

before_create :set_default_role

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20250103100137_create_availabilities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateAvailabilities < ActiveRecord::Migration[7.2]
def change
create_table :availabilities do |t|
t.datetime :start_time, null: false
t.datetime :end_time, null: false
t.references :speaker, null: false, foreign_key: { to_table: :users }
t.references :recurring_availability, null: true, foreign_key: true

t.timestamps
end
end
end
15 changes: 14 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/factories/availabilities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :availability do
start_time { Faker::Time.forward(days: 7, period: :morning) }
end_time { start_time + 1.hour }
association :speaker, factory: [ :user, :speaker ]
association :recurring_availability
end
end
4 changes: 4 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
role { "admin" }
end

trait :speaker do
role { "speaker" }
end

trait :regular_user do
role { "user" }
end
Expand Down
48 changes: 48 additions & 0 deletions spec/models/availability_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rails_helper'

RSpec.describe Availability, type: :model do
let(:speaker) { create(:user, role: "speaker") }

describe "associations" do
it { should belong_to(:speaker).class_name("User") }
it { should belong_to(:recurring_availability).optional }
end

describe "validations" do
it { should validate_presence_of(:start_time) }
it { should validate_presence_of(:end_time) }
it { should validate_presence_of(:speaker) }
end

describe "time range validations" do
context "when start_time is before end_time" do
it "is valid" do
availability = build(:availability, speaker: speaker, start_time: Time.zone.now, end_time: 1.hour.from_now)
expect(availability).to be_valid
end
end

context "when start_time is equal to or after end_time" do
it "is invalid when start_time is after end_time" do
availability = build(:availability, speaker: speaker, start_time: 1.hour.from_now, end_time: Time.zone.now)
expect(availability).not_to be_valid
expect(availability.errors[:start_time]).to include("must be before the end time")
end
end
end

describe "optional associations validation" do
it "creates an availability with a speaker and optional recurring_availability" do
recurring_availability = create(:recurring_availability)
availability = create(:availability, speaker: speaker, recurring_availability: recurring_availability)
expect(availability.speaker).to eq(speaker)
expect(availability.recurring_availability).to eq(recurring_availability)
end

it "creates an availability without a recurring_availability" do
availability = create(:availability, speaker: speaker, recurring_availability: nil)
expect(availability.speaker).to eq(speaker)
expect(availability.recurring_availability).to be_nil
end
end
end

0 comments on commit 6c8a786

Please sign in to comment.