-
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.
Allow sections to be unique within lessons only
- Add migration to fix the unique index - Add scope to the uniqueness validation - Add user factory - Add user association to lesson factory - Lesson factory corrections - Add section factory - Add section model spec NOTE: We modify the start_time and end_time validations to provide a default value for lesson.duration_in_seconds because there's an issue with shoulda matchers and validations that refer to associated models according to this issue thoughtbot/shoulda-matchers#1435
- Loading branch information
1 parent
252ea32
commit fb0267d
Showing
7 changed files
with
58 additions
and
9 deletions.
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
6 changes: 6 additions & 0 deletions
6
db/migrate/20240723030225_fix_section_name_uniqueness_index.rb
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,6 @@ | ||
class FixSectionNameUniquenessIndex < ActiveRecord::Migration[7.0] | ||
def change | ||
remove_index :sections, :name | ||
add_index :sections, [:name, :lesson_id], unique: true | ||
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
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,16 @@ | ||
FactoryBot.define do | ||
factory :section do | ||
lesson | ||
sequence(:name) { |n| "Section #{n}" } | ||
|
||
start_time_hour { 0 } | ||
start_time_minute { 0 } | ||
start_time_second { 1 } | ||
|
||
end_time_hour { 0 } | ||
end_time_minute { 0 } | ||
end_time_second { Faker::Number.between(from: 1, to: 59 ) } | ||
|
||
playback_speed { 0.5.step(by: 0.5, to: 2.0).to_a.sample } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FactoryBot.define do | ||
factory :user do | ||
sequence(:email) { |n| "user#{n}@example.com" } | ||
password { "password123" } | ||
confirmed_at { Date.yesterday } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'rails_helper' | ||
|
||
describe Section, type: :model do | ||
subject { create(:section) } | ||
|
||
describe 'associations' do | ||
it { should belong_to(:lesson) } | ||
end | ||
|
||
describe 'validations' do | ||
it { should validate_uniqueness_of(:name).scoped_to(:lesson_id) } | ||
end | ||
end |