-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Code-the-Dream-School/HER-38-create-avail…
…ability-model “HER-38_CREATE_AVAILABILITY_MODEL”
- Loading branch information
Showing
7 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |